リマインダーを顧客に送信する必要がある Web アプリを開発しています。同じためにJavaMailを使用していますが、無効な受信者の電子メールアドレス(存在しない電子メールID)を提供すると、プログラムはスローしません SendFailedException
. しかし、送信者の電子メール ID の受信トレイには、メールの送信に失敗したという配信レポートが含まれています。プログラムでこの失敗を検出する方法はありますか? 使用したコードは次のとおりです。
[gmail smpt が使用されていることに注意してください]
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.stmp.user", "abc@gmail.com");
//If you want you use TLS
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.password", "password");
// If you want to use SSL
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
String username = "abc@gmail.com";
String password = "password";
return new PasswordAuthentication(username,password);
}
});
String[] to = {"test1@gmail.com","test2@yahoo.in","test3@gmail.com","test4@gmail.com"};
String from = "abc@gmail.com";
String subject = "Testing...";
MimeMessage msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress(from));
InternetAddress[] addressTo = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++)
{
addressTo[i] = new InternetAddress(to[i]);
}
msg.setRecipients(RecipientType.TO, addressTo);
// msg.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject);
// msg.setText("JAVA is the BEST");
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText("This is message body");
// Create a multipar message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
String filename = "file.txt";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
// Send the complete message parts
msg.setContent(multipart );
Transport transport = session.getTransport("smtp");
transport.send(msg);
System.out.println("E-mail sent !");
}
catch(Exception exc) {
System.out.println(exc);
}
}
}