0
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 1 year has passed since last update.

Google Workspace SMTP リレーを使用してメールを送信する

Posted at

Google Workspace 経由でメールを送信する方法で、その中でも
こちらの
https://support.google.com/a/answer/176600?hl=ja
オプション 1: SMTP リレーを使用してメールを送信する(推奨)
という方法をjavaで実装しました。

参照ライブラリは
・activation.jar
・additionnal.jar
・javax.mail.jar

以下サンプルコードです。

Main.java
/** メール環境情報**/
MailInfo mailInfo =new MailInfo();

/** メール送信プロパティ**/
Properties props = new Properties();
props.put("mail.debug", "true"); //true:ログの出力
props.put("mail.smtp.host", "smtp-relay.gmail.com"); //SMTPリレー使用のため、固定
props.put("mail.smtp.auth", "false"); //SMTP認証 false:認証しない
props.put("mail.smtp.port", "587"); //ポート番号の指定
props.put("mail.smtp.starttls.enable", "true"); //TLS接続設定
props.put("mail.smtp.ssl.trust", "smtp-relay.gmail.com");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
Session session = Session.getInstance(props);

// 送信元
String mail = "from@gmail.com";
// 宛先
String to = "to@gmail.com";
// Cc
String cc = "cc@gmail.com";
// Bcc
String bcc = "bcc@gmail.com";
// 件名
String subject = "こちら件名になります。";
// 本文
String text = "こちら本文になります。";

/**メール送信元・送信先を設定する**/
final javax.mail.Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(mail));
msg.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(to));
msg.setRecipients(javax.mail.Message.RecipientType.CC, InternetAddress.parse(cc));	 msg.setRecipients(javax.mail.Message.RecipientType.BCC, InternetAddress.parse(bcc));

/**メール内容の設定**/
msg.setSubject(subject);
msg.setText(text);

/**メールの送信**/
Transport.send(msg);

エンジニア初心者のため、間違いなどありましたらご指摘くださいませ。

0
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
0
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?