简体   繁体   中英

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.

By default GAE included javax.mail packages in their sdk but they not included IMAP protocol packages.

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.

so what is the best way to use IMAP in Java version of app engine ??

I recently needed to send email from AppEngine using IMAP and this worked for me. 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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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