この Java コード例を使用すると、SMTP 経由で Alibaba Mail からメールを送信できます。
Alibaba Mail の設定
SMTP サーバーアドレス:smtp.sg.aliyun.com
ポート:暗号化されていない接続の場合は 25、SSL 暗号化接続の場合は 465。
// pom.xml ファイルに javax.mail への参照を追加するか、javax.mail JAR パッケージをプロジェクトにインポートします。
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.Security;
import java.util.Date;
import java.util.Properties;
public class EnterpriseMailSend {
public static void main(String[] args) {
try{
// SSL 接続とメール環境を設定します。
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties props = System.getProperties();
// プロトコル
//props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", "smtp.sg.aliyun.com"); // SMTP サーバーアドレス
//props.setProperty("mail.smtp.port", "25"); // 非暗号化ポート
// SSL 暗号化を使用する場合は、次の設定を適用します。
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.socketFactory.port", "465");
props.setProperty("mail.smtp.ssl.enable", "true");
//props.setProperty("mail.smtp.ssl.protocols", "TLSv1.2"); // デフォルトのバージョンで失敗する場合は、バージョンの指定を試してください。
props.setProperty("mail.smtp.auth", "true"); // SMTP でのメール送信に ID 認証が必要であることを指定します。
props.setProperty("mail.smtp.from", "sender_address"); // mailfrom パラメーター。
props.setProperty("mail.user","sender_address"); // 送信者のアカウント。
props.setProperty("mail.password","sender_password"); // 送信者アカウントのパスワード。サードパーティ製クライアントのセキュリティパスワードを有効にしている場合は、新しく生成されたパスワードを使用します。
// メールセッションを作成します。
Session session = Session.getDefaultInstance(props, new Authenticator() {
// ID 認証。
protected PasswordAuthentication getPasswordAuthentication() {
// 送信者のアカウントとパスワード。
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
});
// メールオブジェクトを作成します。
MimeMessage message = new MimeMessage(session);
// メール送信者を設定します。
InternetAddress from = new InternetAddress("sender_address","sender_name"); // from パラメーター。中継に使用できます。注:中継メールは、受信者のサーバーに拒否される、または迷惑メールフォルダーに振り分けられる可能性が高くなります。
message.setFrom(from);
// メール受信者を設定します。
String[] to = {"recipient_address_1","recipient_address_2","recipient_address_3"}; // 受信者リスト。
InternetAddress[] sendTo = new InternetAddress[to.length];
for (int i=0;i<to.length;i++){
//System.out.println("Sending to: " + to[i]);
sendTo[i] = new InternetAddress(to[i]);
}
// 受信者を渡します。
message.setRecipients(Message.RecipientType.TO,sendTo);
// メールの件名を設定します。
message.setSubject("email_subject");
// メール本文を設定します。
String content="email_body";
message.setContent(content,"text/html;charset=UTF-8");
// 時刻を設定します。
message.setSentDate(new Date());
message.saveChanges();
// メールを送信します。
Transport.send(message);
System.out.println("Sent successfully!");
}catch(Exception e){
System.out.println("Exception: "+e.toString());
}
}
}
接続テストコマンド:
telnet smtp.sg.aliyun.com 25
openssl s_client -connect smtp.sg.aliyun.com:465 -crlf