解决使用JavaMail发送邮件报错javax.mail.MessagingException: 500 Error: bad syntax


     今天用Java做发邮件功能是发现了一个问题。本地代码和jar包和线上的代码和jar包是保持一致的,但是本地发邮件就可以发送成功,但是在线上发邮件时就报错了,报错信息如下:

javax.mail.MessagingException: 500 Error: bad syntax

        at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1363)
        at com.sun.mail.smtp.SMTPTransport.helo(SMTPTransport.java:838)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:375)
        at javax.mail.Service.connect(Service.java:297)
        at javax.mail.Service.connect(Service.java:156)
        at javax.mail.Service.connect(Service.java:105)
        at javax.mail.Transport.send0(Transport.java:168)
        at javax.mail.Transport.send(Transport.java:98)
        at com.tower.common.util.mybatispagehelper.Mail.sendMail(Mail.java:219)
        at com.tower.common.util.mybatispagehelper.Mail.sendMail(Mail.java:39)
        at com.tower.choosesite.action.ChoosesiteTaskAction.assignBatch(ChoosesiteTaskAction.java:320)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        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:967)
        at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:869)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
        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:240)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
        at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1502)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1458)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Thread.java:745)

    解决办法:

        使用CRT工具连接Linux服务器,输入命令查看服务器名称,路径为(etc/sysconfig/network)如下: 

    [root@template sysconfig]# cat network
    templateNETWORKING=yes
    HOSTNAME=template

   我这台服务器名称为: template

        服务器名称查到之后需要把服务器名称添加到etc/hosts配置文件中,路径为(etc/hosts),如下:

        未添加服务器名称配置文件内容为:

    [root@template etc]# cat hosts
    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

        在127.0.0.1那一行中并没有服务器名称,使用vi hosts命令将服务器名称添加进去,如下:

    [root@template etc]# vi hosts
    127.0.0.1   template  localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

         在进入vi命令之后,按一下 shift+i,就可以编辑了,编辑好之后按 esc再按 shift+: 输入wq! 就保存并退出了,可以是用cat命令在查看一下,如下:

    [root@template etc]# cat hosts
    127.0.0.1   template  localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

         配置文件改好之后就差不多了,下下来就需要让这个配置文件生效。

         使配置文件生效的方式有两种:

             第一种:还是使用命令  source/hosts 或者 ./hosts  

             第二种:将服务器重启,命令是 reboot。但是这种方式不推荐使用。


   至此就大功告成了,可以使用 hostname -i 命令查看一下,如果出现 127.0.0.1那么就没有问题了。再次尝试在线上发送邮件,就发送成功了。


   如果邮件发送失败的原因是:

javax.mail.NoSuchProviderException: Unable to locate provider for protocol: smtp
	at javax.mail.Session.getProvider(Session.java:237)
	at javax.mail.Session.getTransport(Session.java:346)
	at javax.mail.Session.getTransport(Session.java:376)
	at javax.mail.Transport.send(Transport.java:67)
	at javax.mail.Transport.send(Transport.java:48)
	at com.tower.common.util.mybatispagehelper.Mail.sendMail(Mail.java:219)
	at com.tower.common.util.mybatispagehelper.Mail.sendMail(Mail.java:39)
	at com.tower.common.util.mybatispagehelper.Mail.main(Mail.java:48)

    有可能是发件人邮箱没有开通下图的服务,需要在邮箱设置中开通。

   

    如果是向QQ邮箱发送邮件,没有保存,QQ邮箱也没有收到一定是进入QQ邮箱的垃圾箱了。

    目前发现QQ邮箱会放入垃圾箱,其他邮箱是正常的。



  • 1
    点赞
  • 0
    评论
  • 1
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值