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

独自空忆成欢 提交于 2019-12-13 00:45:44

问题


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, "asdf@xyz.ac", "qwer@xyz.ac",
        "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?


回答1:


The JavaMail FAQ is your friend while learning JavaMail. This entry and this entry address your question. Also, be sure to read the entry about common mistakes.



来源:https://stackoverflow.com/questions/11754166/javamail-get-message-for-undelivered-emails-to-gmail-or-ymail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!