[英]Sending email from java
I am trying to send confirmation email to users of my web application, but i am getting the following error: 我正在尝试向我的Web应用程序的用户发送确认电子邮件,但出现以下错误:
PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到到请求目标的有效认证路径
This is my java class: 这是我的java类:
package it.bcsoft.onlinestore.mail;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
public class EmailSender {
public static boolean sendEmail(String from, String pass, String message, String[] to)
{
String host="smtp.gmail.com";
Properties props= System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", 25);
props.put("mail.smtp.auth", "true");
Session session= Session.getDefaultInstance(props, null);
MimeMessage mime= new MimeMessage(session);
try {
mime.setFrom(new InternetAddress(from));
InternetAddress[] toAddress= new InternetAddress[to.length];
for(int i=0; i<to.length; i++)
{
toAddress[i]= new InternetAddress(to[i]);
}
for(int i=0; i<toAddress.length;i++)
{
mime.addRecipient(RecipientType.TO, toAddress[i]);
}
mime.setSubject("Mail from onlinestore");
mime.setText(message);
Transport transport= session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(mime, mime.getAllRecipients());
transport.close();
return true;
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
}
I do not have any SMTP Server installed on my PC and i am using java 1.7 我的PC上未安装任何SMTP服务器,并且使用的是Java 1.7
Make sure you are using the right port. 确保使用正确的端口。 To use Gmail's SMTP server via TLS, you have to use port
587
. 要通过TLS使用Gmail的SMTP服务器,您必须使用端口
587
。
Have a look at this blog post on how to build up a SMTP connection to Gmail. 查看有关如何建立与Gmail的SMTP连接的博客文章 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.