Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Managing undeliverable feedback, notification

843830Mar 9 2006 — edited Mar 10 2006
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("my-em@il.com"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress("my.em@il.com"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress("my.wrong.em@il.com));
message.addRecipient(Message.RecipientType.TO, new InternetAddress("another.wrong.em@il.com"));

message.addHeader("Reply-To", "my.em@il.com");
message.addHeader("Return-Receipt-To", "my.em@il.com");
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[i]).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[i]).getAddress());
}
}
System.out.println("");
}
}

}

--- END ---
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 7 2006
Added on Mar 9 2006
4 comments
388 views