2016-03-09 256 views
2

我使用javax.mail-api 1.5.3發送電子郵件。這是我的電子郵件發送代碼:Java郵件不發送電子郵件

Properties properties = System.getProperties(); 
    properties.setProperty("mail.smtp.host", host); 
    properties.setProperty("mail.smtp.port", port); 
    properties.setProperty("mail.user", user); 
    properties.setProperty("mail.password", password); 
    properties.setProperty("mail.debug", "true"); 
    Session mailSession = Session.getDefaultInstance(properties); 
    MimeMessage mimeMessage = new MimeMessage(mailSession); 
    mimeMessage.setHeader("Content-Type", "text/html; charset=UTF-8"); 
    mimeMessage.setFrom(new InternetAddress(from)); 
    mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); 
    mimeMessage.setSubject(subject, "UTF-8"); 
    mimeMessage.setContent(body, "text/html; charset=UTF-8"); 
    Transport.send(mimeMessage); 

但是,此代碼會導致以下錯誤。

調試輸出:

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle] 
DEBUG SMTP: useEhlo true, useAuth false 
DEBUG SMTP: trying to connect to host "my.smtp.host", port 25, isSSL false 
220 <mail.mysite.com> <mysite> 
DEBUG SMTP: connected to host "my.smtp.host", port: 25 

EHLO my-pc 
250-mail.mysite.com Hello [192.168.120.120] 
250-SIZE 
250-PIPELINING 
250-DSN 
250-ENHANCEDSTATUSCODES 
250-AUTH LOGIN 
250-8BITMIME 
250-BINARYMIME 
250 CHUNKING 
DEBUG SMTP: Found extension "SIZE", arg "" 
DEBUG SMTP: Found extension "PIPELINING", arg "" 
DEBUG SMTP: Found extension "DSN", arg "" 
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg "" 
DEBUG SMTP: Found extension "AUTH", arg "LOGIN" 
DEBUG SMTP: Found extension "8BITMIME", arg "" 
DEBUG SMTP: Found extension "BINARYMIME", arg "" 
DEBUG SMTP: Found extension "CHUNKING", arg "" 
DEBUG SMTP: use8bit false 
MAIL FROM:<[email protected]> 
250 2.1.0 Sender OK 
RCPT TO:<[email protected]> 
550 5.7.1 Unable to relay 
DEBUG SMTP: Invalid Addresses 
DEBUG SMTP: [email protected] 
DEBUG SMTP: Sending failed because of invalid destination addresses 
RSET 
250 2.0.0 Resetting 
DEBUG SMTP: MessagingException while sending, THROW: 
javax.mail.SendFailedException: Invalid Addresses; 
    nested exception is: 
    com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay 

    at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1950) 
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1195) 
    at javax.mail.Transport.send0(Transport.java:254) 
    at javax.mail.Transport.send(Transport.java:124) 
    at org.bob.resume.service.MailService.send(MailService.java:80) 
    at org.bob.resume.service.MailService.send(MailService.java:89) 
    at org.bob.resume.service.MailService.send(MailService.java:95) 
    at org.bob.resume.controller.AdminController.submitPersonUser(AdminController.java:267) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) 
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) 
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) 
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776) 
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705) 
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) 
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) 
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) 
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966) 
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:650) 
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 
    at org.bob.resume.config.BobDispatcherServletInitializer$1.doFilterInternal(BobDispatcherServletInitializer.java:126) 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85) 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) 
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
    at java.lang.Thread.run(Thread.java:619) 
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay 

    at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1803) 
    ... 49 more 
QUIT 
221 Closing connection. Good bye. 

這是和陌生的日誌,目的地電子郵件是有效的,但此日誌表明目標地址無效!!!!

DEBUG SMTP: Sending failed because of invalid destination addresses 

注意:所有電子郵件和ips都是故意更改此問題,他們是正確的。

非常感謝您的關注。

回答

2
550 5.7.1 Unable to relay 

這是說:這些地址都很好,但它不能從您在您的SMTP命令中指定的RCPT TO:提供的MAIL FROM:發送電子郵件。

在發送電子郵件之前,SMTP服務器很可能希望您登錄(使用SMTP AUTH)。

爲了做到這一點,你可以簡單地嘗試這個辦法:

Transport.send(mimeMessage, username, password); 

或者如果失敗的設置適當的SMTP AUTH:

props.put("mail.smtp.auth", "true"); 
Session mailSession = Session.getDefaultInstance(properties, new Authenticator() { 
    public PasswordAuthentication getPasswordAuthentication() { 
     //Fill in your data here. 
     return new PasswordAuthentication("user", "password"); 
    } 
}); 
+0

謝謝,這工作得很好:) –

相關問題