JavaMail Exchange Authentication

后端 未结 8 2054
长情又很酷
长情又很酷 2020-12-03 05:53

I\'m trying to use Exchange authentication from my app using JavaMail to do this. Could some one give me a guide to do this? After authentication I need to send mails that\'

相关标签:
8条回答
  • 2020-12-03 06:28

    Tried the ews-java-api, as mentioned by Populus on a previous comment. It was done on a Java SE environment with jdk1.6 and it works like a charm.
    These are the libs that I had to associate with my sample:

    • commons-cli-1.2.jar
    • commons-codec-1.10.jar
    • commons-lang3-3.1.jar
    • commons-logging-1.2.jar
    • ews-java-api-2.0.jar
    • httpclient-4.4.1.jar
    • httpcore-4.4.5.jar

    Hope it helps.

    0 讨论(0)
  • 2020-12-03 06:29

    Microsoft released an open sourced API for connecting to Exchange Web Service

    https://github.com/OfficeDev/ews-java-api

    0 讨论(0)
  • 2020-12-03 06:34

    Exchange does not start SMTP service by default, so we can't use SMTP protocol to connect to Exchange server and try to send email. BalusC can work fine with the above code because your mailserver administrator enabled SMTP service on Exchange.while in most cases SMTP is disabled.I am also looking for solution.

    This is the best answer among what i have found, but what a frustration is that you have to pay for it after 60 days.

    0 讨论(0)
  • 2020-12-03 06:38

    After authentication I need to send mails

    The below example works fine here with Exchange servers:

    Properties properties = new Properties();
    properties.put("mail.transport.protocol", "smtp");
    properties.put("mail.smtp.host", "mail.example.com");
    properties.put("mail.smtp.port", "2525");
    properties.put("mail.smtp.auth", "true");
    
    final String username = "username";
    final String password = "password";
    Authenticator authenticator = new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    };
    
    Transport transport = null;
    
    try {
        Session session = Session.getDefaultInstance(properties, authenticator);
        MimeMessage mimeMessage = createMimeMessage(session, mimeMessageData);
        transport = session.getTransport();
        transport.connect(username, password);
        transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
    } finally {
        if (transport != null) try { transport.close(); } catch (MessagingException logOrIgnore) {}
    }
    
    0 讨论(0)
  • 2020-12-03 06:39

    Some Exchange servers don't have smtp protocol enabled.
    In these cases you can use DavMail.

    0 讨论(0)
  • 2020-12-03 06:49

    It is a good question! I have solved this issue.

    First, you should import the jar ews-java-api-2.0.jar. if you use maven, you would add the following code into your pom.xml

    <dependency>
      <groupId>com.microsoft.ews-java-api</groupId>
      <artifactId>ews-java-api</artifactId>
      <version>2.0</version>
    </dependency>
    

    Secondly, you should new java class named MailUtil.java.Some Exchange Servers don't start SMTP service by default, so we use Microsoft Exchange WebServices(EWS) instead of SMTP service.

    MailUtil.java

    package com.spacex.util;
    
    
    import microsoft.exchange.webservices.data.core.ExchangeService;
    import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
    import microsoft.exchange.webservices.data.core.service.item.EmailMessage;
    import microsoft.exchange.webservices.data.credential.ExchangeCredentials;
    import microsoft.exchange.webservices.data.credential.WebCredentials;
    import microsoft.exchange.webservices.data.property.complex.MessageBody;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import java.net.URI;
    
    /**
     * Exchange send email util
     *
     * @author vino.dang
     * @create 2017/01/08
     */
    public class MailUtil {
    
        private static Logger logger = LoggerFactory.getLogger(MailUtil.class);
    
    
    
        /**
         * send emial
         * @return
         */
        public static boolean sendEmail() {
    
            Boolean flag = false;
            try {
                ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1); // your server version
                ExchangeCredentials credentials = new WebCredentials("vino", "abcd123", "spacex"); // change them to your email username, password, email domain
                service.setCredentials(credentials);
                service.setUrl(new URI("https://outlook.spacex.com/EWS/Exchange.asmx")); //outlook.spacex.com change it to your email server address
                EmailMessage msg = new EmailMessage(service);
                msg.setSubject("This is a test!!!"); //email subject
                msg.setBody(MessageBody.getMessageBodyFromText("This is a test!!! pls ignore it!")); //email body
                msg.getToRecipients().add("123@hotmail.com"); //email receiver
    //        msg.getCcRecipients().add("test2@test.com"); // email cc recipients
    //        msg.getAttachments().addFileAttachment("D:\\Downloads\\EWSJavaAPI_1.2\\EWSJavaAPI_1.2\\Getting started with EWS Java API.RTF"); // email attachment
                msg.send(); //send email
                flag = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return flag;
    
        }
    
    
        public static void main(String[] args) {
    
            sendEmail();
    
        }
    }
    

    if you want to get more detail, pls refer to https://github.com/OfficeDev/ews-java-api/wiki/Getting-Started-Guide

    0 讨论(0)
提交回复
热议问题