9

これを行うために、JavaMail を使用してアプリから Exchange 認証を使用しようとしています。これを行うためのガイドを教えてもらえますか?認証後、JavaMail を使用している主な理由であるメールを送信する必要があります。私が見つけたすべてのリンクはこれに関する問題について語っていますが、これは Java から行うのは簡単な作業であるに違いないと思います。前もって感謝します。

4

9 に答える 9

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>

第 2 に、新しい Java クラスを作成する必要があります。一部のMailUtil.javaExchange 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();

    }
}

詳細については、https://github.com/OfficeDev/ews-java-api/wiki/Getting-Started-Guideを参照してください。

于 2017-01-08T03:28:39.763 に答える
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) {}
}
于 2009-11-11T12:30:27.290 に答える
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");

このコードは、java メールのダウンロードで届くサンプル コードに基づいています。

于 2009-11-11T11:43:06.180 に答える
1

Exchange はデフォルトでSMTPSMTP protocolサービスを開始しないため、Exchange サーバーに接続して電子メールを送信するために使用することはできません。メールサーバー管理者がExchangeでSMTPサービスを有効にしているため、BalusCは上記のコードで正常に動作します.ほとんどの場合、SMTPは無効になっています.私も解決策を探しています.

これ は私が見つけたものの中で最良の答えですが、60日後に支払いをしなければならないのはなんともどかしいことです。

于 2010-07-10T08:12:24.193 に答える
0

一部の Exchange サーバーでは、smtp プロトコルが有効になっていません。
このような場合、DavMailを使用できます。

于 2012-09-27T23:53:50.357 に答える
0

以前のコメントで Populus が述べたように、ews-java-api を試しました。jdk1.6を使用したJava SE環境で行われ、魅力的に機能します。
サンプルに関連付ける必要があったライブラリは次のとおりです。

  • 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

それが役に立てば幸い。

于 2016-07-05T15:20:02.390 に答える