The JavaMail API defines classes which represent the components of a mail system. JavaMail does not implement an email server, instead it allows you to access an email server using a Java API. In order to test the code presented, you must have access to an email server. While the JavaMail API specification does not mandate support for specific protocols, JavaMail typically includes support for POP3, IMAP, and SMTP.
- Have access to an SMTP server. You must know the host name, port number, and security settings for your SMTP server. Web mail providers may offer SMTP access, view your email account settings or help to find further information. Be aware that your username is often your full email address and not just the name that comes before the @ symbol.
- A Java EE IDE and Application Server such as GlassFish or Oracle WebLogic Server. JavaMail can be downloaded as a library in a Java SE application but this tutorial assumes the use of a Java EE application server which would already include JavaMail.
There are following three steps to send email using JavaMail. They are as follows:
Get the session object –
javax.mail.Session class provides object of session, Session.getDefaultInstance() method and Session.getInstance() method.
// Setup mail server properties.setProperty("mail.smtp.host", host); // mail username and password properties.setProperty("mail.user", "user"); properties.setProperty("mail.password", "password$$");
- Compose the message–
javax.mail.Transport class provides method to send the message.
// javax.mail.internet.MimeMessage class is // mostly used for abstraction. MimeMessage message = new MimeMessage(session); // header field of the header. message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject("subject"); message.setText("Hello, aas is sending email ");
- Send the message –
Following is the Send Mail in Java using SMTP without authentication full implementation in java-
Yo it has been sent...
The program is simple to understand and works well, but in real life most of the SMTP servers use some sort of authentication such as TLS or SSL authentication. So, we will now see how to create Session object for these authentication protocols.
For TLS & SSL you can to know port in which the mail server running those service. I will provide you code taking gmail into consideration.
Following is the Send Mail in Java using SMTP with TLS authentication full implementation in java-
Following is the Send Mail in Java using SMTP with SSL authentication full implementation in java-
For multiple client
We can have following changes in above code
- https://apexapps.oracle.com/pls/apex/f?p=44785:24:17406092454970:::24:P24_CONTENT_ID, P24_PREV_PAGE:6316, 118
- Sending a Text Message Over the Phone Using SmsManager in Android
- User authentication using Firebase in Android
- Send email using Java Program
- Check if email address valid or not in Java
- How to send an Email from your Android App?
- Traverse through a HashSet in Java
- Implementation of Materialized view through the Java Program
- Traverse through a HashMap in Java
- How to open dialer in Android through Intent
- Android | How to open Camera through Intent and display captured image
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.Lang.Float class in Java
- Java.io.BufferedInputStream class in Java
- Java.io.ObjectInputStream Class in Java | Set 1
- Java.util.BitSet class in Java with Examples | Set 1
- Java.io.File Class in Java
- Java.io.BufferedWriter class methods in Java
- Java.io.DataOutputStream in Java
- Java.io.StreamTokenizer Class in Java | Set 1
- Java.io.InputStream Class in Java
This article is contributed by Akash Sharan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.