如果我单击按钮,我希望能够将邮件发送给给定的收件人。我为此编写了一个MailBean类,但是它似乎无法正常工作,因为我从未收到过发送到我的电子邮件地址的邮件。我用调试器测试了我的代码,一切似乎都很好,除了我什么都没收到,甚至在垃圾邮件文件夹中也没收到。
我以outlook.com
作为主持人。
这是我的课程。
public class MailBean {
private final String USERNAME = "htlplus@outlook.com"; //the sender
private final String PASSWORD = "*******";
public Object sendMessage() {
Properties prop = new Properties();
prop.put("mail.smtp.auth", true);
prop.put("mail.smtp.starttls.enable", "true");
prop.put("mail.smtp.host", "smtp-mail.outlook.com");
prop.put("mail.smtp.port", "587");
Session session = Session.getInstance(prop, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(USERNAME, PASSWORD);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("htlplus@outlook.com"));
message.setRecipients(
Message.RecipientType.TO, InternetAddress.parse("andor98@gmail.com"));
message.setSubject("Mail Subject");
String msg = "This is my first email using JavaMailer";
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setContent(msg, "text/html");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(mimeBodyPart);
message.setContent(multipart);
Transport.send(message);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}