1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

springframeworkのJavaMailSenderでメールを送る際の送信主に名前を付与する

Posted at

たまたま、JavaのプロジェクトとKotlinのプロジェクトの両方で送信主に名前を付与して送信する処理を作成したので、備忘録として投稿します。

##Java開発環境

version
java 11
SpringBoot 2.1.0.RELEASE

既存のSimpleMailMessageを用いた処理の場合、InternetAddressを使用することが出来なかったので、MimeMessageを用いた処理に変更

変更前
SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom("hoge@.com");
            message.setTo("hoge");
            message.setSubject("subject");
            message.setText("body");

InternetAddressクラスを用いて、personalと形式を設定

InternetAddressクラス
InternetAddress(StringSE address, StringSE personal, StringSE charset)
変更後
MimeMessage message = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true , "UTF-8");
            helper.setFrom(new InternetAddress( "hoge@.com" ,"不動産SHOPナカジツ", "UTF-8"));
            helper.setTo("hoge");
            helper.setSubject("subject");
            helper.setText("body");

##Kotlinでの設定
こちらはJavaで設定した変更後の処理と同様の処理

version
Kotlin 1.3.70
SpringBoot 2.3.4.RELEASE
val message = javaMailSender!!.createMimeMessage()
val helper = MimeMessageHelper(message, true)
helper.setFrom(InternetAddress("hoge@.com", "不動産SHOPナカジツ", "utf-8"))
helper.setTo("hoge")
helper.setSubject("subject")
helper.setText("body")

###参考
https://spring.pleiades.io/specifications/platform/8/apidocs/javax/mail/internet/internetaddress

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?