【发布时间】:2014-09-13 17:03:26
【问题描述】:
我想用 JavaMail 发送一封电子邮件,但每次我都收到异常。我创建了一个 JFrame 项目,并为按钮创建了一个 actionevent:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Properties props = new Properties();
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");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("xyz@gmail.com", "###");
}
}
);
//I've tried also this version of the Session:
//Session session = Session.getInstance(props,new NewEmpty("xyz@gmail.com","###"));
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("xyz@gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("zyx@gmail.com"));
message.setSubject("Hi");
message.setText("Helló!");
Transport.send(message);
JOptionPane.showMessageDialog(null, "Sent!");
}catch(Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
}
例外是:
【问题讨论】:
-
如果我理解的话。您是否已经在服务器中安装了 .exe?问候
-
什么意思?什么.exe?不,我想...我有一台华硕笔记本电脑,并安装了 NetBeans。我为项目 CLASSPATH 添加了 JavaMail,但没有工作。
-
DavidPostill - 尝试过,但不起作用。 + 我正在尝试使用 SSL 创建它。
标签: java smtp gmail jakarta-mail