Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JavaMail + Groovy samples.

JavaMail + Groovyのサンプル

GroovyでJavaMailの使い方を勉強した時のサンプルコードです。 基本的に JavaMailでのメール送信まとめその1 - あられねこのめも をGroovyでちょこっと手直しした感じのものになります。

サンプルコードの解説

01 - 09 まではひたすらMimeMessageを元にしたメッセージの組み立て方の勉強です。

  • 01_message_plaintext_ascii.groovy
    • 英数字だけの一番シンプルなメッセージ
  • 02_message_plaintext_iso2022jp.groovy
    • ISO-2022-JPでエンコードするシンプルなメッセージ
  • 03_message_plaintext_utf8.groovy
    • UTF-8でエンコードするシンプルなメッセージ
  • 04_message_singlehtml_utf8.groovy
    • UTF-8でシングルパートのHTMLメールを作成
  • 05_message_singlehtml_wrong_contenttype.groovy
    • Content-Typeでわざと間違った値を指定する例
  • 06_message_multipart_txt_and_html.groovy
    • 簡単なHTMLメールの例で、alternativeパートを使ってテキストパートも格納した例
  • 07_message_multipart_txt_and_attachment.groovy
    • テキストメール + 添付ファイルの例
  • 08_message_multipart_html_and_image.groovy
    • HTMLメール + インラインで画像を参照する例
  • 09_message_multipart_html_and_attachment.groovy
    • ↑に加えて、さらに添付ファイルを格納した例

10番についてはメールアドレスの指定方法の勉強例です。

  • 10_mailaddress.groovy
    • メールアドレスの設定方法のバリエーション

11 - 13 まではSMTPサーバへの接続のバリエーションです。

  • 11_sendmail_noauth_plainsock.groovy
    • 認証なし + 非SSL/TLSの場合
  • 12_sendmail_auth_plainsock.groovy
    • 認証あり + 非SSL/TLSの場合
  • 13_sendmail_gmail.groovy
    • GMailのSMTPサーバをSSL/TLSで使う場合

参考

JavaMail関連

ローカルテスト用のJava製メールサーバ

blog記事

  • JavaMailでのメール送信まとめその1 - あられねこのめも
    • http://d.hatena.ne.jp/ttshrk/20110405/1301998708
    • JavaMailでのメッセージの構築パターンの解説。添付ファイル付きだったり、HTMLメールを送信するパターンなど網羅されている。
    • 本Gistのソースコードの元ネタ
  • groovyあれこれ: groovyとjavamailで添付ファイル付のメールを送信する

動作確認

Java7, JavaMail 1.4.7, Groovy 1.8.9, Win7SP1上で動作確認しています。

@Grab(group='javax.mail', module='mail', version='1.4.7')
import java.util.*
import javax.mail.*
import javax.mail.internet.*
import javax.activation.*
// create empty dummy session
Properties props = new Properties()
Session session = Session.getDefaultInstance(props)
Message msg = new MimeMessage(session)
msg.setFrom(new InternetAddress('foo@example.com'))
msg.setSender(new InternetAddress('sender@example.com'))
msg.setRecipient(Message.RecipientType.TO, new InternetAddress('to@example.com'))
msg.setSubject('subject test', 'utf-8')
msg.setText('mail body test', 'utf-8', 'plain')
msg.writeTo(System.out)
/*
From: foo@example.com
Sender: sender@example.com
To: to@example.com
Message-ID: <1803322541.0.1372486737048.JavaMail.FengJing@LB-L400B-120908>
Subject: subject test
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
mail body test
*/
@Grab(group='javax.mail', module='mail', version='1.4.7')
import java.util.*
import javax.mail.*
import javax.mail.internet.*
import javax.activation.*
// create empty dummy session
Properties props = new Properties()
Session session = Session.getDefaultInstance(props)
Message msg = new MimeMessage(session)
msg.setFrom(new InternetAddress('foo@example.com'))
msg.setSender(new InternetAddress('sender@example.com'))
msg.setRecipient(Message.RecipientType.TO, new InternetAddress('to@example.com'))
msg.setSubject('題名', 'iso-2022-jp')
msg.setText('テキスト本文', 'iso-2022-jp', 'plain')
msg.writeTo(System.out)
/*
From: foo@example.com
Sender: sender@example.com
To: to@example.com
Message-ID: <446102999.0.1372487425843.JavaMail.FengJing@LB-L400B-120908>
Subject: =?iso-2022-jp?B?GyRCQmpMPhsoQg==?=
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-2022-jp
Content-Transfer-Encoding: quoted-printable
=1B$B%F%-%9%HK\J8=1B(B
*/
@Grab(group='javax.mail', module='mail', version='1.4.7')
import java.util.*
import javax.mail.*
import javax.mail.internet.*
import javax.activation.*
// create empty dummy session
Properties props = new Properties()
Session session = Session.getDefaultInstance(props)
Message msg = new MimeMessage(session)
msg.setFrom(new InternetAddress('foo@example.com'))
msg.setSender(new InternetAddress('sender@example.com'))
msg.setRecipient(Message.RecipientType.TO, new InternetAddress('to@example.com'))
msg.setSubject('題名', 'utf-8')
msg.setText('テキスト本文', 'utf-8', 'plain')
msg.writeTo(System.out)
/*
From: foo@example.com
Sender: sender@example.com
To: to@example.com
Message-ID: <709055691.0.1372487455534.JavaMail.FengJing@LB-L400B-120908>
Subject: =?utf-8?B?6aGM5ZCN?=
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: base64
44OG44Kt44K544OI5pys5paH
*/
@Grab(group='javax.mail', module='mail', version='1.4.7')
import java.util.*
import javax.mail.*
import javax.mail.internet.*
import javax.activation.*
// create empty dummy session
Properties props = new Properties()
Session session = Session.getDefaultInstance(props)
Message msg = new MimeMessage(session)
msg.setFrom(new InternetAddress('foo@example.com'))
msg.setSender(new InternetAddress('sender@example.com'))
msg.setRecipient(Message.RecipientType.TO, new InternetAddress('to@example.com'))
msg.setSubject('題名', 'utf-8')
msg.setContent('<html><body>HTMLメール本文(singlepart)</body></html>', 'text/html; charset=utf-8')
msg.setHeader('Content-Transfer-Encoding', 'base64')
msg.writeTo(System.out)
/*
From: foo@example.com
Sender: sender@example.com
To: to@example.com
Message-ID: <150227713.0.1372487484590.JavaMail.FengJing@LB-L400B-120908>
Subject: =?utf-8?B?6aGM5ZCN?=
MIME-Version: 1.0
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: base64
PGh0bWw+PGJvZHk+SFRNTOODoeODvOODq+acrOaWhyhzaW5nbGVwYXJ0KTwvYm9keT48L2h0bWw+
*/
@Grab(group='javax.mail', module='mail', version='1.4.7')
import java.util.*
import javax.mail.*
import javax.mail.internet.*
import javax.activation.*
// create empty dummy session
Properties props = new Properties()
Session session = Session.getDefaultInstance(props)
Message msg = new MimeMessage(session)
msg.setFrom(new InternetAddress('foo@example.com'))
msg.setSender(new InternetAddress('sender@example.com'))
msg.setRecipient(Message.RecipientType.TO, new InternetAddress('to@example.com'))
msg.setSubject('題名', 'utf-8')
msg.setText('<html><body>HTMLメール本文(singlepart)</body></html>', 'utf-8', 'plain')
msg.setHeader('Content-Transfer-Encoding', 'base64')
msg.setHeader('Content-Type', 'text/html; charset=euc-jp')
msg.writeTo(System.out)
/*
From: foo@example.com
Sender: sender@example.com
To: to@example.com
Message-ID: <1900109164.0.1372488009259.JavaMail.FengJing@LB-L400B-120908>
Subject: =?utf-8?B?6aGM5ZCN?=
MIME-Version: 1.0
Content-Type: text/html; charset=euc-jp
Content-Transfer-Encoding: base64
PGh0bWw+PGJvZHk+SFRNTOODoeODvOODq+acrOaWhyhzaW5nbGVwYXJ0KTwvYm9keT48L2h0bWw+
*/
@Grab(group='javax.mail', module='mail', version='1.4.7')
import java.util.*
import javax.mail.*
import javax.mail.internet.*
import javax.activation.*
// create empty dummy session
Properties props = new Properties()
Session session = Session.getDefaultInstance(props)
Message msg = new MimeMessage(session)
msg.setFrom(new InternetAddress('foo@example.com'))
msg.setSender(new InternetAddress('sender@example.com'))
msg.setRecipient(Message.RecipientType.TO, new InternetAddress('to@example.com'))
msg.setSubject('multipart : alternative text and html', 'utf-8')
/*
alternative/
text
html
*/
Multipart mp = new MimeMultipart('alternative')
// alternative text part
MimeBodyPart textpart = new MimeBodyPart()
textpart.setText('テキストパートの本文を記述', 'utf-8', 'plain')
textpart.setHeader('Content-Transfer-Encoding', 'base64')
mp.addBodyPart(textpart)
// html part
MimeBodyPart htmlpart = new MimeBodyPart()
htmlpart.setText('<html><body>HTMLメールの本文を記述</body></html>', 'utf-8', 'html')
htmlpart.setHeader('Content-Transfer-Encoding', 'base64')
mp.addBodyPart(htmlpart)
msg.setContent(mp)
msg.writeTo(System.out)
/*
From: foo@example.com
Sender: sender@example.com
To: to@example.com
Message-ID: <1989344978.1.1372488286833.JavaMail.FengJing@LB-L400B-120908>
Subject: multipart : alternative text and html
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="----=_Part_0_1614843520.1372488286814"
------=_Part_0_1614843520.1372488286814
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: base64
44OG44Kt44K544OI44OR44O844OI44Gu5pys5paH44KS6KiY6L+w
------=_Part_0_1614843520.1372488286814
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: base64
PGh0bWw+PGJvZHk+SFRNTOODoeODvOODq+OBruacrOaWh+OCkuiomOi/sDwvYm9keT48L2h0bWw+
------=_Part_0_1614843520.1372488286814--
*/
@Grab(group='javax.mail', module='mail', version='1.4.7')
import java.util.*
import javax.mail.*
import javax.mail.internet.*
import javax.activation.*
// create empty dummy session
Properties props = new Properties()
Session session = Session.getDefaultInstance(props)
Message msg = new MimeMessage(session)
msg.setFrom(new InternetAddress('foo@example.com'))
msg.setSender(new InternetAddress('sender@example.com'))
msg.setRecipient(Message.RecipientType.TO, new InternetAddress('to@example.com'))
msg.setSubject('multipart : text and attachment file', 'utf-8')
/*
mixed/
text
attachment
*/
Multipart mp = new MimeMultipart('mixed')
// text part
MimeBodyPart textpart = new MimeBodyPart()
textpart.setText('テキストパートの本文を記述', 'utf-8', 'plain')
textpart.setHeader('Content-Transfer-Encoding', 'base64')
mp.addBodyPart(textpart)
// attachment file part
MimeBodyPart filepart = new MimeBodyPart()
DataSource datasrc = new FileDataSource('test.gif')
filepart.setDataHandler(new DataHandler(datasrc))
filepart.setFileName(MimeUtility.encodeWord('overrided_filename.gif'))
mp.addBodyPart(filepart)
msg.setContent(mp)
msg.writeTo(System.out)
/*
From: foo@example.com
Sender: sender@example.com
To: to@example.com
Message-ID: <1253071236.1.1372489142058.JavaMail.FengJing@LB-L400B-120908>
Subject: multipart : text and attachment file
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_0_1554390911.1372489142023"
------=_Part_0_1554390911.1372489142023
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: base64
44OG44Kt44K544OI44OR44O844OI44Gu5pys5paH44KS6KiY6L+w
------=_Part_0_1554390911.1372489142023
Content-Type: application/octet-stream; name=overrided_filename.gif
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=overrided_filename.gif
R0lGODlhKAAoAIAAAP3+/wAAACH5BAAAAAAALAAAAAAoACgAAAInhI+py+0Po5y02ouz3rz7D4bi
SJbmiabqyrbuC8fyTNf2jef6ziMFADs=
------=_Part_0_1554390911.1372489142023--
*/
@Grab(group='javax.mail', module='mail', version='1.4.7')
import java.util.*
import javax.mail.*
import javax.mail.internet.*
import javax.activation.*
// create empty dummy session
Properties props = new Properties()
Session session = Session.getDefaultInstance(props)
Message msg = new MimeMessage(session)
msg.setFrom(new InternetAddress('foo@example.com'))
msg.setSender(new InternetAddress('sender@example.com'))
msg.setRecipient(Message.RecipientType.TO, new InternetAddress('to@example.com'))
msg.setSubject('multipart : html and inline image', 'utf-8')
/*
alternative/
text
related/
html
image
*/
Multipart root = new MimeMultipart('alternative')
// text part
MimeBodyPart textpart = new MimeBodyPart()
textpart.setText('テキストパートの本文を記述', 'utf-8', 'plain')
textpart.setHeader('Content-Transfer-Encoding', 'base64')
root.addBodyPart(textpart)
// related part
MimeBodyPart relatedbody = new MimeBodyPart()
Multipart relatedpart = new MimeMultipart('related')
relatedbody.setContent(relatedpart)
root.addBodyPart(relatedbody)
// html part
MimeBodyPart htmlpart = new MimeBodyPart()
htmlpart.setText('<html><body>HTMLメールの本文を記述<img src="cid:12345@12345"></body></html>', 'utf-8', 'html')
htmlpart.setHeader('Content-Transfer-Encoding', 'base64')
relatedpart.addBodyPart(htmlpart)
// inline image part
MimeBodyPart imagepart = new MimeBodyPart()
DataSource datasrc = new FileDataSource('test.gif')
imagepart.setDataHandler(new DataHandler(datasrc))
imagepart.setFileName(MimeUtility.encodeWord('overrided_filename.gif'))
imagepart.setDisposition('inline')
imagepart.setContentID('12345@12345')
relatedpart.addBodyPart(imagepart)
msg.setContent(root)
msg.writeTo(System.out)
/*
From: foo@example.com
Sender: sender@example.com
To: to@example.com
Message-ID: <852534370.2.1372494492209.JavaMail.FengJing@LB-L400B-120908>
Subject: multipart : html and inline image
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="----=_Part_0_232656449.1372494492174"
------=_Part_0_232656449.1372494492174
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: base64
44OG44Kt44K544OI44OR44O844OI44Gu5pys5paH44KS6KiY6L+w
------=_Part_0_232656449.1372494492174
Content-Type: multipart/related;
boundary="----=_Part_1_1126385923.1372494492190"
------=_Part_1_1126385923.1372494492190
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: base64
PGh0bWw+PGJvZHk+SFRNTOODoeODvOODq+OBruacrOaWh+OCkuiomOi/sDxpbWcgc3JjPSJjaWQ6
MTIzNDVAMTIzNDUiPjwvYm9keT48L2h0bWw+
------=_Part_1_1126385923.1372494492190
Content-Type: application/octet-stream; name=overrided_filename.gif
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename=overrided_filename.gif
Content-ID: 12345@12345
R0lGODlhKAAoAIAAAP3+/wAAACH5BAAAAAAALAAAAAAoACgAAAInhI+py+0Po5y02ouz3rz7D4bi
SJbmiabqyrbuC8fyTNf2jef6ziMFADs=
------=_Part_1_1126385923.1372494492190--
------=_Part_0_232656449.1372494492174--
*/
@Grab(group='javax.mail', module='mail', version='1.4.7')
import java.util.*
import javax.mail.*
import javax.mail.internet.*
import javax.activation.*
// create empty dummy session
Properties props = new Properties()
Session session = Session.getDefaultInstance(props)
Message msg = new MimeMessage(session)
msg.setFrom(new InternetAddress('foo@example.com'))
msg.setSender(new InternetAddress('sender@example.com'))
msg.setRecipient(Message.RecipientType.TO, new InternetAddress('to@example.com'))
msg.setSubject('multipart : html and inline image', 'utf-8')
/*
mixed/
alternative/
text
related/
html
image
attachment
*/
Multipart root = new MimeMultipart('mixed')
// alternative part
MimeBodyPart altbody = new MimeBodyPart()
Multipart altpart = new MimeMultipart('alternative')
altbody.setContent(altpart)
root.addBodyPart(altbody)
// text part
MimeBodyPart textpart = new MimeBodyPart()
textpart.setText('テキストパートの本文を記述', 'utf-8', 'plain')
textpart.setHeader('Content-Transfer-Encoding', 'base64')
altpart.addBodyPart(textpart)
// related part
MimeBodyPart relatedbody = new MimeBodyPart()
Multipart relatedpart = new MimeMultipart('related')
relatedbody.setContent(relatedpart)
altpart.addBodyPart(relatedbody)
// html part
MimeBodyPart htmlpart = new MimeBodyPart()
htmlpart.setText('<html><body>HTMLメールの本文を記述<img src="cid:12345@12345"></body></html>', 'utf-8', 'html')
htmlpart.setHeader('Content-Transfer-Encoding', 'base64')
relatedpart.addBodyPart(htmlpart)
// inline image part
MimeBodyPart imagepart = new MimeBodyPart()
DataSource datasrc = new FileDataSource('test.gif')
imagepart.setDataHandler(new DataHandler(datasrc))
imagepart.setFileName(MimeUtility.encodeWord('overrided_filename.gif'))
imagepart.setDisposition('inline')
imagepart.setContentID('12345@12345')
relatedpart.addBodyPart(imagepart)
// attachment file part
MimeBodyPart filepart = new MimeBodyPart()
DataSource filesrc = new FileDataSource('test.gif')
filepart.setDataHandler(new DataHandler(filesrc))
filepart.setFileName(MimeUtility.encodeWord('attach.gif'))
root.addBodyPart(filepart)
msg.setContent(root)
msg.writeTo(System.out)
/*
From: foo@example.com
Sender: sender@example.com
To: to@example.com
Message-ID: <56926606.3.1372495057544.JavaMail.FengJing@LB-L400B-120908>
Subject: multipart : html and inline image
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_0_161317831.1372495057508"
------=_Part_0_161317831.1372495057508
Content-Type: multipart/alternative;
boundary="----=_Part_1_989393009.1372495057521"
------=_Part_1_989393009.1372495057521
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: base64
44OG44Kt44K544OI44OR44O844OI44Gu5pys5paH44KS6KiY6L+w
------=_Part_1_989393009.1372495057521
Content-Type: multipart/related;
boundary="----=_Part_2_1445416312.1372495057524"
------=_Part_2_1445416312.1372495057524
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: base64
PGh0bWw+PGJvZHk+SFRNTOODoeODvOODq+OBruacrOaWh+OCkuiomOi/sDxpbWcgc3JjPSJjaWQ6
MTIzNDVAMTIzNDUiPjwvYm9keT48L2h0bWw+
------=_Part_2_1445416312.1372495057524
Content-Type: application/octet-stream; name=overrided_filename.gif
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename=overrided_filename.gif
Content-ID: 12345@12345
R0lGODlhKAAoAIAAAP3+/wAAACH5BAAAAAAALAAAAAAoACgAAAInhI+py+0Po5y02ouz3rz7D4bi
SJbmiabqyrbuC8fyTNf2jef6ziMFADs=
------=_Part_2_1445416312.1372495057524--
------=_Part_1_989393009.1372495057521--
------=_Part_0_161317831.1372495057508
Content-Type: application/octet-stream; name=attach.gif
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=attach.gif
R0lGODlhKAAoAIAAAP3+/wAAACH5BAAAAAAALAAAAAAoACgAAAInhI+py+0Po5y02ouz3rz7D4bi
SJbmiabqyrbuC8fyTNf2jef6ziMFADs=
------=_Part_0_161317831.1372495057508--
*/
@Grab(group='javax.mail', module='mail', version='1.4.7')
import java.util.*
import javax.mail.*
import javax.mail.internet.*
import javax.activation.*
// create empty dummy session
Properties props = new Properties()
Session session = Session.getDefaultInstance(props)
Message msg = new MimeMessage(session)
msg.setFrom(new InternetAddress('foo@example.com', '日本語名前', 'utf-8'))
msg.setSender(new InternetAddress('sender@example.com', '日本語名前その2', 'iso-2022-jp'))
msg.setRecipients(Message.RecipientType.TO, [
new InternetAddress('to1@example.com', '宛先1', 'utf-8'),
new InternetAddress('to2@example.com', '宛先2', 'euc-jp'),
] as InternetAddress[])
msg.setSubject('題名', 'utf-8')
msg.setText('テキスト本文', 'utf-8', 'plain')
msg.writeTo(System.out)
/*
From: =?utf-8?B?5pel5pys6Kqe5ZCN5YmN?= <foo@example.com>
Sender: =?iso-2022-jp?B?GyRCRnxLXDhsTD5BMCQ9JE4jMhsoQg==?= <sender@example.com>
To: =?utf-8?B?5a6b5YWIMQ==?= <to1@example.com>,
=?euc-jp?B?sLjA6DI=?= <to2@example.com>
Message-ID: <2039375403.0.1372496050584.JavaMail.FengJing@LB-L400B-120908>
Subject: =?utf-8?B?6aGM5ZCN?=
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: base64
44OG44Kt44K544OI5pys5paH
*/
@Grab(group='javax.mail', module='mail', version='1.4.7')
import java.util.*
import javax.mail.*
import javax.mail.internet.*
import javax.activation.*
// create plain socket session without smtp authentication
Properties props = new Properties()
props.put('mail.smtp.host', 'localhost')
props.put('mail.smtp.port', '10025')
props.put('mail.smtp.auth', 'false')
props.put('mail.debug', 'true')
Session session = Session.getDefaultInstance(props)
Message msg = new MimeMessage(session)
msg.setFrom(new InternetAddress('foo@example.com'))
msg.setSender(new InternetAddress('sender@example.com'))
msg.setRecipient(Message.RecipientType.TO, new InternetAddress('to@example.com'))
msg.setSubject('subject test', 'utf-8')
msg.setText('mail body test', 'utf-8', 'plain')
Transport.send(msg)
@Grab(group='javax.mail', module='mail', version='1.4.7')
import java.util.*
import javax.mail.*
import javax.mail.internet.*
import javax.activation.*
// create plain socket session without smtp authentication
Properties props = new Properties()
props.put('mail.smtp.host', 'localhost')
props.put('mail.smtp.port', '10025')
props.put('mail.smtp.auth', 'true')
props.put('mail.debug', 'true')
Session session = Session.getDefaultInstance(props,
[
getPasswordAuthentication: {
return new PasswordAuthentication('smtp_login_id', 'password')
}
] as Authenticator
)
Message msg = new MimeMessage(session)
msg.setFrom(new InternetAddress('foo@example.com'))
msg.setSender(new InternetAddress('sender@example.com'))
msg.setRecipient(Message.RecipientType.TO, new InternetAddress('to@example.com'))
msg.setSubject('subject test', 'utf-8')
msg.setText('mail body test', 'utf-8', 'plain')
Transport.send(msg)
@Grab(group='javax.mail', module='mail', version='1.4.7')
import java.util.*
import javax.mail.*
import javax.mail.internet.*
import javax.activation.*
// create plain socket session without smtp authentication
Properties props = new Properties()
props.put('mail.debug', 'true')
// set timeouts
props.put('mail.smtps.connectiontimeout', '5000')
props.put('mail.smtps.timeout', '5000')
Session session = Session.getDefaultInstance(props)
Message msg = new MimeMessage(session)
msg.setFrom(new InternetAddress('sakamoto.gsyc.3s@gmail.com'))
msg.setSender(new InternetAddress('sakamoto.gsyc.3s@gmail.com'))
msg.setRecipient(Message.RecipientType.TO, new InternetAddress('sakamoto.gsyc.3s@gmail.com'))
msg.setSubject('subject test', 'utf-8')
msg.setText('mail body test', 'utf-8', 'plain')
// see : http://www.oracle.com/technetwork/java/javamail/faq/index.html#gmail
Transport t = session.getTransport('smtps')
t.connect('smtp.gmail.com', 465, 'you@gmail.com', 'password')
t.sendMessage(msg, msg.getAllRecipients())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.