【问题标题】:javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587 Can not manage to solve this problemjavax.mail.MessagingException:无法连接到 SMTP 主机:smtp.gmail.com,端口:587 无法解决此问题
【发布时间】:2019-01-21 00:22:59
【问题描述】:

我试图在没有意图的情况下在 Android Studio 中发送电子邮件,为了实现这一点,我正在尝试使用 JavaMail API。但是每次我尝试发送电子邮件时都会出错:

javax.mail.MessagingException:无法连接到 SMTP 主机:smtp.gmail.com,端口:587

我已经在我的项目中实现了activation.jar、additional.jar 和mail.jar。此外,我在 INTERNET 的清单文件中给予了许可。我还允许对我的 Gmail 帐户进行较低安全性的访问。

这是我的代码:

公共类 MailSend 扩展 AsyncTask {

//Declaring Variables
private Context context;
private Session session;

//Information to send email
private String email;
private String subject;
private String message;

//Progressdialog to show while sending email
private ProgressDialog progressDialog;

//Class Constructor
public MailSend(Context context, String email, String subject, String message) {
    //Initializing variables
    this.context = context;
    this.email = email;
    this.subject = subject;
    this.message = message;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    //Showing progress dialog while sending email
    progressDialog = ProgressDialog.show(context, "Sending message", "Please wait...", false, false);
}

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    //Dismissing the progress dialog
    progressDialog.dismiss();
    //Showing a success message
    Toast.makeText(context, "Message Sent", Toast.LENGTH_LONG).show();
}

@Override
protected Void doInBackground(Void... params) {
    final String username = "user";
    final String password = "pass";

    Properties props = System.getProperties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");

    //Creating a new session
    session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
                //Authenticating the password
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("user", "pass");
                }
            });

    try {
        //Creating MimeMessage object
        MimeMessage mm = new MimeMessage(session);

        //Setting sender address
        mm.setFrom(new InternetAddress("user"));
        //Adding receiver
        mm.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
        //Adding subject
        mm.setSubject(subject);
        //Adding message
        mm.setText(message);

        //Sending email
        Transport.send(mm);

    } catch (MessagingException e) {
        e.printStackTrace();
    }

    return null;
}

}

谁能告诉我我的代码是错误的还是我遗漏了什么?这是完整的错误输出。

W/System.err: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
                nested exception is:
W/System.err: 	java.net.ConnectException: failed to connect to smtp.gmail.com/64.233.184.108 (port 465) from /:: (port 56438): connect failed: ETIMEDOUT (Connection timed out)
W/System.err:     at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391)
W/System.err:     at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
W/System.err:     at javax.mail.Service.connect(Service.java:310)
                  at javax.mail.Service.connect(Service.java:169)
W/System.err:     at javax.mail.Service.connect(Service.java:118)
W/System.err:     at javax.mail.Transport.send0(Transport.java:188)
W/System.err:     at javax.mail.Transport.send(Transport.java:118)
                  at com.example.meric.knockknockapp.MailSend.doInBackground(MailSend.java:105)
                  at com.example.meric.knockknockapp.MailSend.doInBackground(MailSend.java:31)
W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:333)
W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
                  at java.lang.Thread.run(Thread.java:764)
W/System.err: Caused by: java.net.ConnectException: failed to connect to smtp.gmail.com/64.233.184.108 (port 465) from /:: (port 56438): connect failed: ETIMEDOUT (Connection timed out)
                  at libcore.io.IoBridge.connect(IoBridge.java:138)
W/System.err:     at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:129)
                  at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:356)
                  at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
                  at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
W/System.err:     at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
                  at java.net.Socket.connect(Socket.java:616)
                  at java.net.Socket.connect(Socket.java:565)
W/System.err:     at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:233)
                  at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
W/System.err:     at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1359)
              	... 14 more
W/System.err: Caused by: android.system.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
W/System.err:     at libcore.io.Linux.connect(Native Method)
                  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:126)
W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:152)
                  at libcore.io.IoBridge.connect(IoBridge.java:130)

【问题讨论】:

  • 请向我们展示完整的堆栈跟踪
  • 更新了帖子。

标签: java android


【解决方案1】:

将属性更改为:-

props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465")

如果没用

查看此链接:-Could not connect to SMTP host: smtp.gmail.com, port: 587; nested exception is: java.net.ConnectException: Connection timed out: connect

希望这会有所帮助!!!!

【讨论】:

    【解决方案2】:

    此代码适用于我:-

    public class SendMail extends AsyncTask<Void, Void, Void> {
    
    //Declaring Variables
    private Context context;
    private Session session;
    //Information to send email
    private String email="", femail="", fpass="";
    private String subject="";
    private String message="";
    
    //Class Constructor
    public SendMail(Context context, String fromemail, String frompass, String toemail, String subject, String message) {
        //Initializing variables
        this.context = context;
        this.email = toemail;
        this.femail = fromemail;
        this.fpass = frompass;
        this.subject = subject;
        this.message = message;
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
    
    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        //Showing a success message
        Toast.makeText(context, "Send to "+email+" from "+femail+".", Toast.LENGTH_LONG).show();
    }
    
    @Override
    protected Void doInBackground(Void... params) {
        //Creating properties
        Properties props = new Properties();
    
        //Configuring properties for gmail
        //If you are not using gmail you may need to change the values
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
        //Creating a new session
        session = Session.getDefaultInstance(props,
                new javax.mail.Authenticator() {
                    //Authenticating the password
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(femail, fpass);
                    }
                });
        try {
            //Creating MimeMessage object
            MimeMessage mm = new MimeMessage(session);
            //Setting sender address
            mm.setFrom(new InternetAddress(femail));
            //Adding receiver
            mm.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
            //Adding subject
            mm.setSubject(subject);
            //Adding message
            mm.setText(message);           
            //Sending email
            Transport.send(mm);
    
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return null;
    }
    

    }

    【讨论】:

      【解决方案3】:

      我在上面发布的代码完全可以正常工作。问题是我在公共网络上,这似乎是问题的原因。当我切换到专用网络时,问题就消失了。 谢谢大家。

      【讨论】:

        猜你喜欢
        • 2013-08-21
        • 2020-12-26
        • 2011-11-01
        • 2014-03-08
        • 2017-10-07
        • 2020-04-23
        • 1970-01-01
        • 1970-01-01
        • 2016-07-29
        相关资源
        最近更新 更多