通过Exchange Server使用Javamail发送电子邮件

时间:2017-07-13 15:01:17

标签: java smtp javamail exchange-server

我们有一台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服务器似乎有效,但无法发送消息,导致上述错误。它可能是什么?

问候,

凯文

编辑:

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

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

我知道你想要smtp选项,但我觉得问题是你的服务器是如何设置的,而不是你的代码。如果您获得EWS-Java Api,您可以直接登录Exchange服务器并以此方式获取邮件。下面是使这项工作的代码:

 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方法向任何人发送电子邮件。

答案 1 :(得分:0)

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

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