javamail发送Gmail报错

wangluoyu 2013-01-26 08:55:26
最近写的一个Gmail邮件发送程序,本地发送正常,一上线就发不出去,报错信息如下:
javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.AuthenticationFailedException
at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at com.util.mail.SendGMail.sendMail(SendGMail.java:70)
环境为linux+tomcat,
源码如下:

public class SendGMail {
/**
* 设置一些使用属性
*/
private static final String MAIL_CONTENT_TYPE = "text/html;charset=utf-8";

/**
* 设置Properties
*/
private static final String MAIL_SMTP_HOST = "smtp.gmail.com"; // 这是gmail的smtp服务器域名
private static final String MAIL_SMTP_PORT = "465"; // 注意:gmail的smtp服务器使用的是465端口
private static final String MAIL_SMTP_AUTH = "true";
private static final String MAIL_SMTP_SOCKETFACTORY_CLASS = "javax.net.ssl.SSLSocketFactory";
private static final String MAIL_SMTP_SOCKETFACTORY_FALLBACK = "false";
private static final String MAIL_SMTP_SOCKETFACTORY_PORT = "465";
private static final String MAIL_SMTP_SSL = "true";

public static void sendMail(String toEmail,String subject,String content) {
Properties props = System.getProperties();
props.setProperty("mail.smtp.host", MAIL_SMTP_HOST);
props.setProperty("mail.smtp.socketFactory.class", MAIL_SMTP_SOCKETFACTORY_CLASS);
props.setProperty("mail.smtp.socketFactory.fallback", MAIL_SMTP_SOCKETFACTORY_FALLBACK);
props.setProperty("mail.smtp.port", MAIL_SMTP_PORT);
props.setProperty("mail.smtp.socketFactory.port", MAIL_SMTP_SOCKETFACTORY_PORT);
props.setProperty("mail.smtp.auth", MAIL_SMTP_AUTH);
props.setProperty("mail.smtp.ssl", MAIL_SMTP_SSL);
props.put("mail.smtp.debug", "true");

Session session = Session.getDefaultInstance(props, new Authenticator() {
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication(SysType.GMAIL_CONFIG[0], SysType.GMAIL_CONFIG[1]);
}
});

// create a new message
Message message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(SysType.GMAIL_CONFIG[0]));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
message.setSubject(subject);

/** 设置邮件内容 */
Multipart multipart = new MimeMultipart();
BodyPart bodyPart = new MimeBodyPart();
bodyPart.setContent(content, MAIL_CONTENT_TYPE);
multipart.addBodyPart(bodyPart);

message.setContent(multipart);
message.setSentDate(new java.util.Date());
message.saveChanges();

Transport.send(message);
} catch(MessagingException ex) {
ex.printStackTrace();
}
}

public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
sb.append("test");
String toEmail = "00000000000@qq.com";
String subject = "test";
SendGMail.sendMail(toEmail,subject,sb.toString());
System.out.println("OK");
}
}

不知道是哪里有问题,盼各位大虾指教。
...全文
690 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangluoyu 2013-01-28
  • 打赏
  • 举报
回复
Debug 信息: DEBUG: JavaMail version 1.3.1 DEBUG: java.io.FileNotFoundException: /usr/local/java/jdk1.7.0_02/jre/lib/javamail.providers (No such file or directory) DEBUG: !anyLoaded DEBUG: not loading resource: /META-INF/javamail.providers DEBUG: successfully loaded resource: /META-INF/javamail.default.providers DEBUG: Tables of loaded providers DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]} DEBUG: Providers Listed By Protocol: {imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]} DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map DEBUG: !anyLoaded DEBUG: not loading resource: /META-INF/javamail.address.map DEBUG: java.io.FileNotFoundException: /usr/local/java/jdk1.7.0_02/jre/lib/javamail.address.map (No such file or directory) DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc] DEBUG SMTP: useEhlo true, useAuth true DEBUG SMTP: useEhlo true, useAuth true DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587 220 mx.google.com ESMTP se8sm2112512pbb.69 - gsmtp DEBUG SMTP: connected to host "smtp.gmail.com", port: 465 EHLO li421-97 250-mx.google.com at your service, [50.116.3.97] 250-SIZE 35882577 250-8BITMIME 250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 250 ENHANCEDSTATUSCODES DEBUG SMTP: Found extension "SIZE", arg "35882577" DEBUG SMTP: Found extension "8BITMIME", arg "" DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH XOAUTH2" DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg "" DEBUG SMTP: Attempt to authenticate AUTH LOGIN 334 VXNlcm5hbWU6 a2luYXNlY2hlbUBnbWFpbC5jb20= 334 UGFzc3dvcmQ6 a2luZzM5MjU= 535-5.7.1 Please log in with your web browser and then try again. Learn more at 535 5.7.1 https://support.google.com/mail/bin/answer.py?answer=78754 se8sm2112512pbb.69 - gsmtp javax.mail.SendFailedException: Sending failed; nested exception is: class javax.mail.AuthenticationFailedException at javax.mail.Transport.send0(Transport.java:218) at javax.mail.Transport.send(Transport.java:80) at com.util.mail.SendGMail.sendMail(SendGMail.java:75) at com.selleck.action.front.MemberAction.sendMail(MemberAction.java:221) at com.selleck.action.front.MemberAction.doReg(MemberAction.java:197) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601)
wangluoyu 2013-01-27
  • 打赏
  • 举报
回复
求高手指点啊
  • 打赏
  • 举报
回复
看下gmail邮箱是否开启smtp协议。
wangluoyu 2013-01-26
  • 打赏
  • 举报
回复
开启着呢吧,我在本地测试,邮件可以正常发送。

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧