0

I am implementing a Bulk mail application, in this applica This link.

I am able to connect to the server and and send the emails to the destination addresses, but I want to identify the undelivered mails.

By using below program I am able to get the email subjects. But based on the subject it will be difficult to identify the exact undelivered mails.

public static void main(String[] args) {

        Properties props = System.getProperties();
        props.setProperty("mail.host", host);
        props.setProperty("mail.user", user);
        props.setProperty("mail.from", from);
        //props.setProperty("mail.debug", "true");
        //props.setProperty("mail.domain", domain);
        try {
        Session session = Session.getInstance(props, null);
        Store store = session.getStore(protocol);
        Session session1 = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(user, password);
                    }
                });

        
        System.out.println((store.isConnected())?"Already Connected":"Not Already Connected");

        store.connect(host, port, user, password);
         
        
        Folder inbox = store.getFolder("INBOX");
        System.out.println("folder>>>" + inbox.getFullName() + "<<<");
        System.out.println("folder URLName>>>" + inbox.getURLName() + "<<<");
        System.out.println((inbox.exists()?"folder exists":"folder does not exist"));

        int folderType = inbox.getType();
        System.out.println("folder type>>>" + folderType + "<<<");

        inbox.open(Folder.READ_WRITE);
        System.out.println("Message Count:" + inbox.getMessageCount());

        Message[] m = inbox.getMessages();

        for (int x = 0; x < m.length; x++) {
            System.out.println(m[x].getSubject());
        }
        inbox.close(false);
        store.close();
        }catch(Exception e) {
            System.out.println(e.getLocalizedMessage());
        }
    }

How can I get the undelivered mails(Bounced).

I am using Hmailserver as my mail server.

Gen
  • 2,400
  • 4
  • 24
  • 46
  • SMTP protocol doesn't support that. All "bounced" emails are generated on some mail server, there is no RFC about what those emais should contain (there isn't even requirement to send them). – talex Aug 28 '20 at 11:05
  • @talex I have modified my question, there is no need to send them again, but I want to identify the bounced mails. I am using Hmailserver, is there any way to identify them. – Gen Aug 28 '20 at 11:24
  • Generally speaking there is no way to identify them. You can use heuristics to try. – talex Aug 28 '20 at 11:26

1 Answers1

0

You can use this below code

MimeMessage payload = (MimeMessage) message.getPayload();
Multipart mp = (Multipart) payload.getContent();
for (int i = 0; i < mp.getCount(); i++) {
                    BodyPart bodyPart = mp.getBodyPart(i);
                    StringWriter writer = new StringWriter();
                    IOUtils.copy(bodyPart.getInputStream(), writer);
                    System.out.println("Content inputstream: " +  writer.toString());


}
  • can you please explain a bit. – Gen Aug 28 '20 at 11:36
  • The diagnostic code information is a part of message content and can be read using the following code. Here payload is the object which contains all the data regarding your message and you can fetch your exact requirement through it. – Farukh Tariq Aug 28 '20 at 11:42
  • I am not have any method with `message.getPayload();` – Gen Aug 28 '20 at 11:43
  • I see. Well you can use this dependency. here is quick view on it https://docs.oracle.com/javaee/6/api/javax/mail/internet/MimeMessage.html – Farukh Tariq Aug 28 '20 at 11:49