GMail没有返回退回的电子邮件

时间:2018-06-15 19:31:55

标签: java email gmail

我们有一个Java应用程序,可以通过Google提供的公司电子邮件帐户发送电子邮件。它在大多数情况下都能正常工作,但是当我们有错误的电子邮件地址时,无论我们发送的是什么,我们都不会收到退回邮件。

但是,如果我登录到同一个电子邮件帐户并发送到同一个错误的电子邮件地址,我会收到退回邮件。

为什么当我以编程方式发送电子邮件时,我没有收到退回邮件?

以下是我们发送电子邮件的代码:

private void sendMessageImpl(String toAddresses, String replyToAddress, String subject, String body, int failedAttempts, long startTime) throws MessagingException {
    final Properties properties = getMailProperties();

    String fromAddress = properties.getProperty("mail.from");        
    Session session = getSession();
    Transport transport = null;
    boolean tryingToConnect = false;

    try {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(fromAddress));
        toAddresses = toAddresses.replaceAll(";", ",");
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddresses));
        message.setSubject(subject);
        message.setText(body);

        // Send the message
        transport = session.getTransport(message.getAllRecipients()[0]);
        tryingToConnect = true;
        transport.connect();
        tryingToConnect = false;
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
        transport = null;
    } catch (NullPointerException e) {
        Logger.getLogger(Mail.class.getName()).log(Level.SEVERE, "Fail to send email to " + toAddresses+". Subject: "+subject+"\nContents: "+body, e);
        throw new MessagingException("Mail Failure");
    } catch (MessagingException e) {
        Logger.getLogger(Mail.class.getName()).log(Level.SEVERE, "Fail to send email to " + toAddresses+". Subject: "+subject+"\nContents: "+body, e);
    } finally {
        if(transport != null) {
            try {
                transport.close();
            } catch (MessagingException e) {
                Logger.getLogger(Mail.class.getName()).log(Level.SEVERE, "Failed to close transport after earlier error left it open. ", e);
            }
        }
    }
}

0 个答案:

没有答案