Email email = new SimpleEmail();
String authuser = "[email protected]";
String authpwd = "*******";
// Very Important, Don't use email.setAuthentication()
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator(authuser, authpwd));
email.setDebug(true); // true if you want to debug
email.setHostName("smtp.gmail.com");
email.getMailSession().getProperties().put("mail.smtp.auth", "true");
email.getMailSession().getProperties().put("mail.debug", "true");
email.getMailSession().getProperties().put("mail.smtp.port", "465");
email.getMailSession().getProperties().put("mail.smtp.socketFactory.port", "465");
email.getMailSession().getProperties().put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
email.getMailSession().getProperties().put("mail.smtp.socketFactory.fallback", "false");
email.getMailSession().getProperties().put("mail.smtp.starttls.enable", "true");
email.setFrom("[email protected]", "SenderName");
email.setSubject("TestMail");
email.setMsg("This is a test mail?");
email.addTo("[email protected]", "ToName");
email.send();
和它提供了以下異常發送電子郵件使用共享的電子郵件到Gmail
SEVERE: org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.gmail.com:465
你能發佈整個異常堆棧跟蹤嗎?異常的原因可能在其中列出。例外是通用的。這可能是一個簡單的身份驗證失敗,也可能是其他問題。歡呼 – aldrin