使用java通过Exchange服务器发邮件时,在内网可以正常发送,但是不能发往互联网邮箱,错误代码如下:

严重: Servlet.service() for servlet jspthrew exception
com.sun.mail.smtp.SMTPSendFailedException: 501 5.1.7 Invalid address
       atcom.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1333)
       at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:906)
       atcom.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:535)
       attest.jmail.ExchangeMail.send(ExchangeMail.java:72)
       atorg.apache.jsp.index_jsp._jspService(index_jsp.java:67)
       atorg.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
       atjavax.servlet.http.HttpServlet.service(HttpServlet.java:820)
       atorg.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
       atorg.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
       atorg.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
       atjavax.servlet.http.HttpServlet.service(HttpServlet.java:820)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
       atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
       atorg.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
       atorg.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
       atorg.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
       atorg.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
       atorg.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
       atorg.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
       at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
       atorg.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
       atorg.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
       at java.lang.Thread.run(Thread.java:619)



程序代码如下:

public void addMail() throws AddressException, MessagingException{
              Properties props = System.getProperties();
              props.put("mail.smtp.host", "10.2.32.145");
              props.put("mail.transport.protocol", "smtp");
          props.put("mail.smtp.auth", "true");  
          Email_AutherticatorBean auth = new Email_AutherticatorBean("zhq.hao@cbhb.com.cn","Password02!");
              for(int i=0;i<1;i++){
                      Session mailSession = Session.getInstance(props, auth);
                      Message msg =  new MimeMessage(mailSession);
                      //发件人邮箱
                      msg.setFrom(new InternetAddress("han.huang@cbhb.com.cn"));
                      //收件人邮箱地址
                      InternetAddress[] address = null;
                      address = InternetAddress.parse("test_bohai@163.com",false);
                      msg.setRecipients( Message.RecipientType.TO, address);
                      String temp = "内容内容";
                      msg.setSubject("主题haha"+(i+1));                              
                      msg.setSentDate(newDate());                          
                      msg.setText(temp);                                                    
//                     Transport.send(msg);
                      SMTPTransport t = (SMTPTransport) mailSession.getTransport("smtp");
                      try {
                              if (true)
                                      t.connect("10.2.32.145", "cbhb/zhq.hao","Password02!");
                                      t.connect();
                              t.sendMessage(msg, msg.getAllRecipients());
                      } finally {
                              t.close();


经过分析后,发现代码需要简单修改,修改后如下:

DEBUG: setDebug: JavaMail version 1.3.1
DEBUG: getProvider() returningjavax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,SunMicrosystems, Inc] DEBUG SMTP: useEhlo true, useAuth true DEBUG SMTP: useEhlotrue, useAuth true DEBUG SMTP: trying to connect to host "10.2.32.145",port 587
220 BHDCPEXGAP03.cbhb.root.cbhbank.netMicrosoft ESMTP MAIL Service ready at Mon, 25 Nov 2013 17:10:15 +0800 DEBUGSMTP: connected to host "10.2.32.145", port: 587
EHLO cbhb-4bcb3aa917
250-BHDCPEXGAP03.cbhb.root.cbhbank.netHello [10.16.73.99] 250-SIZE 10485760 250-PIPELINING 250-DSN250-ENHANCEDSTATUSCODES 250-AUTH GSSAPI NTLM LOGIN 250-8BITMIME 250-BINARYMIME
250 CHUNKING
DEBUG SMTP: Found extension"SIZE", arg "10485760"
DEBUG SMTP: Found extension"PIPELINING", arg ""
DEBUG SMTP: Found extension "DSN",arg ""
DEBUG SMTP: Found extension"ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension"AUTH", arg "GSSAPI NTLM LOGIN"
DEBUG SMTP: Found extension"8BITMIME", arg ""
DEBUG SMTP: Found extension"BINARYMIME", arg ""
DEBUG SMTP: Found extension "CHUNKING",arg ""
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
334 VXNlcm5hbWU6
aGFuLmh1YW5n
334 UGFzc3dvcmQ6
UGFzc3dvcmQwMyE=
235 2.7.0 Authentication successful
DEBUG SMTP: use8bit false
MAIL FROM:<han.huang@cbhb.com.cn>
250 2.1.0 Sender OK
RCPT TO:<eidolond@gmail.com>
250 2.1.5 Recipient OK
DEBUG SMTP: Verified Addresses
DEBUG SMTP:eidolond@gmail.com
354 Start mail input; end with.
Message-ID: <17514905.1385370503062.JavaMail.a@cbhb-4bcb3aa917>
From: han.huang@cbhb.com.cn
To: eidolond@gmail.com
Subject: =?GBK?B?suLK1NPKvP4=?=
Mime-Version: 1.0
Content-Type: text/plain; charset=GBK
Content-Transfer-Encoding: base64
suLK1NPKvP4=
250 2.6.0 <17514905.1385370503062.JavaMail.a@cbhb-4bcb3aa917>[InternalId=1322968] Queued mail for delivery QUIT

其中最重要的问题是,我的邮件服务器上为了配合其他应用开启了匿名访问,然而这段程序中用的是验证身份的方式,在通过邮件服务器时,服务器认为是匿名发送,导致不能发往外网。我把代码中smtp用的端口25改为587就好使了。还有一个方法是专门为这个java发送服务器开启中继也可以,一定是只针对这个java服务器的IP而不是针对所有的中继。

http://blog.chinaunix.net/uid-7528962-id-4010054.html

使用java mail 发送邮件报错处理com.sun.mail.smtp.SMTPSendFailedException: 501 Mail from address must be same as
意思是:com.sun.mail.smtp.SMTPSendFailedException:来自地址的501邮件必须与授权用户相同 在这个项目里,邮件服务是腾讯企业邮提供的,不支持高权限的用户使用不用的用户作为发件人。所以使用统一的系统账号进行作为发件人。 转载于:https://www.cnblogs.com/jiml/p/9357048.html...
com.sun.mail.smtp.SMTPSenderFailedException: 501 mail from address must be same as authorization user at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.jav... org.springframework.mail.MailSendException: Failed messages: com.sun.mail.smtp.SMTPSendFailedException: 553 Envolope sender mismatch with login user… nested exception is: com.sun.mail.smtp.SMTPSenderFailedException: 553 Envolope sender mismatch with
最新版FOXMAIL7.1设置邮件账号(EXCHANGE 2013)时请注意,账号一定要加上后缀,如不加上将会发不了邮件.提示信息是:FROM错误 错误信息:501 5.1.7 Invalid address注意:第一次配置时一定要打全,否则无法更改. 转载于:https://blog.51cto.com/28917/1293757...
今天做邮箱验证的时候,遇到一个比较奇怪的问题: javax.mail.SendFailedException: Invalid Addresses;   nested exception is: com.sun.mail.smtp.SMTPAddressFailedException: 550 Invalid User: "13540619068@163.com" at com.s
javax.mail.SendFailedException: Invalid Addresses; nested exception is: class com.sun.mail.smtp.SMTPAddressFailedException: 550 5.1.1 Mailbox <null> does not exist nested excepti...
idea中报错No compiler is provided in this environment. Perhaps you are runningon a JRE woowahahahahaha: 在cmd命令行里执行mvn clean install安装第三方jar包的时候,遇到的这个问题: "其中一种情况是maven找不到javac命令" 调好javac环境变量就好了!!! idea中报错No compiler is provided in this environment. Perhaps you are runningon a JRE PUdd: 谢谢 有用表情包 浏览器 net::ERR_CONTENT_LENGTH_MISMATCH 206 (Partial Content) 报错解决方案 umbrella_show: 我是第一种情况,成功解决了我的问题。谢谢博主的总结分享!表情包 IDEA 一直自动加载indexing的解决方法 good study day hard: 这种根本就不好用