JavaMail 交换认证 [英] JavaMail Exchange Authentication

查看:24
本文介绍了JavaMail 交换认证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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's the main reason that I'm using JavaMail. All the links that I found talks about problems with this but I think this must be an easy task to do from Java. Thanks in advance.

推荐答案

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

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

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

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>

其次,你应该新建一个名为MailUtil.java的java类.一些Exchange Servers默认不启动SMTP服务,所以我们使用Microsoft Exchange WebServices(EWS) 而不是 SMTP 服务.

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

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

    }
}

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

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

这篇关于JavaMail 交换认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆