主要參考以下文章:http://www.andowson.com/posts/list/196.page

以下程式碼要先掛載以下 Jar :Apache Commons Email, Sun JavaMail API, JavaBeans Activation Framework (JAF)

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

public class Test {

    public static void main(String[] args) {
        String subject = "測試使用 Gmail SMTP SSL發信";
        String message = "<html><head><title>測試</title></head><body>這是一封測試信,收到請自行刪除 </body></html>";

        Email email = new HtmlEmail();

        String authuser = "username@gmail.com";
        String authpwd = "the_password";
        email.setHostName("smtp.gmail.com");

        email.setSmtpPort(465);
        email.setAuthenticator(new DefaultAuthenticator(authuser, authpwd));
        email.setDebug(true);
        email.setSSL(true);
        email.setSslSmtpPort("465");
        email.setCharset("UTF-8");
        email.setSubject(subject);
        try {
            email.setFrom("username@gmail.com", "網站客服中心");
            email.setMsg(message);
            email.addTo("somebody@30elite.com", "親愛的會員");
            email.send();
            System.out.println("郵件發送成功");
        } catch (EmailException e) {
            e.printStackTrace();
        }
    }
}




arrow
arrow
    全站熱搜

    黃彥霖 發表在 痞客邦 留言(0) 人氣()