使用java經過Exchange服務器發郵件時,在內網能夠正常發送,可是不能發往互聯網郵箱,錯誤代碼以下:java
嚴重: 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!");
else
t.connect();
t.sendMessage(msg, msg.getAllRecipients());
} finally {
t.close();
}
}
}
通過分析後,發現代碼須要簡單修改,修改後以下:apache
DEBUG: setDebug: JavaMail version 1.3.1tomcat
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: 587app
EHLO cbhb-4bcb3aa917jsp
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-BINARYMIMEide
250 CHUNKINGspa
DEBUG SMTP: Found extension"SIZE", arg "10485760".net
DEBUG SMTP: Found extension"PIPELINING", arg ""3d
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
DATA
354 Start mail input; end with<CRLF>.<CRLF>
Message-ID: <17514905.1385370503062.JavaMail.a@cbhb-4bcb3aa917>
From: han.huang@cbhb.com.cn
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而不是針對全部的中繼。