GAE不使用java使用Send Grid发送邮件

时间:2015-02-11 13:28:26

标签: java google-app-engine email sendgrid

我尝试通过将代码部署到GAE来使用SendGrid发送邮件。以下是我的代码。

private static final String SMTP_HOST_NAME = "smtp.sendgrid.net";
    private static final String SMTP_AUTH_USER = "*******";
    private static final String SMTP_AUTH_PWD = "*******";
    private static final int SMTP_PORT = 2525;

    public void sendCustomer(String userName, String toEmail, int custId) {

        try {
            Properties props = new Properties();
            props.put("mail.transport.protocol", "smtp");
            props.put("mail.smtp.auth", "true");

            Authenticator auth = new SMTPAuthenticator();
            Session mailSession = Session.getDefaultInstance(props, auth);
            Transport transport = mailSession.getTransport();


            MimeMessage message = new MimeMessage(mailSession);

            Multipart multipart = new MimeMultipart("alternative");

            // Sets up the contents of the email message
            BodyPart part1 = new MimeBodyPart();

            part1.setText("Hello "
                    + userName
                    + "\n\n\n\n"
                    + "Welcome to NotionViz. You have been registered successfully in NotionViz.");

            multipart.addBodyPart(part1);
            message.setText("UTF-8", "html");
            message.setContent(multipart);
            message.setFrom(new InternetAddress(SMTP_AUTH_USER));
            message.setSubject("Customer Registration");
            InternetAddress internetAddress = null;

            try {
                internetAddress = new InternetAddress(toEmail);
                internetAddress.validate();

            } catch (Exception e) {

                System.out.println("Not a valid email address");

            }
            message.addRecipient(Message.RecipientType.TO, internetAddress);

            InternetAddress address = new InternetAddress("cloud.spaninfotech@gmail.com");

            message.setFrom(address);

            // Sends the email
            transport.connect(SMTP_HOST_NAME, SMTP_PORT, SMTP_AUTH_USER,
                    SMTP_AUTH_PWD);
            transport.sendMessage(message,
                    message.getRecipients(Message.RecipientType.TO));
            transport.sendMessage(message,
                    message.getFrom());
            transport.close();

        } catch (Exception e) {

        }

    }

    // Authenticates to SendGrid
    class SMTPAuthenticator extends javax.mail.Authenticator {
        @Override
        public PasswordAuthentication getPasswordAuthentication() {
            String username = SMTP_AUTH_USER;
            String password = SMTP_AUTH_PWD;
            return new PasswordAuthentication(username, password);
        }
    }

此程序运行正常并在本地发送邮件。但如果我部署到GAE并检查,我没有收到电子邮件。请让我知道为什么GAE限制第三方邮件发送。

2 个答案:

答案 0 :(得分:2)

尝试更改您正在使用的端口。对于普通/ TLS连接,您可以通过端口587,25或2525命中SendGrid(如果您要使用SSL,则为465)。

SendGrid建议端口587以避免某些托管公司设置的速率限制,所以我会给出一个机会。

答案 1 :(得分:0)

在GAE套接字上受到一系列限制(请参阅套接字API documentation)。提交端口587上允许使用经过身份验证的SMTP,因此您可以按照LaCroixed建议的那样使用它。