【问题标题】:JavaMailSender SMTP Bounce back - Different domain email addressJavaMailSender SMTP 反弹 - 不同的域电子邮件地址
【发布时间】:2013-01-27 04:31:05
【问题描述】:

我正在使用 Spring java mailer 类向我的用户发送电子邮件: org.springframework.mail.javamail.JavaMailSenderImpl 版本 1.4 使用 Spring 框架 3.0.7.RELEASE。

我想为失败的电子邮件设置退回邮件,以发送到我的用户的电子邮件地址,该地址与我的 smtp 服务器的域不同。有谁知道实现这一目标的方法? 例如: 我的系统向 email-does-not-exist@gmail.com 发送电子邮件。我的 smtp 服务器配置为具有域 somebusiness.com。失败后,将退回邮件发送给我的用户:test.user@gmail.com。

我多次阅读以下文章: Specifying the bounce-back address for email

我尝试使用他们设置 mail.smtp.from 属性的方法,但它根本不会发送任何电子邮件(甚至还没有计算来自无效电子邮件的退回尝试)。

Properties p = new Properties();
p.put("mail.smtp.from", "test.user@gmail.com"); //If I comment this out, it sends emails again
mailSender.setJavaMailProperties(p);
Session session = Session.getDefaultInstance(p, null);
MimeMessage mimeMessage = new MimeMessage(session);

MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,
                false, "utf-8");
mimeMessage.setContent(emailBody, "text/html");
helper.setTo(toAddress);
helper.setSubject(subject);
helper.setFrom(fromAddress);
mailSender.send(mimeMessage);

有人知道为什么吗?显而易见的答案似乎是我们使用的 smtp 服务器正在阻止它,但我希望有其他潜在的想法。

【问题讨论】:

    标签: spring smtp jakarta-mail email-bounces


    【解决方案1】:

    我刚刚检查了 Apache Commons Mail 如何实现它的退回功能,它实际上只是设置了发件人地址。这意味着您可以在 Spring Mail 中通过 org.springframework.mail.javamail.MimeMessageHelper 类上的 setFrom(...) 执行相同的操作。

    源代码sn-p来自org.apache.commons.mail.Email类:

    if (this.bounceAddress != null) {
        properties.setProperty(MAIL_SMTP_FROM, this.bounceAddress);
    }
    

    在来源中查看它: http://grepcode.com/file/repo1.maven.org/maven2/org.apache.commons/commons-email/1.2/org/apache/commons/mail/Email.java#539

    【讨论】:

    • 调用MimeMessageHelpersetFrom(...)方法是设置特定消息的from-property。这与定义 Java 邮件属性 mail.smtp.from 不同!但是在您发布的 sn-p 中设置了此属性。 mail.smtp.from的定义是Email address to use for SMTP MAIL command. This sets the envelope return address. Defaults to msg.getFrom() or InternetAddress.getLocalAddress(). NOTE: mail.smtp.user was previously used for this.
    【解决方案2】:

    我遇到了类似的问题。我还没有解决方案,但目前我正在考虑用org.apache.commons.mail 替换Spring 的邮件包,因为它有一个简单的setBounceAddress(emailAddressString) 方法。

    请参阅用户指南的最后部分“处理退回的消息”:

    http://commons.apache.org/proper/commons-email//userguide.html

    以及 API 文档:

    http://commons.apache.org/proper/commons-email//apidocs/org/apache/commons/mail/Email.html#setBounceAddress(java.lang.String)

    【讨论】:

      猜你喜欢
      • 2011-03-11
      • 1970-01-01
      • 2013-01-24
      • 1970-01-01
      • 2015-06-13
      • 1970-01-01
      • 2016-03-20
      • 1970-01-01
      • 2013-12-20
      相关资源
      最近更新 更多