I have the following sample program that sends email. But how do I make sure the email is delivered successfully to the recipient. I want to make sure assuming all the email addresses are valid want to check if email is delivered successfully. As currently it delivers email sometimes and sometimes I don't get email. Using MS Exchange.
package email;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import javax.mail.event.*;
// Send a simple, single part, text/plain e-mail
public class TestEmail implements ConnectionListener, TransportListener{
private static final String SMTP_HOST_NAME = "mail.somedomain.com";
private static final String SMTP_AUTH_USER = "user@somedomain.com";
private static final String SMTP_AUTH_PWD = "password";
public static void main(String[] args) {
TestEmail te = new TestEmail();
// SUBSTITUTE YOUR EMAIL ADDRESSES HERE!!!
String to = "user12@yahoo.com";
String from = "user@somedomain.com";
// SUBSTITUTE YOUR ISP'S MAIL SERVER HERE!!!
String host = "mail.somedomain.com";
// Create properties, get Session
Properties props = new Properties();
// If using static Transport.send(),
// need to specify which host to send it to
props.put("mail.smtp.host", host);
//props.put("mail.smtp.port", "25");
props.put("mail.smtp.auth", "true");
// To see what is going on behind the scene
//props.put("mail.debug", "true");
te.go(props, from, to);
}
public TestEmail() {}
public void go(Properties props, String from, String to) {
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, auth);
//Session session = Session.getInstance(props);
try {
Transport trans = null;
// Instantiatee a message
Message msg = new MimeMessage(session);
//Set message attributes
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("Test E-Mail through Java");
msg.setSentDate(new Date());
// Set message content
msg.setText("This is a test of sending a " +
"plain text e-mail through Java.\n" +
"Here is line 2.");
// get the smtp transport for the address
trans = session.getTransport(address[0]);
// register ourselves as listener for ConnectionEvents
// and TransportEvents
trans.addConnectionListener(this);
trans.addTransportListener(this);
// connect the transport
trans.connect();
// send the message
trans.sendMessage(msg, address);
//Send the message
//trans.send(msg);
}
catch (MessagingException mex)
{
// Prints all nested (chained) exceptions as well
mex.printStackTrace();
}
}
public void messageDelivered(TransportEvent e)
{
System.out.println("****** DEBUG messageDelivered");
}
public void messageNotDelivered(TransportEvent e)
{
System.out.println("****** DEBUG messageNotDelivered");
}
public void messagePartiallyDelivered(TransportEvent e)
{
System.out.println("****** DEBUG messagePartiallyDelivered");
}
public void closed(ConnectionEvent e)
{
System.out.println("****** DEBUG closed");
}
public void disconnected(ConnectionEvent e)
{
System.out.println("****** DEBUG disconnect");
}
public void opened(ConnectionEvent e)
{
System.out.println("****** DEBUG opened");
}
/**
* SimpleAuthenticator is used to do simple authentication
* when the SMTP server requires it.
*/
private class SMTPAuthenticator extends javax.mail.Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
String username = SMTP_AUTH_USER;
String password = SMTP_AUTH_PWD;
return new PasswordAuthentication(username, password);
}
}
}//End of class
Thanks