2015-10-19 107 views
0

我试图从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和认证,同时发送较早尝试,但无济于事。

我很欣赏这方面的任何意见,因为我已经彻底坚持了这个问题很长时间了。

回答

0

对不起,我在“答复”中发帖。没有足够的观点来评论你的问题。

默认情况下,Exchange 2013默认没有上次安装它时465上的SMTPS侦听器,它必须先建好。这与您的Microsoft Exchange连接有什么关系?

openssl s_client -starttls smtp -connect example.com:465 

只是验证您可以先使用SMTPS得到正确的响应。

+0

我不是通过465连接,而443是Exchange ActiveSync的默认端口。至于你的命令建议:“openssl不被识别为内部或外部命令”,但我可能做错了 –

0

我有一个类似的问题,它可能是一个网络问题或防火墙问题。

从日志我可以看到,IP是:00.000.00.000所以可能你没有连接。

您可以尝试使用帮助库像BackgroundMailLibrary

但它看起来像网络,而不是代码。

注意到另一件事是,你允许你的帐户访问其他邮件客户端

+0

感谢您的输入。正如我所说,IP地址在邮件中是匿名的,我确实从服务器获得响应。 –

相关问题