【问题标题】:Sending EMail with Javamail via Exchange Server通过 Exchange Server 使用 Javamail 发送电子邮件
【发布时间】:2017-12-18 11:04:48
【问题描述】:

我们有一个 Exchange Server,我想测试用它发送邮件。但不知何故,我总是得到错误:

com.sun.mail.smtp.SMTPSendFailedException: 550 5.7.1 Message rejected as spam by Content Filtering.

at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2108)
at com.sun.mail.smtp.SMTPTransport.finishData(SMTPTransport.java:1889)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1120)
at javax.mail.Transport.send0(Transport.java:195)
at javax.mail.Transport.send(Transport.java:124)
at Test.sendMailJava(Test.java:89)
at Test.main(Test.java:29)

如果允许匿名用户并且他们是允许的,我尝试查看我们的交换,我们的打印机也会发送未经任何身份验证的邮件。

这是我的Java代码,希望有人能帮忙:

import java.net.URI;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.simplejavamail.email.Email;
import org.simplejavamail.mailer.Mailer;
import org.simplejavamail.mailer.config.ProxyConfig;
import org.simplejavamail.mailer.config.ServerConfig;
import org.simplejavamail.util.ConfigLoader;

public class Test {

public static void main(String[] args) {
////        // TODO Auto-generated method stub
    sendMailJava();
}

public static void sendMailJava()
{
    String to = "Recipient"
    // Sender's email ID needs to be mentioned
    String from = "Sender";

    // Assuming you are sending email from localhost
    String host = "Server Ip-Adress";

    // Get system properties
    Properties  properties = System.getProperties();

    // Setup mail server
    properties.setProperty("mail.smtp.host", host);
    properties.setProperty("mail.smtp.port", "25");
    properties.setProperty("mail.imap.auth.plain.disable","true");
    properties.setProperty("mail.debug", "true");
    Session session = Session.getDefaultInstance(properties);
    try {
        // Create a default MimeMessage object.
        MimeMessage message = new MimeMessage(session);
        // Set From: header field of the header.
        message.setFrom(new InternetAddress(from));
        // Set To: header field of the header.
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
        // Set Subject: header field
        message.setSubject("Subject");
        // Now set the actual message
        message.setContent("Content", "text/html; charset=utf-8");
        // Send message
        Transport.send(message);
        System.out.println("Sent message successfully....");
    }catch (MessagingException mex) {
    mex.printStackTrace();
    }
}
}

我也试过 SimpleMail,但还是一样的错误。

与 smtp 服务器的连接似乎有效,但无法发送消息,原因是上述错误。会是什么?

您好,

凯文

编辑:

我发现了我的错误,我不知道为什么我们的打印机可以毫无错误地发送邮件,但似乎我不得不在我们的交换服务器上将我的 ip 列入白名单。代码完全没问题。

感谢帮助

【问题讨论】:

  • 为什么不让他们在 Exchange 服务器上为您创建一个帐户并使用 EWS 发送邮件?
  • 我在服务器上有一个帐户,但我想尝试在不使用 Outlook 的情况下发送电子邮件,就像我说我们的扫描仪和打印机也可以在没有任何身份验证的情况下发送邮件,这没关系,所以它必须工作不知何故...我的理论是邮件标题或文本中的某些内容不正确,因此垃圾邮件过滤器会立即捕获电子邮件,但我不知道是什么
  • 我知道在我们的办公室里,我们的服务器上什至没有打开 smtp 端口。如果我是网络管理员并且它是开放的,我会设置服务器,以便只有特定 IP 可以使用它,其余的将被视为垃圾邮件。
  • 为什么不能打开 smtp 端口?我的意思是我们公司想发邮件对吧?我是我们公司的系统管理员,我看不到我们服务器的任何限制
  • 因为 Exchange 不使用 smtp。它使用 mapi

标签: java smtp jakarta-mail exchange-server


【解决方案1】:

您的 JavaMail 代码是 not authenticating to your server,这可能是服务器拒绝带有该错误消息的消息的原因。 (垃圾邮件发送者通常使用开放式电子邮件服务器。)

更改您的代码以调用Transport.send method that accepts a user name and password

【讨论】:

  • 就像我说的,我们的打印机也发送没有任何身份验证的电子邮件,所以它必须在没有它的情况下工作,但以防万一,我也尝试了你的方法,我什至无法连接到该方法服务器
  • 认证失败了吗?您可能需要将属性mail.smtp.ssl.enable 设置为true。打印机可能会根据(例如)其 IP 地址在服务器上列入白名单。在您的测试程序中,您是否从邮件服务器上的其他地址发送或发送到其他地址? JavaMail debug output 显示什么?
  • 没有身份验证没有失败,就像上面提到的错误是垃圾邮件内容过滤器捕获了电子邮件
  • 你说它没有连接。如果它没有连接,您将不会收到该错误。为了清楚起见,您发送的实际内容是单个单词“内容”,对吗?你肯定不会发送任何看起来甚至接近垃圾邮件的东西,对吧?
  • 不,我从来没有说过它没有连接,当然还有其他文本而不是内容,但没有什么接近垃圾邮件...我编辑了我的第一篇文章,因为我发现了我的错误
【解决方案2】:

我知道您想要 smtp 选项,但我感觉问题在于您的服务器是如何设置的,而不是在您的代码中。如果您获得 EWS-Java Api,您可以直接登录到您的交换服务器并以这种方式获取邮件。下面是可以实现该功能的代码:

 public class ExchangeConnection {
      private final ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2); // change to whatever server you are running, though 2010_SP2 is the most recent version the Api supports

      public ExchangeConnection(String username, String password) {
           try {
                service.setCredentials(new WebCredentials(username, password));
                service.setUrl(new URI("https://(your webmail address)/ews/exchange.asmx"));
           }
           catch (Exception e) { e.printStackTrace(); }
      }

      public boolean sendEmail(String subject, String message, List<String> recipients, List<String> filesNames) {
           try {
                EmailMessage email = new EmailMessage(service);
                email.setSubject(subject);
                email.setBody(new MessageBody(message));
                for (String fileName : fileNames) email.getAttachments().addFileAttachment(fileName);
                for (String recipient : recipients) email.getToRecipients().add(recipient);
                email.sendAndSaveCopy();
                return true;
           }
           catch (Exception e) { e.printStackTrace(); return false; }
      }
 }

在您的代码中,您只需创建类,然后使用 sendEmail 方法向任何人发送电子邮件。

【讨论】:

  • 我会试试,如果我们有 Exchange 2007,我必须在 serverversion 中添加什么?我可以在没有 Outlook 的情况下使用该功能吗?
  • 直接支持2007
  • 这个“功能”与 Outlook 没有任何关系。它不需要使用电子邮件程序来获取您的电子邮件......它会联系交换服务器本身并发送电子邮件
  • 直接是什么意思?我必须写什么而不是 Exchange2010_SP2?
  • Exchange2007_SP2 是您需要的标志。在我的情况下,我们有 Exchange 2012,我必须使用 Exchange2010_SP2,所以它不直接支持我的 Exchange 服务器
猜你喜欢
  • 2020-10-02
  • 1970-01-01
  • 2013-11-12
  • 1970-01-01
  • 1970-01-01
  • 2012-04-19
  • 2013-06-05
  • 1970-01-01
  • 2016-12-28
相关资源
最近更新 更多