JavaMailを使ったメール送信

Maven Repository

http://mvnrepository.com/artifact/com.sun.mail

http://mvnrepository.com/artifact/com.sun.mail/javax.mail/1.5.2

Charset(ISO-2022-JP)

setSubject、setTextのString charsetに”ISO-2022-JP”を指定する。

Bエンコード

MimeUtilityを使う。

Content-Transfer-Encoding: 7bit

順番が大事。

http://blog.livedoor.jp/tak_bon/archives/5909426.html

Envelope From

SMTPMessage#setEnvelopeFrom(String) を使うと設定できる。

http://slashdot.jp/journal/460267/

Message ID

メッセージIDは自動で生成される。独自のものを設定するときはメソッドをオーバーライトして作る。

http://stackoverflow.com/questions/8366843/how-do-i-set-or-change-the-smtp-message-id-with-javax-mail

http://www.oracle.com/technetwork/java/faq-135477.html#msgid

まとめ

普通のメールっぽく送る場合は、次のようにする。

MimeMessage msg = new MimeMessage(session);
msg.setSubject(MimeUtility.encodeText("JavaMailからの日本語", "ISO-2022-JP", "B"));
msg.setText("メールの本文", "ISO-2022-JP");
msg.setHeader("Content-Transfer-Encoding", "7bit");

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *