简体   繁体   English

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

[英]Sending EMail with Javamail via Exchange Server

we have an Exchange Server and i wanted to test sending a mail with it. 我们有一个Exchange Server,我想测试发送邮件。 But somehow i always get the error: 但是以某种方式我总是会得到错误:

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)

i tried looking at our exchange if anonymous users were allowed and they are, our Printer also send Mails without any authentification. 如果允许匿名用户,我尝试查看我们的交易所,并且我们的打印机还发送了未经任何身份验证的邮件。

Here is my Java code, hope someone can help: 这是我的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();
    }
}
}

I also tried SimpleMail, but there is the same error. 我也尝试过SimpleMail,但是存在相同的错误。

The Connection to the smtp Server seems to work, but the message cannot be send, cause of the error above. 到smtp服务器的连接似乎可以正常工作,但是由于上述错误导致无法发送消息。 What could it be? 会是什么呢?

Greetings, 问候,

Kevin 凯文

Edit: 编辑:

i found my error, i don't know why our printers can send maisl without errors but it seems i had to whitelist my ip at our exchange server. 我发现了我的错误,我不知道为什么我们的打印机可以毫无错误地发送maisl,但似乎我不得不在我们的交换服务器上将我的IP列入白名单。 Code was completely fine. 代码是完全可以的。

thanks for the help 谢谢您的帮助

I know you are wanting the smtp option, but I have a feeling the issue is how your server is setup and not in your code. 我知道您需要smtp选项,但是我感觉到问题在于服务器的设置方式而不是代码中。 If you get the EWS-Java Api, you can log into your exchange server directly and grab mail that way. 如果您获得EWS-Java Api,则可以直接登录到交换服务器并以这种方式获取邮件。 Below is the code that would make that work: 下面是使该代码起作用的代码:

 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; }
      }
 }

In your code you just have to create the class, then use the sendEmail method to send emails to whomever. 在您的代码中,您只需创建类,然后使用sendEmail方法将电子邮件发送给任何人。

Your JavaMail code is not authenticating to your server , which may be why the server is rejecting the message with that error message. 您的JavaMail代码未对服务器进行身份验证 ,这可能是服务器拒绝包含该错误消息的消息的原因。 (Spammers often use open email servers.) (垃圾邮件发送者经常使用开放式电子邮件服务器。)

Change your code to call the Transport.send method that accepts a user name and password . 更改代码以调用接受用户名和密码Transport.send方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM