Reputation: 19
This is my code.Can anyone tell me why it send an email two times to single person.
I have to send only one mail.where I will change to get a correct result?
public void dbbackup_notify(String email, String data, String subject) {
String toEmails = email;
Session session = Session.getInstance(props, newjavax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, pass);
}
});
Message message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(userid));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmails));
message.setSubject(subject);
message.setText(data);
message.setContent(data, "text/html");
transport = session.getTransport("smtp");
transport.connect(host, user, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
System.out.println("Sent Successfully: " + new Date());
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
System.out.println("Don't Send Successfully" + new Date());
}
}
Upvotes: 0
Views: 1214
Reputation: 29971
The obvious answer is because you're calling it twice. How do you know that you're not?
When you get two messages from your program, do they both have the same Message-ID?
If you call session.setDebug(true), do you see the message being sent twice?
BTW, note that setContent just overwrites what setText does; you don't need both. Declare message to be of type MimeMessage and then replace both set calls with message.setText(data, "utf-8", "html");
Note also that you don't need the Authenticator because you're passing the username and password in the connect call.
Upvotes: 1