JavaMail获取未送达电子邮件的邮件(到Gmail或Ymail) [英] JavaMail Get Message for Undelivered Emails (to Gmail or Ymail)

查看:172
本文介绍了JavaMail获取未送达电子邮件的邮件(到Gmail或Ymail)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个应用程序,以将批量报告电子邮件发送到具有各种主机的许多地址.我正在使用Javamail,虽然我仍然在学习它.

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.

我找到了一个示例,并尝试使用公司服务器作为主机(例如xyz公司)发送电子邮件.

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

这是示例代码

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();
    }
}
}

它工作正常,当地址无效时,我得到一个错误的堆栈跟踪. 但这只有在我将电子邮件发送到同一服务器/主机mail.xyz.ac时才会发生.

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.

如果我将电子邮件发送到一些随机的gmail或ymail地址(可能不存在),则我的应用返回了成功消息,但此后没有任何反应,只有发件人收件箱中的一条消息(例如gmail中的mailer-daemon)说它没有交付.

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?

推荐答案

JavaMail FAQ 是您学习JavaMail的朋友. 此条目常见错误的条目

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.

这篇关于JavaMail获取未送达电子邮件的邮件(到Gmail或Ymail)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆