【问题标题】:How to send an email using JavaMail via Google App Engine如何通过 Google App Engine 使用 JavaMail 发送电子邮件
【发布时间】:2016-10-19 10:59:36
【问题描述】:

我是 APP Engine Google 世界的新手,但我有我的项目并发送电子邮件我正在使用 JavaMail API,它运行良好,但我需要将“发件人”字段更改为不存在的帐户或与我的个人帐户不同(我不确定是否有必要在 APP Engine 中注册我需要出现在“发件人”字段中的帐户)。我发送的电子邮件使用我在“发件人”字段中经过身份验证的帐户(很明显,不是吗)。所以问题是这是否可能?我也从这个网站上阅读了许多关于这个问题的网站,但我仍然没有工作。 Google APP引擎在API管理器中有Gmail API,但我不确定它是否与使用JavaMail API相同。

我的一些发送电子邮件但使用来自我的帐户的身份验证的代码:

public void sendEmail(String[] recipients, String subject, String body, String username, String password) {

Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true"); //I tried disabling this but it not works
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
props.put("mail.smtp.port", "587"); //I tried with another port

//I tried without authentication from my account like this:
//Session.getDefaultInstance(props, null); 
//It not works
session = Session.getInstance(props, new Authenticator() {
     @Override
     protected PasswordAuthentication getPasswordAuthentication() {
     return new PasswordAuthentication(MailService.this.username, MailService.this.password);
    }
});

Message message = new MimeMessage(session);
// Here is the key, sending email not from authenticated account
message.setFrom(new InternetAddress("whateveraccount@example.com", "whateveraccount.engine@example.com")); 
message.setReplyTo(InternetAddress.parse("whateveraccount@example.com",false));

//Sending to multiple recipients
Address[] to = new Address[recipients.length];
for (int i=0; i<recipients.length; i++) {
    to[i] = new InternetAddress(recipients[i]);
}

message.setRecipients(Message.RecipientType.TO, to);
message.setSubject(subject);

/**
 Multi part message email
 **/

Multipart multipart = new MimeMultipart();

//body
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(body, "text/html");
multipart.addBodyPart(htmlPart);

// adds attachments
String[] attachFiles = new String[2];
attachFiles[0] = "..path to send attachment..";
attachFiles[1] = "..path to send attachment..";

if(attachFiles != null && attachFiles.length > 0){
    for (String filePath : attachFiles) {
        MimeBodyPart attachPart = new MimeBodyPart();
        try {
            attachPart.attachFile(filePath);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        multipart.addBodyPart(attachPart);
    }
}

message.setContent(multipart);
Transport.send(message);
}

更新: 更具体地说,我需要在 Google App Engine 中进行配置。

【问题讨论】:

标签: java google-app-engine jakarta-mail sendmail gmail-api


【解决方案1】:

通过使用 Sendgrid,您可以从控制台中声明的域以外的其他域发送电子邮件。

您只需要执行以下操作:

    SendGrid sendgrid = new SendGrid(Constants.SENDGRID_API_KEY);
    SendGrid.Email email = new SendGrid.Email();
    email.addTo("recipient@gmail.com");
    email.setFrom("whatever@whatever.com");
    email.setFromName("Whatever");
    email.setSubject(...);
    ....

文档非常好,从 AppEngine Mail API 切换到 Sendgrid 很简单

https://cloud.google.com/appengine/docs/java/mail/sendgrid

【讨论】:

    【解决方案2】:

    您不能在setFrom() 通话中使用不存在的电子邮件地址。

    来自Sending Mail with the Mail API

    1. 要设置消息的发送者和接收者,请使用 InternetAddress 类。

      一个。通过调用 MimeMessage 对象的 setFrom() 方法来识别发送者。或者,您可以提供个人姓名作为 第二个参数中的字符串。有关哪个电子邮件的更多信息 您可以用作发件人地址的地址,请参阅Who can send mail

    并且Who can send mail 说明了对发件人电子邮件地址的限制:

    出于安全考虑,邮件的发件人地址必须是以下地址之一 以下:

    • 当前登录用户的 Gmail 或 Google Apps 帐户
    • 任何形式为anything@[APP_NAME].appspotmail.com 或anything@[APP_ALIAS].appspotmail.com 的电子邮件地址
    • Cloud Platform Console 中Email API Authorized Senders 下列出的任何电子邮件地址

    Email API Authorized Senders 列表中的所有电子邮件地址都需要 是有效的 Gmail 或 Google 托管的域帐户。应用管理员 可以将以下帐户添加到授权发件人列表中:

    • 他们自己的电子邮件地址
    • 他们是所有者或管理者的任何组
    • 托管在 Google Apps 域中的应用程序:noreply@[DOMAIN].com,只要 noreply@[DOMAIN].com 是有效帐户(用户或组)。

    此外,由 Google Apps 管理的域的域管理员 可以将其域中的任何用户添加到授权发件人列表中。

    【讨论】:

    • 是的,我明白了。您知道我可以在 App Engine 配置的哪个部分注册该地址或域吗?在那之后需要改变哪些方法? .例如:setFrom(new InternetAddress("anything@app_name.com");
    • Settings -> Application settings 部分,如果您单击文档中的“Email API 授权发件人”链接 (console.cloud.google.com/project/_/appengine/settings),您将进入该部分
    猜你喜欢
    • 2016-10-27
    • 1970-01-01
    • 2010-10-27
    • 1970-01-01
    • 2012-04-19
    • 2020-10-02
    • 1970-01-01
    • 2016-06-08
    • 2011-03-25
    相关资源
    最近更新 更多