我在使用公司交换服务器通过Javamail发送电子邮件时遇到了一些麻烦。我们有一个应用程序可以通过gmail服务器发送电子邮件,没有任何问题,但是对于Google政策的某些更改,我们希望使用公司服务器来完成。我肯定会话属性中的问题,但是我找不到方法使其工作]
Properties props = new Properties();
props.put("mail.smtp.port", 465);
props.put("mail.smtp.socketFactory.port", 465);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.host", _server);
session = Session.getInstance(props, this);
try {
transport = session.getTransport("smtp");
transport.connect("mail.company.com",_user,_pass);
transport.close();
这是错误,正在显示日志
javax.mail.MessagingException:无法连接到SMTP主机:mail.company.com,端口:443; 嵌套的异常是: avax.net.ssl.SSLHandshakeException:java.security.cert.CertPathValidatorException:找不到证书路径的信任锚。
您必须检查您的电子邮件提供商及其SMTP设置;服务器,端口和加密方法。
以下代码段适用于我
放置
//1) get the session object
Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
// You have missed this line.
properties.put("mail.smtp.starttls.enable", "true");
// This SMTP server works with me for all Microsoft email providers, like: -
// Outlook, Hotmail, Live, MSN, Office 365 and Exchange.
properties.put("mail.smtp.host", "smtp.live.com");
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.user", user);
properties.put("mail.smtp.pwd", password);
Session session = Session.getInstance(properties, null);
session.setDebug(true); // To trace the code implementation.
Transport transport = session.getTransport("smtp");
transport.connect("smtp.live.com", 587, user, password);
transport.close();
而不是
props.put("mail.smtp.port", 465);
props.put("mail.smtp.socketFactory.port", 465);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.host", _server);
session = Session.getInstance(props, this);
try {
transport = session.getTransport("smtp");
transport.connect("mail.company.com",_user,_pass);
transport.close();
[我发现this website非常有用,它有助于获取其他电子邮件提供商的SMTP设置信息。