//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:Connection超时:connect
这是我用来发送电子邮件的代码: javax.mail.MessagingException:无法连接到SMTP主机:SMTP.gmail.com,端口:465; 嵌套异常为: java.net.connectException:连接被拒绝 在com.sun.mail.SMTP.smtpransport.openserver(smtpransport.java:1961) 在com.sun.mail
问题内容: 以下是我发送邮件的代码: 但是当我运行代码时,它给了我以下异常。 当我ping到它时,它给我的答复没有任何问题。我在用 请帮我解决这个问题。 问题答案: 这是这两行让我感到困扰的问题: 并添加了这一行: 删除并添加以上代码行后,它可以正常工作。
我是一个初学者,如果你能让我知道,我将不胜感激。
在发送邮件时,我遇到了这个错误 java.lang.RuntimeException:javax.mail.SendFailedException:发送失败;嵌套异常为:类javax.mail.MessagingException:无法连接到SMTP主机:SMTP.gmail.com,端口:465,响应:-1 我的代码是:
下面是应用程序的代码。我一直在尝试使用eclipse IDE来运行它。我还添加了所有必需的java邮件jar文件,即。但会出现以下错误。 没有防火墙阻止访问,因为ping smtp.gmail.com时会收到回复。我甚至这样试过: null
我正在获取javax.mail.MessagingException:无法连接到SMTP主机:主机名端口:25响应:552 有时邮件发送成功。但有时我会遇到例外。 我不清楚为什么会发生这种情况。如果有什么事情出了问题,那么就不应该发送邮件。但例外情况有时还是会发生。 javax.mail.messagingException:无法连接到SMTP主机:mail.mydomain.com,端口:25,