while sending mail I am getting this error
在发送邮件时,我收到了这个错误
java.lang.RuntimeException: javax.mail.SendFailedException: Sending failed; nested exception is: class javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1
. lang。RuntimeException:javax.mail。SendFailedException:发送失败;嵌套异常是:类javax.mail。无法连接SMTP主机:SMTP .gmail.com,端口:465,响应:-1。
my code is:
我的代码是:
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("email","password");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("email"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(this.to));
message.setSubject("Testing");
message.setText("Hey, this is the testing email.");
Transport.send(message);
Any help would be appreciated.
如有任何帮助,我们将不胜感激。
Thanks in Advance.
提前谢谢。
4 个解决方案
#1
12
You need to tell it that you are using SSL:
你需要告诉它你正在使用SSL:
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
In case you miss anything, here is working code:
如果您漏掉了什么,这里是工作代码:
String d_email = "address@gmail.com",
d_uname = "Name",
d_password = "urpassword",
d_host = "smtp.gmail.com",
d_port = "465",
m_to = "toAddress@gmail.com",
m_subject = "Indoors Readable File: " + params[0].getName(),
m_text = "This message is from Indoor Positioning App. Required file(s) are attached.";
Properties props = new Properties();
props.put("mail.smtp.user", d_email);
props.put("mail.smtp.host", d_host);
props.put("mail.smtp.port", d_port);
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.port", d_port);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
SMTPAuthenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
session.setDebug(true);
MimeMessage msg = new MimeMessage(session);
try {
msg.setSubject(m_subject);
msg.setFrom(new InternetAddress(d_email));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));
Transport transport = session.getTransport("smtps");
transport.connect(d_host, Integer.valueOf(d_port), d_uname, d_password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
} catch (AddressException e) {
e.printStackTrace();
return false;
} catch (MessagingException e) {
e.printStackTrace();
return false;
}
#2
6
I have been experiencing this issue when debugging using NetBeans, even executing the actual jar file. Antivirus would block sending email. You should temporarily disable your antivirus during debugging or exclude NetBeans and the actual jar file from being scanned. In my case, I'm using Avast.
我在使用NetBeans调试时遇到过这个问题,甚至在执行实际的jar文件时也是如此。杀毒软件会阻止发送电子邮件。您应该在调试期间暂时禁用杀毒程序,或者排除NetBeans和实际的jar文件。我用的是Avast。
See this link on how to Exclude : How to Add File/Website Exception into avast! Antivirus 2014
查看这个如何排除的链接:如何添加文件/网站异常到avast!杀毒软件2014
It works for me.
它适合我。
#3
1
What i did was i commented out the
我所做的就是把这个注释掉
props.put("mail.smtp.starttls.enable","true");
Because apparently for G-mail you did not need it. Then if you haven't already done this you need to create an app password in G-mail for your program. I did that and it worked perfectly. Here this link will show you how: https://support.google.com/accounts/answer/185833.
因为显然你不需要G-mail。如果你还没有这样做,你需要在G-mail中为你的程序创建一个应用程序密码。我这样做了,效果很好。在这里,这个链接将向您展示:https://support.google.com/accounts/answer/185833。
#4
0
Port 465 is for "smtp over SSL".
端口465表示“通过SSL的smtp”。
http://javamail.kenai.com/nonav/javadocs/com/sun/mail/smtp/package-summary.html
http://javamail.kenai.com/nonav/javadocs/com/sun/mail/smtp/package-summary.html
[...] For example, use
props.put("mail.smtp.port", "888");
to set the mail.smtp.port property, which is of type int.
Note that if you're using the "smtps" protocol to access SMTP over SSL,
all the properties would be named "mail.smtps.*"
#1
12
You need to tell it that you are using SSL:
你需要告诉它你正在使用SSL:
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
In case you miss anything, here is working code:
如果您漏掉了什么,这里是工作代码:
String d_email = "address@gmail.com",
d_uname = "Name",
d_password = "urpassword",
d_host = "smtp.gmail.com",
d_port = "465",
m_to = "toAddress@gmail.com",
m_subject = "Indoors Readable File: " + params[0].getName(),
m_text = "This message is from Indoor Positioning App. Required file(s) are attached.";
Properties props = new Properties();
props.put("mail.smtp.user", d_email);
props.put("mail.smtp.host", d_host);
props.put("mail.smtp.port", d_port);
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.port", d_port);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
SMTPAuthenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
session.setDebug(true);
MimeMessage msg = new MimeMessage(session);
try {
msg.setSubject(m_subject);
msg.setFrom(new InternetAddress(d_email));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));
Transport transport = session.getTransport("smtps");
transport.connect(d_host, Integer.valueOf(d_port), d_uname, d_password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
} catch (AddressException e) {
e.printStackTrace();
return false;
} catch (MessagingException e) {
e.printStackTrace();
return false;
}
#2
6
I have been experiencing this issue when debugging using NetBeans, even executing the actual jar file. Antivirus would block sending email. You should temporarily disable your antivirus during debugging or exclude NetBeans and the actual jar file from being scanned. In my case, I'm using Avast.
我在使用NetBeans调试时遇到过这个问题,甚至在执行实际的jar文件时也是如此。杀毒软件会阻止发送电子邮件。您应该在调试期间暂时禁用杀毒程序,或者排除NetBeans和实际的jar文件。我用的是Avast。
See this link on how to Exclude : How to Add File/Website Exception into avast! Antivirus 2014
查看这个如何排除的链接:如何添加文件/网站异常到avast!杀毒软件2014
It works for me.
它适合我。
#3
1
What i did was i commented out the
我所做的就是把这个注释掉
props.put("mail.smtp.starttls.enable","true");
Because apparently for G-mail you did not need it. Then if you haven't already done this you need to create an app password in G-mail for your program. I did that and it worked perfectly. Here this link will show you how: https://support.google.com/accounts/answer/185833.
因为显然你不需要G-mail。如果你还没有这样做,你需要在G-mail中为你的程序创建一个应用程序密码。我这样做了,效果很好。在这里,这个链接将向您展示:https://support.google.com/accounts/answer/185833。
#4
0
Port 465 is for "smtp over SSL".
端口465表示“通过SSL的smtp”。
http://javamail.kenai.com/nonav/javadocs/com/sun/mail/smtp/package-summary.html
http://javamail.kenai.com/nonav/javadocs/com/sun/mail/smtp/package-summary.html
[...] For example, use
props.put("mail.smtp.port", "888");
to set the mail.smtp.port property, which is of type int.
Note that if you're using the "smtps" protocol to access SMTP over SSL,
all the properties would be named "mail.smtps.*"