我有一个示例gmail客户端类。在那个课程中,我初始化两个IMAPS& SMTPS会话。 有一段时间它工作正常。但现在Gmail会抛出SMTO发送失败异常。
以下是我通过javamail连接Gmail的方式..
//String smtpHost = "imap.gmail.com";
//String imapHost = "smtp.gmail.com";
//String userName = "XXXXXX@gmail.com";
//String userPass = "PASSWORD";
//Properties props = new Properties();
public DirectMailBox(String smtpHost, String imapHost, String userName,
String userPass, Properties props) throws MessagingException {
super();
this.imapHost = imapHost;
this.smtpHost = smtpHost;
this.userName = userName;
this.userPass = userPass;
this.props = props;
session = Session.getDefaultInstance(props);
session.setDebug(true);
imapsStore = session.getStore("imaps");
imapsStore.connect(imapHost, userName, userPass);
smtpsTransport = session.getTransport("smtps");
smtpsTransport.connect(smtpHost, userName, userPass);
smtpsTransport.addTransportListener(this);
}
以下是我如何调用sendmail ..
public void sendMail(MimeMessage newMessage) throws MessagingException {
smtpsTransport.sendMessage(newMessage, newMessage.getAllRecipients());
}
此网址上的以下说明但没有解决方案。 http://support.google.com/mail/bin/answer.py?answer=14257
这是一个调试转储.....
DEBUG: setDebug: JavaMail version 1.4ea
DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc]
DEBUG: mail.imap.fetchsize: 16384
* OK Gimap ready for requests from 14.140.225.250 uy8if8067087pbc.8
A0 CAPABILITY
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH
A0 OK Thats all she wrote! uy8if8067087pbc.8
IMAP DEBUG: AUTH: XOAUTH
DEBUG: protocolConnect login, host=smtp.gmail.com, user=XXXXXX@gmail.com, password=<non-null>
A1 LOGIN XXXXXX@gmail.com PASSWORD
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE
A1 OK XXXXXX@gmail.com Rakesh Waghela authenticated (Success)
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "imap.gmail.com", port 465, isSSL true
220 mx.google.com ESMTP mt9sm3371719pbb.14
DEBUG SMTP: connected to host "imap.gmail.com", port: 465
EHLO LPT0031
250-mx.google.com at your service, [14.140.225.250]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH
250 ENHANCEDSTATUSCODES
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG: setDebug: JavaMail version 1.4ea
DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc]
DEBUG: mail.imap.fetchsize: 16384
* OK Gimap ready for requests from 14.140.225.250 vf8if4407540pbc.7
A0 CAPABILITY
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH
A0 OK Thats all she wrote! vf8if4407540pbc.7
IMAP DEBUG: AUTH: XOAUTH
DEBUG: protocolConnect login, host=smtp.gmail.com, user=XXXXXX@gmail.com, password=<non-null>
A1 LOGIN XXXXXX@gmail.com PASSWORD
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE
A1 OK XXXXXX@gmail.com Rakesh Waghela authenticated (Success)
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "imap.gmail.com", port 465, isSSL true
220 mx.google.com ESMTP ru4sm3359950pbc.66
DEBUG SMTP: connected to host "imap.gmail.com", port: 465
EHLO LPT0031
250-mx.google.com at your service, [14.140.225.250]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH
250 ENHANCEDSTATUSCODES
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: use8bit false
MAIL FROM:<RakeshW@LPT0031>
530-5.5.1 Authentication Required. Learn more at
530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 mt9sm3371719pbb.14
com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required. Learn more at
530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 mt9sm3371719pbb.14
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583)
at com.wellogic.phoenix.mail.direct.DirectMailBox.sendMail(DirectMailBox.java:86)
at com.wellogic.phoenix.test.DirectMailTest.testSendMail(DirectMailTest.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Not Delivered...
答案 0 :(得分:5)
终于解决了这个问题.. !!使用以下方法。
http://mlusincuba.wordpress.com/2012/03/03/java-mail-gmail-smtp-client/
package com.mycompany.employee.utils;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
* @author Mlungisi
*
*/
public class GMail {
public static void sendMail(String senderEmail, String recipientEmail, String subject, String message) throws MessagingException, UnsupportedEncodingException {
// SSL // I USED THIS METHOD
Properties propsSSL = new Properties();
// EVEN IF YOU SKIP THESE TWO PROP IT WOULD WORK
propsSSL.put("mail.transport.protocol", "smtps");
propsSSL.put("mail.smtps.host", "smtp.gmail.com");
// THIS IS THE MOST IMPORTANT PROP --> "mail.smtps.auth"
propsSSL.put("mail.smtps.auth", "true");
Session sessionSSL = Session.getInstance(propsSSL);
sessionSSL.setDebug(true);
Message messageSSL = new MimeMessage(sessionSSL);
messageSSL.setFrom(new InternetAddress("yourgmail@gmail.com", "Mlungisi Sincuba"));
messageSSL.setRecipients(Message.RecipientType.TO, InternetAddress.parse("yourgmail@gmail.com")); // real recipient
messageSSL.setSubject("Test mail using SSL");
messageSSL.setText("This is test email sent to Your account using SSL.");
Transport transportSSL = sessionSSL.getTransport();
// EVEN IF YOU SKIP PORT NUMBER , IT WOULD WORK
transportSSL.connect("smtp.gmail.com", 465, "yourgmail@gmail.com", "yourpassword"); // account used
transportSSL.sendMessage(messageSSL, messageSSL.getAllRecipients());
transportSSL.close();
System.out.println("SSL done.");
}
public static void main(String[] args) throws MessagingException, UnsupportedEncodingException {
System.out.println("Hello World!");
sendMail(null, null, null, null);
}
}
答案 1 :(得分:0)
您似乎使用imap.gmail.com作为您的smtp服务器。您也可以尝试打开smtp auth。