Salingga
Salingga

Reputation: 109

JavaMail Get Message for Undelivered Emails (to Gmail or Ymail)

I'm trying to build an app to send bulk report emails to many addresses with various hosts. I'm using Javamail and well, I'm still learning it though.

I found an example and try sending emails with my company server as host (let's say xyz company).

here is the sample code

package mailexample;

import javax.mail.*;
import javax.mail.internet.*;


public class MailExample {
public static void send(String smtpHost, int smtpPort,
    String from, String to,
    String subject, String content) {

    try {

        java.util.Properties props = new java.util.Properties();
        props.put("mail.smtp.host", smtpHost);
        props.put("mail.smtp.port", ""+smtpPort);
        Session session = Session.getDefaultInstance(props, null);
        //Store store = session.getStore();
        //Folder folder = store.getFolder("INBOX");
        //System.out.println(folder.getMessage(1)); 

        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(from));
        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
        msg.setSubject(subject);
        msg.setText(content);

        Transport.send(msg);
    }
    catch(Exception e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) throws Exception {
    try {
        send("mail.xyz.ac", 25, "[email protected]", "[email protected]",
        "title", "content");
    }
    catch(Exception e) {
        e.printStackTrace();
    }
}
}

It works fine and I get an error stacktrace when the address is invalid. But that is only happen if I send an email to the same server/host which is mail.xyz.ac.

If I send an email to some random gmail or ymail addresses (that likely don't exist), my app return success message but nothing happened after that, only a message (like mailer-daemon in gmail) in sender inbox that said it is not delivered.

The problem is, I need to store that message in my database for further notice. Is it possible to get that message from my app?

Upvotes: 1

Views: 2579

Answers (0)

Related Questions