I am new in the APP Engine Google world but I have my project into there and to send emails I am using JavaMail API and it's working well, but I need to change the "From" field to an non-existent account or different from my personal account (I am not sure if it's necessary register in APP Engine the account what I need to appear in the "From" field). The emails that I have sent use my account authenticated in the "From" field (which it is obvious, isn't it). So the question is if it is possible? I read many websites about this question also from this website but I still not working. Google APP engine has Gmail API in the API manager, but I am no sure if it's the same as using JavaMail API.
Some of my code that sends emails but using the authentication from my account:
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("[email protected]", "[email protected]"));
message.setReplyTo(InternetAddress.parse("[email protected]",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);
}
Updated: More specifically I need the configurations into Google App Engine.
You cannot use a non-existent email address in the setFrom()
call.
From 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.
And Who can send mail spells out the restrictions for the sender email address:
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.