土曜日, 6月 20, 2009

[Scala] GAE/JのMailサービス

本文は普通に日本語文章を与えたらOK。サブジェクトだけ自分でいじることにしました。

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

class sendmail(strFrom:String) {

var props:Properties = new Properties();
var session:Session = Session.getDefaultInstance(props, null);

def send(strTo:String, strSubject:String, strBody:String) {
try {
var msg:Message = new MimeMessage(session);
msg.setFrom(new InternetAddress(strFrom));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress(strTo, "Mr. User"));

msg.setSubject(MimeUtility.encodeText(strSubject,"iso-2022-jp","B"));
msg.setText(strBody+"\n");

Transport.send(msg);

} catch {
case e:AddressException => printf("Exp_A(%s)\n", e.getMessage)
case e:MessagingException => printf("Exp_B(%s)\n", e.getMessage)
case e => e.printStackTrace
}
}
}
エラー処理とかとても適当ですが、何かの参考に。

blog comments powered by Disqus

Twitter Updates

 
.