在java邮件中获取送货确认

时间:2013-06-28 14:22:21

标签: java javamail

我有一个简单的java邮件代码,我试图通过电子邮件或通过任何可能的方式确认电子邮件是否成功发送,现在我已经尝试了以下代码,电子邮件已发送很好,只是因为我无法收到送货确认信。

import java.io.File;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import com.sun.mail.smtp.SMTPMessage;

import net.htmlparser.jericho.Source;

public class SmtpMail {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String to = "amit.jadhav@myorg.com"; //both emails are valid
        String from = "reciever@sample.com";
        String host = "relay.myorg.com";
        Properties properties = System.getProperties();
        properties.setProperty("mail.smtp.host", host);
        properties.setProperty("mail.transport.protocol", "SMTP");
        Session session = Session.getDefaultInstance(properties);

        SMTPMessage smtpMessage = new SMTPMessage(session);
        try {
            smtpMessage.setSender(new InternetAddress(from));
            smtpMessage.setRecipient(Message.RecipientType.TO,
                    new InternetAddress(to));
            smtpMessage.setSubject("Hello Test Email");
            smtpMessage.setText("Test Email to check Delivery Report");
            smtpMessage.setReturnOption(SMTPMessage.RETURN_FULL);
            smtpMessage.setNotifyOptions(SMTPMessage.NOTIFY_SUCCESS);
            Transport.send(smtpMessage);
        } catch (Exception e) {
            System.out.println("Error while building smpt email");
        }

    }

}

我是否在正确的轨道上?我还需要推荐别的吗?我试图了解有关交付状态通知的更多信息,但到目前为止还没有找到一个有效的例子。

1 个答案:

答案 0 :(得分:1)

看起来不错。确保您的smtp服务器支持传递状态通知。

使用telnet发送EHLO应该足够了;它应该输出DSN。像

这样的东西
[root@blah ~]$ telnet localhost 25
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 blah ESMTP Sendmail 8.14.3/8.14.3/Debian-9.1ubuntu1; Fri, 28 Jun 2013 10:30:28 -0400; (No UCE/UBE) logging access from: localhost(OK)-localhost [127.0.0.1]
EHLO toto
250-blah Hello localhost [127.0.0.1], pleased to meet you
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-EXPN
250-VERB
250-8BITMIME
250-SIZE
250-DSN
250-ETRN
250-AUTH DIGEST-MD5 CRAM-MD5
250-DELIVERBY
250 HELP