JavaMail Exchange身份验证

raf*_*hoa 9 java authentication exchange-server web-applications jakarta-mail

我正在尝试使用JavaMail从我的应用程序中使用Exchange身份验证来执行此操作.有人可以给我一个指导吗?身份验证后,我需要发送邮件,这是我使用JavaMail的主要原因.我发现的所有链接都谈到了这个问题,但我认为从Java开始这一定很容易.提前致谢.

Jav*_*all 7

这是一个很好的问题!我已经解决了这个问题.

首先,你应该导入jar ews-java-api-2.0.jar.如果您使用maven,您可以将以下代码添加到您的pom.xml

<dependency>
  <groupId>com.microsoft.ews-java-api</groupId>
  <artifactId>ews-java-api</artifactId>
  <version>2.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

其次,你应该新的java类名为MailUtil.java.Some Exchange Server SMTP默认情况下不启动服务,所以我们使用Microsoft Exchange WebServices(EWS)而不是SMTP服务.

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();

    }
}
Run Code Online (Sandbox Code Playgroud)

如果您想获得更多细节,请参阅https://github.com/OfficeDev/ews-java-api/wiki/Getting-Started-Guide


Bal*_*usC 6

身份验证后,我需要发送邮件

以下示例适用于Exchange服务器:

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) {}
}
Run Code Online (Sandbox Code Playgroud)


Dav*_*itz 3

对我有用:

Properties props = System.getProperties();
// Session configuration is done using properties. In this case, the IMAP port. All the rest are using defaults
props.setProperty("mail.imap.port", "993");
// creating the session to the mail server
Session session = Session.getInstance(props, null);
// Store is JavaMails name for the entity holding the mails
Store store = session.getStore("imaps");
// accessing the mail server using the domain user and password
store.connect(host, user, password);
// retrieving the inbox folder
Folder inbox = store.getFolder("INBOX");
Run Code Online (Sandbox Code Playgroud)

此代码基于随 java 邮件下载而提供的示例代码。