2012-08-28 61 views
2

我的android應用程序使用JavaMail庫發送電子郵件和附件。超過Wifi該應用程序工作得很好,但是當我從WiFi斷開並打開3G時,郵件無法發送。似乎我無法接觸到郵件服務器,即使郵件服務器在互聯網上公開,而不是在本地網絡上。從Wifi到3G的不同SMTP端口

當我連接到3G時SMTP端口是否更改?如果是這樣,這很奇怪。用於發送郵件的codesnippet是下

package Logic; 

public class SendMail extends javax.mail.Authenticator { 

    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; 
    SharedPreferences sharedPrefs; 

    public SendMail() { 

     _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 SendMail(Context c) { 
     this(); 

     sharedPrefs = PreferenceManager.getDefaultSharedPreferences(c); 
     _host = sharedPrefs.getString("host", null); 
     _port = sharedPrefs.getString("port", null); 
     _sport = sharedPrefs.getString("port", null); 
     _user = sharedPrefs.getString("mail", null); 
     _pass = sharedPrefs.getString("pw", null); 

    } 

    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, 
        new GMailAuthenticator(_user, _pass)); 

      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, henger noe her?! 
      Transport.send(msg); 

      return true; 
     } else { 
      return false; 
     } 
    } 

    public void addAttachment(String filename) throws Exception { 
     BodyPart messageBodyPart = new MimeBodyPart(); 
     DataSource source = new FileDataSource(filename); 
     messageBodyPart.setDataHandler(new DataHandler(source)); 
     messageBodyPart.setFileName(filename); 


     _multipart.addBodyPart(messageBodyPart); 
    } 

    class GMailAuthenticator extends Authenticator { 
     String user; 
     String pw; 

     public GMailAuthenticator(String username, String password) { 
      super(); 
      this.user = username; 
      this.pw = password; 
     } 

     public PasswordAuthentication getPasswordAuthentication() { 
      return new PasswordAuthentication(user, pw); 
     } 
    } 

    private Properties _setProperties() { 
     Properties props = new Properties(); 

     props.put("mail.smtp.host", _host); 
     props.put("mail.smtp.starttls.enable", "true"); 

     if (_debuggable) { 
      props.put("mail.debug", "true"); 
     } 

     if (_auth) { 
      props.put("mail.smtp.auth", "true"); 
     } 

     props.put("mail.smtp.port", _port); 
     props.put("mail.smtp.socketFactory.port", _sport); 
     props.put("mail.smtp.socketFactory.fallback", "true"); 

     return props; 
    } 

    public String getBody() { 
     return _body; 
    } 

    public void setBody(String _body) { 
     this._body = _body; 
    } 

    public void setTo(String[] toArr) { 
     this._to = toArr; 
    } 

    public void setFrom(String string) { 
     this._from = string; 
    } 

    public void setSubject(String string) { 
     this._subject = string; 
    } 
} 

是的,我已經指定了正確的權限在我的清單

<uses-permission android:name="android.permission.INTERNET"/> 
+0

您是否有任何可以提示該問題的日誌跟蹤? – fiddler

回答

1

一些ISP限制的SMTP服務器的使用,並強迫你使用自己的SMTP服務器,而不是擁有。
因此,您將收到pop.yourmailserver.com的郵件並使用smtp.yourisp.com發送郵件。

或者,他們禁止您使用來自3G的SMTP發送郵件。