问题描述
我正在尝试从android应用程序中发送电子邮件。 我已经使用gmail smtp服务器进行了正常运行的演示,但是事实证明迁移到使用Microsoft Exchange 2013邮件服务器非常困难。 我得到以下异常跟踪(我已经匿名化了服务器名称和IP地址):
10-21 10:21:00.162 14167-14546/no.uib.jmi053.tefapplication W/System.err: javax.mail.MessagingException: Exception reading response;
10-21 10:21:00.162 14167-14546/no.uib.jmi053.tefapplication W/System.err: nested exception is:
10-21 10:21:00.162 14167-14546/no.uib.jmi053.tefapplication W/System.err: javax.net.ssl.SSLException: Read error: ssl=0xb895bf18: I/O error during system call, Connection reset by peer
10-21 10:21:00.172 14167-14546/no.uib.jmi053.tefapplication W/System.err: at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1611)
10-21 10:21:00.172 14167-14546/no.uib.jmi053.tefapplication W/System.err: at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1369)
10-21 10:21:00.172 14167-14546/no.uib.jmi053.tefapplication W/System.err: at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
10-21 10:21:00.172 14167-14546/no.uib.jmi053.tefapplication W/System.err: at javax.mail.Service.connect(Service.java:288)
10-21 10:21:00.172 14167-14546/no.uib.jmi053.tefapplication W/System.err: at no.uib.jmi053.tefapplication.io.mail.GmailSender.send(GmailSender.java:109)
10-21 10:21:00.172 14167-14546/no.uib.jmi053.tefapplication W/System.err: at no.uib.jmi053.tefapplication.io.mail.AsyncSendTask.doInBackground(AsyncSendTask.java:70)
10-21 10:21:00.172 14167-14546/no.uib.jmi053.tefapplication W/System.err: at no.uib.jmi053.tefapplication.io.mail.AsyncSendTask.doInBackground(AsyncSendTask.java:12)
10-21 10:21:00.172 14167-14546/no.uib.jmi053.tefapplication W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:288)
10-21 10:21:00.172 14167-14546/no.uib.jmi053.tefapplication W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
10-21 10:21:00.172 14167-14546/no.uib.jmi053.tefapplication W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
10-21 10:21:00.172 14167-14546/no.uib.jmi053.tefapplication W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
10-21 10:21:00.172 14167-14546/no.uib.jmi053.tefapplication W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
10-21 10:21:00.172 14167-14546/no.uib.jmi053.tefapplication W/System.err: at java.lang.Thread.run(Thread.java:818)
10-21 10:21:00.172 14167-14546/no.uib.jmi053.tefapplication W/System.err: Caused by: javax.net.ssl.SSLException: Read error: ssl=0xb895bf18: I/O error during system call, Connection reset by peer
10-21 10:21:00.172 14167-14546/no.uib.jmi053.tefapplication W/System.err: at com.android.org.conscrypt.NativeCrypto.SSL_read(Native Method)
10-21 10:21:00.172 14167-14546/no.uib.jmi053.tefapplication W/System.err: at com.android.org.conscrypt.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:728)
10-21 10:21:00.172 14167-14546/no.uib.jmi053.tefapplication W/System.err: at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:110)
10-21 10:21:00.172 14167-14546/no.uib.jmi053.tefapplication W/System.err: at java.io.InputStream.read(InputStream.java:162)
10-21 10:21:00.172 14167-14546/no.uib.jmi053.tefapplication W/System.err: at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:149)
10-21 10:21:00.172 14167-14546/no.uib.jmi053.tefapplication W/System.err: at java.io.BufferedInputStream.read(BufferedInputStream.java:234)
10-21 10:21:00.172 14167-14546/no.uib.jmi053.tefapplication W/System.err: at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:88)
10-21 10:21:00.172 14167-14546/no.uib.jmi053.tefapplication W/System.err: at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1589)
10-21 10:21:00.172 14167-14546/no.uib.jmi053.tefapplication W/System.err: ... 12 more
与客户协商后,似乎出现问题是我试图以非标准方式或服务器无法识别的方式连接到他们的服务器。 我对Microsoft Exchange不太熟悉,而且我不确定如何以可以使用的方式连接到服务器。
这是我的发送者类:
package no.uib.jmi053.tefapplication.io.mail;
import com.sun.mail.smtp.SMTPTransport;
import java.util.Date;
import java.util.Properties;
import javax.activation.CommandMap;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.activation.MailcapCommandMap;
import javax.mail.BodyPart;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
/**
* Created by JoaT Development on 02.10.2015.
*
* Handles sending an email from Gmail-client.
*/
public class GmailSender{
private String _user;
private String _pass;
private String[] _to;
private String _from;
private String _port;
private String _sport;
private String _host;
private String _subject;
private String _body;
private boolean _auth;
private boolean _debuggable;
private Multipart _multipart;
public GmailSender() {
_host = "mail.company-name.com"; // default smtp server
_port = "443"; // default smtp port
_sport = "443"; // default socketfactory port
_user = ""; // username
_pass = ""; // password
_from = ""; // email sent from
_subject = ""; // email subject
_body = ""; // email body
_debuggable = false; // debug mode on or off - default off
_auth = true; // smtp authentication - default on
_multipart = new MimeMultipart();
// There is something wrong with MailCap, javamail can not find a handler for the multipart/mixed part, so this bit needs to be added.
MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mc);
}
public GmailSender(String user, String pass) {
this();
_user = user;
_pass = pass;
}
public boolean send() throws Exception {
Properties props = _setProperties();
if(!_user.equals("") && !_pass.equals("") && _to.length > 0 && !_from.equals("") && !_subject.equals("") && !_body.equals("")) {
Session session = Session.getInstance(props, null);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(_from));
InternetAddress[] addressTo = new InternetAddress[_to.length];
for (int i = 0; i < _to.length; i++) {
addressTo[i] = new InternetAddress(_to[i]);
}
msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);
msg.setSubject(_subject);
msg.setSentDate(new Date());
// setup message body
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(_body);
_multipart.addBodyPart(messageBodyPart);
// Put parts in message
msg.setContent(_multipart);
// send email
SMTPTransport t = (SMTPTransport)session.getTransport("smtps");
t.connect(_host, _user, _pass);
t.send(msg);
t.close();
return true;
} else {
return false;
}
}
public void addAttachment(String filePath, String filename) throws Exception {
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filePath);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
_multipart.addBodyPart(messageBodyPart);
}
private Properties _setProperties() {
Properties props = new Properties();
props.put("mail.smtp.host", _host);
if(_debuggable) {
props.put("mail.debug", "true");
}
if(_auth) {
props.put("mail.smtp.auth", "true");
}
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", _host);
props.put("mail.smtp.port", _port);
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.trust", _host);
props.setProperty("mail.smtp.quitwait", "false");
return props;
}
// the getters and setter
public void setBody(String _body) {
this._body = _body;
}
public void setTo(String[] toArr) {
// TODO Auto-generated method stub
this._to=toArr;
}
public void setFrom(String string) {
// TODO Auto-generated method stub
this._from=string;
}
public void setSubject(String string) {
// TODO Auto-generated method stub
this._subject=string;
}
}
这是从AsyncTask调用的:
sender.setSubject("Reimbursement for " + userName + " " + startDate);
sender.setBody("This mail has been sent from a Travel Expense Form Application. " +
"It contains a single expense for " + userName + " " + startDate);
sender.addAttachment(formPath, userName + " " + startDate + ".xls");
} else {
sender.setSubject("Travel expense form for " + userName + ": " + startDate + " - "
+ endDateString);
sender.setBody("This mail has been sent from a Travel Expense Form Application. " +
"It contains a travel for " + userName + " with route " + travelRoute +
": " + startDate + " - " + endDateString);
sender.addAttachment(formPath, userName + ": " + startDate + " - " + endDateString +
".xls");
}
for (int i = 0; i < imagePathList.size(); i++) {
sender.addAttachment(imagePathList.get(i), findNameFromPath(imagePathList.get(i)));
}
sender.setFrom(sendAddress);
String[] recipients = {toAddress, sendAddress};
sender.setTo(recipients);
success = sender.send();
我之前曾尝试使用javax.mail.Authenticator并在发送时进行身份验证,但无济于事。
我对此表示感谢,因为我已经在相当长的一段时间内完全解决了这个问题。
1楼
抱歉,我在“答案”中发布。 没有足够的观点来评论您的问题。
默认情况下,Exchange 2013默认上次安装465时没有SMTPS侦听器,因此必须先构建它。 对于您的Microsoft Exchange连接,结果如何?
openssl s_client -starttls smtp -connect example.com:465
只要验证一下,您就可以先使用SMTPS获得正确的响应。
2楼
我有一个类似的问题,可能是网络问题或防火墙问题。
从日志中,我可以看到IP为:00.000.00.000,所以您可能没有连接。
您可以尝试使用帮助库,例如
但这看起来像是网络,而不是代码。
需要注意的另一件事是,您已允许自己的帐户访问其他邮件客户端