我不熟悉这个在java中发送邮件的功能。我在发送电子邮件以重置密码时遇到错误。希望你能给我一个解决方案。
下面是我的代码:
public synchronized static boolean sendMailAdvance(String emailTo, String subject, String body)
{
String host = AppConfigManager.getProperty("SENDER-EMAIL-SMTP-ADDRESS");
String userName = AppConfigManager.getProperty("SENDER-EMAIL-SMTP-USERNAME");
String password = AppConfigManager.getProperty("SENDER-EMAIL-SMTP-PASSWORD");
String port = AppConfigManager.getProperty("SENDER-EMAIL-SMTP-PORT");
String starttls = AppConfigManager.getProperty("SENDER-EMAIL-SMTP-STARTTLS");
String socketFactoryClass = AppConfigManager.getProperty("SENDER-EMAIL-SMTP-SOCKET-CLASS");
String fallback = AppConfigManager.getProperty("SENDER-EMAIL-SMTP-ALLOW-FALLBACK");
try
{
java.util.Properties props = null;
props = System.getProperties();
props.put("mail.smtp.user", userName);
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.debug", "true");
if(!"".equals(port))
{
props.put("mail.smtp.port", port);
props.put("mail.smtp.socketFactory.port", port);
}
if(!"".equals(starttls))
props.put("mail.smtp.starttls.enable",starttls);
if(!"".equals(socketFactoryClass))
props.put("mail.smtp.socketFactory.class",socketFactoryClass);
if(!"".equals(fallback))
props.put("mail.smtp.socketFactory.fallback", fallback);
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(userName));
msg.setSubject(subject);
msg.setText(body, "ISO-8859-1");
msg.setSentDate(new Date());
msg.setHeader("content-Type", "text/html;charset=\"ISO-8859-1\"");
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(emailTo));
msg.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host, userName, password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
return true;
}
catch (Exception mex)
{
mex.printStackTrace();
return false;
}
}
引发以下错误:
调试:setDebug:JavaMail 版本 1.4.1ea-SNAPSHOT
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
调试 SMTP:useEhlo true,useAuth true
调试 SMTP:尝试连接到主机“smtp.gmail.com”,端口 465,isSSL false 220 mx.google.com ESMTP m4sm5929870pbg.38 - gsmtp
调试 SMTP:连接到主机“smtp.gmail.com”,端口:465
EHLO脂肪
250-mx.google.com 为您服务,[175.139.198.14]
250 尺寸 35882577
250-8BITMIME
250-AUTH 登录 PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN
250 增强状态代码
第250章
调试 SMTP:找到扩展名“SIZE”,arg“35882577”
调试 SMTP:找到扩展名“8BITMIME”,arg“”
调试 SMTP:找到扩展名“AUTH”,arg“LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN”
调试 SMTP:找到扩展“ENHANCEDSTATUSCODES”,arg“”
调试 SMTP:找到扩展名“CHUNKING”,arg“”
调试 SMTP:尝试进行身份验证
授权登录
334 VXNlcm5hbWU6
YWNjb3VudEBibG9vbWluZy5jb20ubXk=
第334章
Ymxvb21pbmc= 535-5.7.8 用户名和密码不被接受。如需了解详情,请访问 535 5.7.8 http://support.google.com/mail/bin/answer.py?answer=14257
m4sm5929870pbg.38 - gsmtp
[STDOUT] javax.mail.AuthenticationFailedException
[STDOUT] 在 javax.mail.Service.connect(Service.java:319)
[STDOUT] 在 javax.mail.Service.connect(Service.java:169)
[STDOUT] 在 com.vlee.util.mail.SendMail.sendMailAdvance(SendMail.java:283)
[STDOUT] 在 com.vlee.servlet.ecommerce.DoMemberLogin.fnSendPwd(DoMemberLogin.java:251)
[STDOUT] 在 com.vlee.servlet.ecommerce.DoMemberLogin.doPost(DoMemberLogin.java:72)