当前位置: 首页 > 知识库问答 >
问题:

javax.mail.MessagingException:无法连接到SMTP主机:SMTP.gmail.com,端口:587无法解决此问题

姬寂离
2023-03-14
//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)

共有1个答案

包沈义
2023-03-14

将属性更改为:-

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")

如果不起作用

检查此链接:-无法连接到SMTP主机:SMTP.gmail.com,端口:587;嵌套异常为:java.net.ConnectException:Connection超时:connect

 类似资料: