早上好。我有个问题 。我想通过使用eclipse oxygen 3月201日通过Gmail发送电子邮件。我用了两个罐子:mail-1.4.7和激活1.1.1。我允许访问Gmail帐户的安全性较低。这是我的代码:
package Login_sys;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class EmailSend {
final String emailSMTPserver = "smtp.gmail.com";
final String emailServerPort = "587";
String receiverEmail = null;
String emailSubject = null;
String emailBody = null;
String from = null ;
String password = null ;
public EmailSend(String from,String password,String subject ,String message,String to)
{
this.from = from ;
this.password = password ;
this.emailSubject = subject ;
this.emailBody = message ;
this.receiverEmail = to ;
Properties props = System.getProperties();
props.put("mail.smtp.user", from);
props.put("mail.smtp.host", emailSMTPserver);
props.put("mail.smtp.port", emailServerPort);
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.password", password);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.port", emailServerPort);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
SecurityManager security = System.getSecurityManager();
try
{
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
Message msg = new MimeMessage(session);
msg.setText(emailBody);
msg.setSubject(emailSubject);
msg.setFrom(new InternetAddress(from));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress(receiverEmail));
Transport.send(msg);
System.out.println("send successfully");
} catch (Exception ex) {
System.err.println("Error occurred while sending.!");
}
}
private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from , password);
}
}}
我从其他班级称呼它:
EmailSend f = new EmailSend ("******@gmail.com","*****","Testing","testin
email","****@gmail.com");
始终打印:发送时出错。 。我不知道我的错误在哪里
答案 0 :(得分:0)
您设置了错误的端口号。将其更改为:
final String emailServerPort = "465";
Gmail SMTP端口(TLS):587
Gmail SMTP端口(SSL):465
答案 1 :(得分:0)
要修复SSLHandshakeException
,您需要更新cacerts
中的JAVA_HOME/jre/lib/security
。获取InstallCert.java
的副本。看看这里:InstallCert.java
您可以将jssecacerts
置于/lib/security
下,也可以使用KeyStore资源管理器并从jssecacerts
导出证书并将其导入cacerts
。