我们编写了通过Gmail SMTP服务器发送邮件的代码。它在Windows 7机器上运行良好,但在Windows 8机器上无效。
配置
Outgoing Mail (SMTP) Server
requires TLS or SSL: smtp.gmail.com (use authentication)
Use Authentication: Yes
Port for TLS/STARTTLS: 587
Port for SSL: 465
代码
final String username = "aaa@gmail.com";
final String password = "aaa";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
String itemNames = getItemNames(itemSet);
String purchaseNote = "";
if (delAddress == null) {
purchaseNote = "You can collect the items in our store by giveing the tracking number with in 10 days.";
} else {
purchaseNote = "N/A";
}
Message message = new MimeMessage(session);
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("arjun@gmail.com"));
message.setSubject("SuperDeal Purchase Detail");
message.setText("Dear " + customer.getFullName() + "\n\n" + "Purchase Items: " + itemNames + "\n" + "Total Cost: " + totalCost + "\n" + "Tracking No: " + trackingNumber + "\n" + "Purchase Note: " + purchaseNote + "\n\n" + "Note: If you have any queries please call to our hot line 0112345647 any time.");
Transport.send(message);
} catch (MessagingException exception) {
LOGGER.error(exception.getMessage(), exception);
}
错误
68352 [http-bio-8080-exec-55] ERROR com.superdeal.mail.MailSender - can't determine local email address
javax.mail.MessagingException: can't determine local email address
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:906)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583)
at javax.mail.Transport.send0(Transport.java:169)
at javax.mail.Transport.send(Transport.java:98)
at com.superdeal.mail.MailSender.send(MailSender.java:66)
at com.superdeal.servlet.AddOrderServlet.processRequest(AddOrderServlet.java:91)
at com.superdeal.servlet.AddOrderServlet.doPost(AddOrderServlet.java:239)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:409)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1044)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
71600 [http-bio-8080-exec-54] ERROR com.superdeal.mail.MailSender - can't determine local email address
javax.mail.MessagingException: can't determine local email address
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:906)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583)
at javax.mail.Transport.send0(Transport.java:169)
at javax.mail.Transport.send(Transport.java:98)
at com.superdeal.mail.MailSender.send(MailSender.java:66)
at com.superdeal.servlet.AddOrderServlet.processRequest(AddOrderServlet.java:91)
at com.superdeal.servlet.AddOrderServlet.doPost(AddOrderServlet.java:239)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:409)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1044)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
请告诉我为什么它在Windows7中工作正常,在Windows8中不起作用。
答案 0 :(得分:1)
您没有在邮件中设置发件人地址,JavaMail无法确定要使用的地址。后者通常是因为JDK无法确定本地主机的主机名,可能是因为它使用DHCP获取IP地址,可能是因为本地计算机上的名称服务配置不正确。
无论如何,简单的解决方法是在邮件上设置一个发件人地址,无论如何你应该这样做。