Javamail-api-gmail-smtp-server
JavaMail API-Gmail SMTPサーバー
前のすべての章では、JangoSMTPサーバーを使用してメールを送信しました。 この章では、Gmailが提供するSMTPサーバーについて学びます。 Gmail(特に)は、パブリックSMTPサーバーの使用を無料で提供しています。
Gmail SMTPサーバーの詳細は、https://support.google.com/mail/answer/13287?hl = ja [こちら]で確認できます。 詳細でわかるように、TLSまたはSSL接続を使用して、Gmail SMTPサーバー経由でメールを送信できます。
Gmail SMTPサーバーを使用してメールを送信する手順は、リンクの章:/javamail_api/javamail_api_sending_emails [メールの送信]で説明されている手順と似ていますが、ホストサーバーを変更する点が異なります。 前提条件として、送信者のメールアドレスはアクティブなGmailアカウントである必要があります。 例を試してみましょう。
Javaクラスを作成する
Javaファイル SendEmailUsingGMailSMTP を作成します。その内容は次のとおりです。
package com.finddevguides;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendEmailUsingGMailSMTP {
public static void main(String[] args) {
//Recipient's email ID needs to be mentioned.
String to = "[email protected]";//change accordingly
//Sender's email ID needs to be mentioned
String from = "[email protected]";//change accordingly
final String username = "abc";//change accordingly
final String password = "*****";//change accordingly
//Assuming you are sending email through relay.jangosmtp.net
String host = "smtp.gmail.com";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");
//Get the Session object.
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
//Create a default MimeMessage object.
Message message = new MimeMessage(session);
//Set From: header field of the header.
message.setFrom(new InternetAddress(from));
//Set To: header field of the header.
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
//Set Subject: header field
message.setSubject("Testing Subject");
//Now set the actual message
message.setText("Hello, this is sample for to check send "
+ "email using JavaMailAPI ");
//Send message
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
ここでは、ホストは_smtp.gmail.com_として設定され、ポートは_587_として設定されます。 ここで、TLS接続を有効にしました。
コンパイルして実行する
クラスの準備ができたので、上記のクラスをコンパイルしましょう。 クラスSendEmailUsingGMailSMTP.javaをディレクトリに保存しました: /home/manisha/JavaMailAPIExercise 。 クラスパスに_javax.mail.jar_および_activation.jar_のjarが必要です。 以下のコマンドを実行して、コマンドプロンプトからクラス(両方のjarが/home/manisha/ディレクトリに配置されます)をコンパイルします。
javac -cp/home/manisha/activation.jar:/home/manisha/javax.mail.jar: SendEmailUsingGMailSMTP.java
クラスがコンパイルされたので、次のコマンドを実行して実行します。
java -cp/home/manisha/activation.jar:/home/manisha/javax.mail.jar: SendEmailUsingGMailSMTP
出力を検証する
コマンドコンソールに次のメッセージが表示されます。
Sent message successfully....