Google AppEngine - sending e-mail in Java
Here is a sample Java code snippet to send a text e-mail from Google AppEngine Java application.
I used this code successfully somewhere in 2012.
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailExample {
public void sendMail() {
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
String messageBody = "body";
String messageSubject = "Subject";
try {
Message msg = new MimeMessage(session);
// I think sender address must match the user that runs the Google AppEngine application
InternetAddress sender = new InternetAddress("sender@example.com", "John Smith");
InternetAddress receiver = new InternetAddress("receiver@example.com", "Receiver Smith");
InternetAddress receiverCC = new InternetAddress("cc@example.com", "Cc Smith");
msg.setFrom(sender);
msg.addRecipients(RecipientType.CC, new InternetAddress[] {receiverCC});
msg.addRecipient(Message.RecipientType.TO, receiver);
msg.setSubject(messageSubject);
msg.setText(messageBody);
Transport.send(msg);
} catch (MessagingException e) {
throw new RuntimeException("Networkconnection problem", e);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Invalid e-mail address", e);
}
}
}