我一直在使用JavaMailSender,但为了在我的应用程序上发送邮件通知,我查找了Apache Common Email但是无法 实现这种邮件传递状态机制。
这是我的代码:
String smtpServer = "smtp.gmail.com";
final String userid = "xxxx";
final String password = "xxxx";
String contentType = "text/html";
String subject = "an email to a fake address"
String from = "xxxxx";
String to = "xxx";//some invalid address
String body = "Body: an email to a fake address";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", smtpServer);
props.put("mail.smtp.port", "25");
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.from", from);
Session mailSession = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userid, password);
}
});
MimeMessage message = new MimeMessage(mailSession);
message.addFrom(InternetAddress.parse(from));
message.setRecipients(Message.RecipientType.TO, to);
message.setSubject(subject);
message.setContent(body, contentType);
message.setHeader("Return-Receipt-To", "XXXXXX");
message.setHeader("Disposition-Notification-To", "xxxxxxxx");
SMTPMessage smtpMessage = new SMTPMessage(message);
smtpMessage.setNotifyOptions(SMTPMessage.NOTIFY_FAILURE|SMTPMessage.NOTIFY_SUCCESS);
Transport transport = mailSession.getTransport();
try {
System.out.println("Sending ....");
transport.connect(smtpServer, port, userid, password);
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
System.out.println("Sending done ...");
} catch (Exception e) {
System.err.println("Error Sending: ");
e.printStackTrace();
}
transport.close();
通过此代码,我可以发送收据批准通知,但我无法在我的申请中看到它。
我设法扫描邮件服务器的收件箱并在我的应用程序中显示它。
请帮助我。