我試圖從android應用程序內發送電子郵件。我已經使用gmail smtp服務器進行了功能演示,但是遷移到使用Microsoft Exchange 2013郵件服務器證明是困難的。我得到以下異常跟蹤(我匿名服務器名稱和IP地址):使用Exchange ActiveSync發送來自android應用程序的電子郵件
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和認證,同時發送較早嘗試,但無濟於事。
我很欣賞這方面的任何意見,因爲我已經徹底堅持了這個問題很長時間了。
我不是通過465連接,而443是Exchange ActiveSync的默認端口。至於你的命令建議:「openssl不被識別爲內部或外部命令」,但我可能做錯了 –