3

我有一个简单的 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");
        }

    }

}

我在正确的轨道上吗?我需要参考别的东西吗?我试图了解有关交付状态通知的更多信息,但到目前为止还没有找到一个可行的示例。

4

1 回答 1

1

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

发送一个EHLOusing telnet 就足够了;它应该输出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
于 2013-06-28T14:32:25.370 回答