如何通过Google App Engine使用JavaMail发送电子邮件

时间:2016-06-17 14:41:03

标签: java google-app-engine javamail sendmail gmail-api

我是APP Engine Google世界的新手,但我有我的项目并发送电子邮件我正在使用JavaMail API并且它运行良好,但我需要将“发件人”字段更改为不存在的帐户或与我的个人账户不同(我不确定是否需要在APP引擎中注册我需要在“发件人”字段中显示的内容)。我发送的电子邮件使用我在“发件人”字段中验证过的帐户(很明显,不是这样)。所以问题是它是否可能?我也从这个网站上阅读了许多关于这个问题的网站,但我仍然没有工作。 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中。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您无法在setFrom()来电中使用不存在的电子邮件地址。

来自Sending Mail with the Mail API

  
      
  1. 要设置邮件发件人和收件人,请使用InternetAddress类。

         

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

  2.   

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

  

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

     
      
  • 当前已登录的用户的Gmail或Google Apps帐户
  •   
  • @ [APP_NAME] .appspotmail.com或任何@ [APP_ALIAS] .appspotmail.com
  • 表格的任何电子邮件地址   
  • Email API Authorized Senders
  • 下的云平台控制台中列出的任何电子邮件地址   
     

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

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

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

相关问题