我为发送邮件编写了以下代码。(java maven web application)。
公共类MailAction扩展了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()”异常。 请帮我解决这个问题。
答案 0 :(得分:0)
问题在于“传输”,请参阅附加代码
Transport transport = session.getTransport("smtp");
transport.connect("smtp.gmail.com", 465, "username", "password");
transport.sendMessage(message, message.getAllRecipients());
transport.close();