Java Mail 不发送电子邮件

标签 java email jakarta-mail

我正在使用 javax.mail-api 1.5.3 发送电子邮件。这是我的电子邮件发送代码:

    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:<test@mysite.com>
250 2.1.0 Sender OK
RCPT TO:<bob123456@gmail.com>
550 5.7.1 Unable to relay
DEBUG SMTP: Invalid Addresses
DEBUG SMTP:   bob123456@gmail.com
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 都是有意为这个问题更改的,它们是正确的。

提前感谢您的关注。

最佳答案

 550 5.7.1 Unable to relay

这表示:地址没问题,但不允许从您提供的 MAIL FROM: 发送电子邮件到 SMTP 命令中指定的 RCPT TO:

很可能 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");
    }
});

关于Java Mail 不发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35892704/

相关文章:

java - 以编程方式展开子 JMenuItems

java - 将 CSV 文件转换为 Java - 向后复制

javascript - HTML 消息发送器

JavaMail 和电子邮件帐户的连接

java - 使用手动导入的jar文件如何创建Spring Web项目?

java - pom.xml 输入不包含数据@

java - Java 中的国际 InternedAddress

java.lang.NoClassDefFoundError : com/sun/mail/util/LineInputStream for send mail through Java 错误

php - PHP 中的 "Signed"电子邮件

email - 使用 SMTP 向多个地址发送邮件 : relaying the message to different servers?