Electronic mail is a method of exchanging messages between people using electronic devices. In this post, we’ll explain how to send an email message with Java through Platform.sh.
By default, only the master environment can send emails. For non-master environments, you can configure outgoing emails via the management console.
Emails from Platform.sh are sent via a SendGrid-based SMTP proxy. Each Platform.sh project is provisioned as a SendGrid sub-account. These SendGrid sub-accounts are capped at 12k emails per month.
Below the sample code that uses Java Mail:
import sh.platform.config.Config;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
public class JavaEmailSender {
private static final Logger LOGGER = Logger.getLogger(JavaEmailSender.class.getName());
public void send() {
Config config = new Config();
String to = "";//change accordingly
String from = "";//change accordingly
String host = config.getSmtpHost();
//or IP address
//Get the session object
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
Session session = Session.getDefaultInstance(properties);
//compose the message
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Ping");
message.setText("Hello, this is example of sending email ");
// Send message
Transport.send(message);
System.out.println("message sent successfully....");
} catch (MessagingException exp) {
exp.printStackTrace();
LOGGER.log(Level.SEVERE, "there is an error to send an message", exp);
}
}
}
There is plenty of additional l documentation about using JavaMail, like this one, that shows how to send email with HTML format and an attachment:
https://mkyong.com/java/java-how-to-send-email/