java API包“mail”或调用“Send()”未找到异常
我为发送邮件编写以下代码。(java maven web应用程序)
公共类邮件操作扩展了ActionSupport{
private String mailId;
public String getMailId() {
return mailId;
}
public void setMailId(String mailId) {
this.mailId = mailId;
}
final String username = "my email";
final String password = "my password";
public String execute() throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse servletResponse;
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("my email"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(request.getParameter("mailId")));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler,"
+ "\n\n No spam to my email, please!");
System.out.println(request.getParameter("mailId"));
System.out.println(message);
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
return SUCCESS;
}
当我运行该程序时,未发现API包“mail”或调用“Send()”异常。 请帮我解决这个问题
# 1 楼答案
该问题与“运输”有关,请参见所附代码