分类: 系统运维
2013-11-26 09:55:09
使用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("","Password02!");
for(int i=0;i<1;i++){
Session mailSession = Session.getInstance(props, auth);
Message msg = new MimeMessage(mailSession);
//发件人邮箱
msg.setFrom(new InternetAddress(""));
//收件人邮箱地址
InternetAddress[] address = null;
address = InternetAddress.parse("",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();
}
}
}
经过分析后,发现代码需要简单修改,修改后如下:
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:<>
250 2.1.0 Sender OK
RCPT TO:<>
250 2.1.5 Recipient OK
DEBUG SMTP: Verified Addresses
DEBUG SMTP:
DATA
354 Start mail input; end with
Message-ID: <>
From:
To:
Subject: =?GBK?B?suLK1NPKvP4=?=
Mime-Version: 1.0
Content-Type: text/plain; charset=GBK
Content-Transfer-Encoding: base64
suLK1NPKvP4=
.
250 2.6.0 <>[InternalId=1322968] Queued mail for delivery QUIT
其中最重要的问题是,我的邮件服务器上为了配合其他应用开启了匿名访问,然而这段程序中用的是验证身份的方式,在通过邮件服务器时,服务器认为是匿名发送,导致不能发往外网。我把代码中smtp用的端口25改为587就好使了。还有一个方法是专门为这个java发送服务器开启中继也可以,一定是只针对这个java服务器的IP而不是针对所有的中继。