如何通过 Google App Engine 使用 JavaMail 发送电子邮件
How to send an email using JavaMail via Google App Engine
我是 APP Engine Google 领域的新手,但我在那里有我的项目并发送电子邮件我正在使用 JavaMail API 并且运行良好,但我需要更改 "From" 字段到一个不存在的帐户或与我的个人帐户不同(我不确定是否有必要在 APP Engine 中注册我需要在 "From" 字段中出现的帐户)。我发送的电子邮件使用我在 "From" 字段中验证的帐户(这很明显,不是吗)。所以问题是这是否可能?我也从这个网站上阅读了很多关于这个问题的网站,但我仍然没有工作。
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 的配置。
您不能在 setFrom()
调用中使用不存在的电子邮件地址。
来自Sending Mail with the Mail API:
To set the message sender and recipient, use the InternetAddress class.
a. Identify the sender by calling the setFrom() method on the MimeMessage object. Optionally, you can provide a personal name as a
string in the second parameter. For more information on which email
addresses you can use as the sender address, see Who can send
mail.
并且Who can send mail阐明了发件人电子邮件地址的限制:
For security purposes, the sender address of a message must be one of
the following:
- The Gmail or Google Apps Account of the user who is currently signed in
- Any email address of the form anything@[APP_NAME].appspotmail.com or anything@[APP_ALIAS].appspotmail.com
- Any email address listed in the Cloud Platform Console under Email API Authorized Senders
All email addresses on the Email API Authorized Senders list need
to be valid Gmail or Google-hosted domain accounts. App Administrators
can add the following accounts to the list of Authorized Senders:
- Their own email address
- Any group for which they are an Owner or Manager
- Applications hosted in a Google Apps domain: noreply@[DOMAIN].com, as long as noreply@[DOMAIN].com is a valid account (user or group).
In addition, domain administrators of domains managed by Google Apps
can add any user in their domain to the list of authorized senders.
通过使用 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
我是 APP Engine Google 领域的新手,但我在那里有我的项目并发送电子邮件我正在使用 JavaMail API 并且运行良好,但我需要更改 "From" 字段到一个不存在的帐户或与我的个人帐户不同(我不确定是否有必要在 APP Engine 中注册我需要在 "From" 字段中出现的帐户)。我发送的电子邮件使用我在 "From" 字段中验证的帐户(这很明显,不是吗)。所以问题是这是否可能?我也从这个网站上阅读了很多关于这个问题的网站,但我仍然没有工作。 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 的配置。
您不能在 setFrom()
调用中使用不存在的电子邮件地址。
来自Sending Mail with the Mail API:
To set the message sender and recipient, use the InternetAddress class.
a. Identify the sender by calling the setFrom() method on the MimeMessage object. Optionally, you can provide a personal name as a string in the second parameter. For more information on which email addresses you can use as the sender address, see Who can send mail.
并且Who can send mail阐明了发件人电子邮件地址的限制:
For security purposes, the sender address of a message must be one of the following:
- The Gmail or Google Apps Account of the user who is currently signed in
- Any email address of the form anything@[APP_NAME].appspotmail.com or anything@[APP_ALIAS].appspotmail.com
- Any email address listed in the Cloud Platform Console under Email API Authorized Senders
All email addresses on the Email API Authorized Senders list need to be valid Gmail or Google-hosted domain accounts. App Administrators can add the following accounts to the list of Authorized Senders:
- Their own email address
- Any group for which they are an Owner or Manager
- Applications hosted in a Google Apps domain: noreply@[DOMAIN].com, as long as noreply@[DOMAIN].com is a valid account (user or group).
In addition, domain administrators of domains managed by Google Apps can add any user in their domain to the list of authorized senders.
通过使用 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