【发布时间】:2019-07-23 10:40:07
【问题描述】:
我正在尝试发送一封包含表单数据的电子邮件。目前正在尝试使用Gmail,目标是从本地服务器发送。但这给了我一个例外。我已将.jar 添加到库中。对不起,如果这个问题很愚蠢,我是新手。
如何一步步解决?
public void sendMail(String from, String password, String recipient, FormModel formModel) {
Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls", "true");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");
Session session = Session.getInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, pass);
}
});
Message message = pmessage(session, from, recipient);
try {
Transport.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
private Message pmessage(Session session, String myaccount, String recipient) {
Message message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(myaccount));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
message.setSubject("formular");
message.setText("TEST");
return message;
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
return null;
}
【问题讨论】:
-
1) 粘贴异常 2) 你添加了什么 jar 3) 如果它是正确的 jar,它可能不在类路径上。
-
@Antoniossss thx 我对问题进行了更改
-
所以它不在类路径上。如何更改取决于您运行应用程序的方式。
-
@Antoniossss 我正在使用 intellij idea 2019,我正在使用 (Shift+F10) 运行
标签: java jakarta-ee jakarta-mail