使用javamail连接发送电子邮件失败:ETIMEDOUT(连接超时)

时间:2016-12-22 18:50:58

标签: android email connection javamail

好的,我在这里遇到问题。当我从Android使用3G / 4G网络时,在我的应用程序的后台发送电子邮件工作完全正常但是一旦我将它连接到wifi它就不再有效并且给出连接超时错误。没有一个端口在android端工作,我已经使用telnet通过同一个wifi网络从我的笔记本电脑使用端口25进行连接。不知道这里的问题是什么。

javax.mail.MessagingException: Could not connect to SMTP host:      
smtp.gmail.com, port: 25;
12-22 13:31:11.861 5380-6822/com.apksrc.loot W/System.err:   nested 
exception is:
12-22 13:31:11.861 5380-6822/com.apksrc.loot W/System.err:   
java.net.ConnectException: failed to connect to   
smtp.gmail.com/2607:f8b0:4002:c0c::6c (port 25): connect failed: ETIMEDOUT  
(Connection timed out)
12-22 13:31:11.863 5380-6822/com.apksrc.loot W/System.err:     at com

我发送电子邮件的代码是:

     @Override
    protected Void doInBackground(Void... params) {

    Properties props = new Properties();

    props.put("mail.smtp.host","smtp.gmail.com");

    props.put("mail.smtp.starttls.enable","true");
    props.put("mail.smtp.ssl.enable","true");
    props.put("mail.smtp.auth","true");
    props.put("mail.smtp.port","25");

    session = Session.getInstance(props, new GMailAuthenticator(Config.EMAIL, Config.PASSWORD));
    session.setDebug(true);
    try{
        MimeMessage mm = new MimeMessage(session);
        mm.setFrom(new InternetAddress(Config.EMAIL));
        mm.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
        mm.setSubject(subject);
        mm.setText(message);



        Transport.send(mm);
    }catch (MessagingException e){
        e.printStackTrace();
    }

    return null;
}

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);
    }
}

0 个答案:

没有答案