简体   繁体   English

如何在Google App引擎(Java)中使用IMAP

[英]How to use IMAP in Google App engine (Java)

We are trying to use IMAP in our web app which is running on Google App enigne (GAE) , we are using java version of GAE. 我们正在尝试在运行于Google App enigne(GAE)的Web应用程序中使用IMAP,我们正在使用Java版本的GAE。

By default GAE included javax.mail packages in their sdk but they not included IMAP protocol packages. 默认情况下,GAE在其sdk中包含javax.mail软件包,但不包含IMAP协议软件包。

So we try to add java mail library to our app to get IMAP packges but this library not compatible with app engine java sdk javax.mail packages because this library also having javax.mail packages with differences in code compare to appengine sdk javax.mail packages. 因此,我们尝试将Java邮件库添加到我们的应用中以获取IMAP包,但是该库与App Engine Java sdk javax.mail包不兼容,因为该库还具有与appengine sdk javax.mail包相比代码有所不同的javax.mail包。

so what is the best way to use IMAP in Java version of app engine ?? 那么在Java版本的应用程序引擎中使用IMAP的最佳方法是什么?

I recently needed to send email from AppEngine using IMAP and this worked for me. 我最近需要使用IMAP从AppEngine发送电子邮件,这对我来说很有效。 I've added this libraries: 我添加了以下库:

    <!-- https://mvnrepository.com/artifact/javax.activation/activation -->
    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>activation</artifactId>
        <version>1.1.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.mail/javax.mail-api -->
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>javax.mail-api</artifactId>
        <version>1.6.0-rc2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.sun.mail/smtp -->
    <dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>smtp</artifactId>
        <version>1.6.0-rc2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.sun.mail/javax.mail -->
    <dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>javax.mail</artifactId>
        <version>1.6.0-rc2</version>
    </dependency>

And this is the code: 这是代码:

package ro.cbn.it.call.utils;

import javax.activation.DataHandler;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.URLName;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.util.ByteArrayDataSource;

import com.sun.mail.smtp.SMTPTransport;

import java.util.Properties;

public class MailUtils {

    public static void sendEmail(String userId, String password, String messageBody, String fromEmail, String messageSubject, String messageToAddress) throws MessagingException {
        Properties props = new Properties();
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.starttls.required", "true");
        props.put("mail.smtp.sasl.enable", "false");
        Session session = Session.getInstance(props);
        //session.setDebug(true);
        final URLName unusedUrlName = null;
        SMTPTransport transport = new SMTPTransport(session, unusedUrlName);
        // If the password is non-null, SMTP tries to do AUTH LOGIN.
        transport.connect("smtp.gmail.com", 587, userId, password);
        MimeMessage message = new MimeMessage(session);
        DataHandler handler = new DataHandler(new ByteArrayDataSource(messageBody.getBytes(), "text/plain"));
        message.setSender(new InternetAddress(fromEmail));
        message.setSubject(messageSubject);
        message.setDataHandler(handler);
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(messageToAddress));
        transport.sendMessage(message, message.getAllRecipients());

        System.out.println("SentTo:"+messageToAddress);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM