title: Sending EMail with Javamail via Exchange Server
body: <p>we have an Exchange Server and i wanted to test sending a mail with it. But somehow i always get the error:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>i tried looking at our exchange if anonymous users were allowed and they are, our Printer also send Mails without any authentification.</p>
  
  <p>Here is my Java code, hope someone can help:</p>
  
  <pre><code>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();
      }
  }
  }
  </code></pre>
  
  <p>I also tried SimpleMail, but there is the same error.</p>
  
  <p>The Connection to the smtp Server seems to work, but the message cannot be send, cause of the error above. What could it be?</p>
  
  <p>Greetings,</p>
  
  <p>Kevin</p>
  
  <p>Edit:</p>
  
  <p>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. Code was completely fine.</p>
  
  <p>thanks for the help </p>