![](/img/trans.png)
[英]javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587
[英]javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587 Can not manage to solve this problem
我正在尝试在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)
将属性更改为此:-
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:连接超时:connect
希望这会有所帮助!!!
该代码对我有用:
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;
}
}
我上面发布的代码完全可以正常工作。 问题是我在公共网络上,这似乎是导致问题的原因。 当我切换到专用网络时,问题就消失了。 感谢大家。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.