Integrating JavaMail with Email Delivery
Use JavaMail to send emails through the Email Delivery service.
JavaMail provides a platform-independent and protocol-independent framework to build mail and messaging applications. Before you use JavaMail, you must configure Email Delivery and take note of your SMTP sending information and SMTP credentials. This guide uses the Eclipse IDE and the JavaMail API to send email through Email Delivery.
These instructions contain sample code for your convenience and should be used as a reference. For client support, you must contact JavaMail customer support. These steps were tested on an Oracle Linux Server release 7.9 compute instance and Java 8 and 11. Java applications (including JavaMail) must be updated to the latest version to ensure that the latest protocols, ciphers, and security patches are in compliance with Oracle's supported security policies and ciphers.
Configure JavaMail to Send Email Through Email Delivery
To enable JavaMail to test the configuration of Email Delivery:
-
Ensure Email Delivery is configured to send email. See Getting Started.
Note
The SMTP credentials are required to configure JavaMail to use Email Delivery. Be sure to note the user name and password when you generate the SMTP credentials.
- Open a browser and go to https://github.com/javaee/javamail/releases.
- Under Downloads, select javax.mail.jar to download the latest version of JavaMail.
- Create a project in Eclipse by performing the following steps:
- In Eclipse, open the File menu. Select New, and then click Java Project.
- In the Create a Java Project dialog box, enter a project name, and then click Next.
- In the Java Settings dialog box, select the Libraries tab.
- Click Add External JARs.
- In the JAR Selection dialog box, browse to the folder in which you downloaded JavaMail. Select the javax.mail.jar file, and then click Open.
- In the Java Settings dialog box, click Finish.
- In Eclipse, in the Package Explorer window, expand your project.
- Under your project, right-click the src directory, select New, and then click Class.
- In the New Java Class dialog box, enter "OCIemail" in the Name field and then click Finish.
-
Enter the following code in OCIemail.java to send a test email with JavaMail:
import java.util.Properties; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class OCIemail { // Replace FROM with your "From" address. // This address must be added to Approved Senders in the console. static final String FROM = "<sender email address>"; static final String FROMNAME = "<sender name>"; // Replace TO with a recipient address. static final String TO = "<recipient email address>"; // Replace smtp_username with your Oracle Cloud Infrastructure SMTP username generated in console. static final String SMTP_USERNAME = "<username OCID from SMTP credentials>"; // Replace smtp_password with your Oracle Cloud Infrastructure SMTP password generated in console. static final String SMTP_PASSWORD = "<SMTP password>"; // Oracle Cloud Infrastructure Email Delivery hostname. static final String HOST = "<SMTP endpoint>"; // The port you will connect to on the SMTP endpoint. Port 25 or 587 is allowed. static final int PORT = 587; static final String SUBJECT = "<subject of your email>"; static final String BODY = String.join( System.getProperty("line.separator"), "<h1>OCI Email Delivery test</h1>", "<p>This email was sent with OCI Email Delivery using the ", "<a href='Javamail'>https://github.com/javaee/javamail'>Javamail Package</a>", " for <a href='Javahttps://www.java.com'>Java</a>." ); public static void main(String[] args) throws Exception { // Create a Properties object to contain connection configuration information. Properties props = System.getProperties(); props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.port", PORT); //props.put("mail.smtp.ssl.enable", "true"); //the default value is false if not set props.put("mail.smtp.auth", "true"); props.put("mail.smtp.auth.login.disable", "true"); //the default authorization order is "LOGIN PLAIN DIGEST-MD5 NTLM". 'LOGIN' must be disabled since Email Delivery authorizes as 'PLAIN' props.put("mail.smtp.starttls.enable", "true"); //TLSv1.2 is required props.put("mail.smtp.starttls.required", "true"); //Oracle Cloud Infrastructure required // Create a Session object to represent a mail session with the specified properties. Session session = Session.getDefaultInstance(props); // Create a message with the specified information. MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(FROM,FROMNAME)); msg.setRecipient(Message.RecipientType.TO, new InternetAddress(TO)); msg.setSubject(SUBJECT); msg.setContent(BODY,"text/html"); // Create a transport. Transport transport = session.getTransport(); // Send the message. try { System.out.println("Sending Email now...standby..."); // Connect to OCI Email Delivery using the SMTP credentials specified. transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD); // Send email. transport.sendMessage(msg, msg.getAllRecipients()); System.out.println("Email sent!"); } catch (Exception ex) { System.out.println("The email was not sent."); System.out.println("Error message: " + ex.getMessage()); } finally { // Close & terminate the connection. transport.close(); } } }
-
In the OCIemail.java file, replace the following with your own values:
Note
Email addresses are case-sensitive. Ensure that the addresses are the same as the ones you entered in Approved Senders in the console.
- FROM - Replace with your sender email address. This email address must be added to the Approved Senders list in Email Delivery first.
- TO - Replace with your recipient email address.
- SMTP credentials - Replace smtp_username and smtp_password with your Oracle Cloud Infrastructure SMTP username and password generated in the console.
- HOST - Replace with the Email Delivery SMTP endpoint. For example, smtp.us-ashburn-1.oraclecloud.com.
-
Refer to the requirements for configuring SMTP connection with Email Delivery. TLSv1.2 is required for Email Delivery. Some default settings of Javamail need to be disabled. For example, JavaMail authorizes in a certain order. The default authorization order is "LOGIN PLAIN DIGEST-MD5 NTLM". Since Email Delivery authorizes as "PLAIN", "LOGIN" needs to be disabled. For example, the following code is entered in OCIemail.java file to configure the SMTP connection:
props.put("mail.smtp.auth", "true"); props.put("mail.smtp.auth.login.disable", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.starttls.required", "true");
- Open the File menu and click Save.
- To build the project, open the Project menu and then select Build Project. If this option is disabled, you may have automatic building enabled.
- To start the program and send the email, open the Run menu and then click Run.
- Review the output. If the email was successfully sent, the console displays "Email sent successfully!" Otherwise, it displays an error message.
- Log into the recipient inbox to verify receipt of the email.
More Information
- See the JavaMail documentation for more information.
- There is a known issue that can cause an error. See JavaMail issues occur when many recipients are set in an email and one or more of the email addresses are suppressed.