2009-11-23 5 views
6
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(); 

und es gibt die folgende AusnahmeE-Mail verwenden Commons-E-Mail an Google Mail gesendet

SEVERE: org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.gmail.com:465 
+0

Können Sie die gesamte Ausnahmestapelüberwachung posten? Die Ursache der Ausnahme ist wahrscheinlich irgendwo darin aufgeführt. Die Ausnahme ist generisch. Es könnte ein einfacher Authentifizierungsfehler sein oder es könnte etwas anderes sein. – aldrin

Antwort

14

Das funktioniert für mich

Email email = new SimpleEmail(); 
String authuser = "[email protected]"; 
String authpwd = "xxxx"; 
email.setSmtpPort(587); 
email.setAuthenticator(new DefaultAuthenticator(authuser, authpwd)); 
email.setDebug(true); 
email.setHostName("smtp.gmail.com"); 
email.getMailSession().getProperties().put("mail.smtps.auth", "true"); 
email.getMailSession().getProperties().put("mail.debug", "true"); 
email.getMailSession().getProperties().put("mail.smtps.port", "587"); 
email.getMailSession().getProperties().put("mail.smtps.socketFactory.port", "587"); 
email.getMailSession().getProperties().put("mail.smtps.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
email.getMailSession().getProperties().put("mail.smtps.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.setTLS(true); 
email.send(); 
+0

prost Versuchte es mit smtps auch nicht – user217029

+0

funktioniert nicht mit 587 arbeiten als well..still nicht – user217029

+0

prüfen erweitert funktioniert Antwort wieder – jitter

10

nicht Sie tell Commons Email that you're sending a TLS email benötigen Sie:

email.setTLS(true); 

vor Ihrem Anruf auf email.send()?

Ich bin mir nicht sicher, ob das behebt, was Sie plagt, da ich nicht sicher bin, ob bei der Verbindung mit smtp.gmail.com:465 ein Problem auftritt oder erfolgreich gesendet wurde (die Fehlermeldung/Ausnahme ist zweideutig, wie Sie es vorgestellt haben), aber es ist definitiv etwas, was fehlt, soweit ich es beurteilen kann.

13

Die Commons E-Mail Benutzerhandbuch hat ein Beispiel für Gmail SSL.

https://commons.apache.org/proper/commons-email/userguide.html

SSL/TLS (Port 465) -> email.setSSLOnConnect (true);

Email email = new SimpleEmail(); 
email.setHostName("smtp.gmail.com"); 
email.setSmtpPort(465); 
email.setAuthenticator(new DefaultAuthenticator("username", "password")); 
email.setSSLOnConnect(true); 
email.setFrom("[email protected]"); 
email.setSubject("TestMail"); 
email.setMsg("This is a test mail ... :-)"); 
email.addTo("[email protected]"); 
email.send(); 

STARTTLS (Port 587) -> email.setStartTLSEnabled (true);

Email email = new SimpleEmail(); 
email.setHostName("smtp.gmail.com"); 
email.setSmtpPort(587); 
email.setAuthenticator(new DefaultAuthenticator("username", "password")); 
email.setStartTLSEnabled(true); 
email.setFrom("[email protected]"); 
email.setSubject("TestMail"); 
email.setMsg("This is a test mail ... :-)"); 
email.addTo("[email protected]"); 
email.send(); 
+0

Dieser Code funktioniert für mich! Vielen Dank! – Ascension

+0

Ich erhalte Klasse javax.mail.MessagingException: 530 5.7.0 Muss zuerst einen STARTTLS-Befehl ausgeben. g66sm11757390ywh.8 - gsmtp Ausnahme. Aber ich habe den Code wie deinen Code aktualisiert. – Curious

Verwandte Themen