使用Google App Engine发送电子邮件

时间:2015-11-29 16:17:10

标签: java google-app-engine

我正在尝试使用以下代码通过servlet从Google App Engine发送电子邮件。 application.owner@gmail.com列在“电子邮件API授权发件人”中。我没有收到任何错误,也没有收到任何电子邮件。我可以在日志中看到日志消息,所以我很确定代码是执行的。我没有收到任何电子邮件的任何想法?

package com.myapplication;

import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SendMailServlet extends HttpServlet {

    final static Logger logger = Logger.getLogger(SendMailServlet.class.getName());

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        try {

            sendMail();

            logger.log(Level.INFO, "Send mail!");

        } catch (AddressException e) {
            logger.log(Level.WARNING, e.getMessage());
        } catch (MessagingException e) {
            logger.log(Level.WARNING, e.getMessage());
        }
    }

    private void sendMail() throws MessagingException {

        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

        Message msg = new MimeMessage(session);

        msg.setFrom(new InternetAddress("application.owner@gmail.com"));
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress("application.owner@gmail.com"));
        msg.setSubject("The subject");
        msg.setText("The message body");

        Transport.send(msg);
    }
}

0 个答案:

没有答案