H
himgi
Hi to all,
I'd like to know how to catch the unliverable messages.
I'm sending email through my class below, but I cannot catch if a
message is not arrived, if the email is wrong and if the receiver has
received my message.
I could I catch all these things?
Where do I find a complete list headers for JavaMail?
Thanks in advance, Himgi.
--- START ---
import java.util.Properties;
import javax.mail.*;
import javax.mail.event.*;
import javax.mail.internet.*;
public class MailExample implements TransportListener,
ConnectionListener {
/**
*
*/
public MailExample() {
super();
}
public static void main(String[] args) throws Exception {
MailExample example = new MailExample();
example.test();
}
public void test() throws Exception {
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", "myownsmtpserver"); // the smtp is valid
// Get session
Session session = Session.getInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("(e-mail address removed)"));
message.addRecipient(Message.RecipientType.TO, new
InternetAddress("(e-mail address removed)"));
message.addRecipient(Message.RecipientType.TO, new
InternetAddress("(e-mail address removed)));
message.addRecipient(Message.RecipientType.TO, new
InternetAddress("(e-mail address removed)"));
message.addHeader("Reply-To", "(e-mail address removed)");
message.addHeader("Return-Receipt-To", "(e-mail address removed)");
message.addHeader("X-Mailer", "JavaMail API");
message.setSentDate(new Date());
message.setSubject("Testing JavaMail");
message.setText("POWER S.S. LAZIO");
SMTPMessage msg = new SMTPMessage(message);
// Send message
Transport transport = session.getTransport("smtp");
transport.addConnectionListener(this);
transport.addTransportListener(this);
transport.connect();
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
System.out.println("\nMail was sent successfully.");
}
public void opened(ConnectionEvent e) {
System.out.println("Connection opened");
}
public void disconnected(ConnectionEvent e) {
System.out.println("Connection disconnected");
}
public void closed(ConnectionEvent e) {
System.out.println("Connection closed");
}
public void messageDelivered(TransportEvent e) {
System.out.println("Message delivered for:");
if (e != null) {
Address[] a = e.getValidSentAddresses();
if (a != null && a.length > 0) {
for (int i = 0; i < a.length; i++) {
System.out.println(((InternetAddress) a).getAddress());
}
}
System.out.println("");
}
}
public void messageNotDelivered(TransportEvent e) {
System.out.println("Message not delivered for:");
if (e != null) {
Address[] a = e.getValidUnsentAddresses();
if (a != null && a.length > 0) {
for (int i = 0; i < a.length; i++) {
System.out.println(((InternetAddress) a).getAddress());
}
}
System.out.println("");
}
}
public void messagePartiallyDelivered(TransportEvent e) {
System.out.println("These addresses are invalid:");
if (e != null) {
Address[] a = e.getInvalidAddresses();
if (a != null && a.length > 0) {
for (int i = 0; i < a.length; i++) {
System.out.println(((InternetAddress) a).getAddress());
}
}
System.out.println("");
}
}
}
--- END ---
I'd like to know how to catch the unliverable messages.
I'm sending email through my class below, but I cannot catch if a
message is not arrived, if the email is wrong and if the receiver has
received my message.
I could I catch all these things?
Where do I find a complete list headers for JavaMail?
Thanks in advance, Himgi.
--- START ---
import java.util.Properties;
import javax.mail.*;
import javax.mail.event.*;
import javax.mail.internet.*;
public class MailExample implements TransportListener,
ConnectionListener {
/**
*
*/
public MailExample() {
super();
}
public static void main(String[] args) throws Exception {
MailExample example = new MailExample();
example.test();
}
public void test() throws Exception {
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", "myownsmtpserver"); // the smtp is valid
// Get session
Session session = Session.getInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("(e-mail address removed)"));
message.addRecipient(Message.RecipientType.TO, new
InternetAddress("(e-mail address removed)"));
message.addRecipient(Message.RecipientType.TO, new
InternetAddress("(e-mail address removed)));
message.addRecipient(Message.RecipientType.TO, new
InternetAddress("(e-mail address removed)"));
message.addHeader("Reply-To", "(e-mail address removed)");
message.addHeader("Return-Receipt-To", "(e-mail address removed)");
message.addHeader("X-Mailer", "JavaMail API");
message.setSentDate(new Date());
message.setSubject("Testing JavaMail");
message.setText("POWER S.S. LAZIO");
SMTPMessage msg = new SMTPMessage(message);
// Send message
Transport transport = session.getTransport("smtp");
transport.addConnectionListener(this);
transport.addTransportListener(this);
transport.connect();
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
System.out.println("\nMail was sent successfully.");
}
public void opened(ConnectionEvent e) {
System.out.println("Connection opened");
}
public void disconnected(ConnectionEvent e) {
System.out.println("Connection disconnected");
}
public void closed(ConnectionEvent e) {
System.out.println("Connection closed");
}
public void messageDelivered(TransportEvent e) {
System.out.println("Message delivered for:");
if (e != null) {
Address[] a = e.getValidSentAddresses();
if (a != null && a.length > 0) {
for (int i = 0; i < a.length; i++) {
System.out.println(((InternetAddress) a).getAddress());
}
}
System.out.println("");
}
}
public void messageNotDelivered(TransportEvent e) {
System.out.println("Message not delivered for:");
if (e != null) {
Address[] a = e.getValidUnsentAddresses();
if (a != null && a.length > 0) {
for (int i = 0; i < a.length; i++) {
System.out.println(((InternetAddress) a).getAddress());
}
}
System.out.println("");
}
}
public void messagePartiallyDelivered(TransportEvent e) {
System.out.println("These addresses are invalid:");
if (e != null) {
Address[] a = e.getInvalidAddresses();
if (a != null && a.length > 0) {
for (int i = 0; i < a.length; i++) {
System.out.println(((InternetAddress) a).getAddress());
}
}
System.out.println("");
}
}
}
--- END ---