2014年10月8日水曜日

JavaMailでsmtp-authでメールを送信する方法

    Properties props = new Properties();
    props.put("mail.transport.protocol", "smtp");  <----25か587のサブミッションポートでもok
    props.put("mail.smtp.host", SMTP_HOST_NAME);
    props.put("mail.smtp.auth", "true")

    Authenticator auth = new SMTPAuthenticator();
    Session mailSession = Session.getDefaultInstance(props, auth);

    // パスワード認証のプライベートクラス
    private class SMTPAuthenticator extends javax.mail.Authenticator {  
        public PasswordAuthentication getPasswordAuthentication() {
           String username = SMTP_AUTH_USER;
           String password = SMTP_AUTH_PWD;
           return new PasswordAuthentication(username, password);
        }
    }


http://www.rgagnon.com/javadetails/java-0538.html

0 件のコメント:

コメントを投稿