【问题标题】:How to use IMAP in Google App engine (Java)如何在 Google App 引擎 (Java) 中使用 IMAP
【发布时间】:2017-07-08 13:58:05
【问题描述】:

我们正在尝试在 Google App enigne (GAE) 上运行的网络应用中使用 IMAP,我们使用的是 Java 版本的 GAE。

默认情况下,GAE 在其 sdk 中包含 javax.mail 包,但不包含 IMAP 协议包。

所以我们尝试将java mail library 添加到我们的应用程序以获取 IMAP 包,但此库与应用程序引擎 java sdk javax.mail 包不兼容,因为此库也有 javax.mail 包,与 appengine sdk javax 相比,代码有所不同.mail 包。

那么在 Java 版本的应用引擎中使用 IMAP 的最佳方式是什么??

【问题讨论】:

    标签: java google-app-engine gmail-imap


    【解决方案1】:

    我最近需要使用 IMAP 从 AppEngine 发送电子邮件,这对我有用。 我已经添加了这个库:

        <!-- 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>
    

    这是代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-11
      • 2017-10-18
      相关资源
      最近更新 更多