JavaMail API – Sending Email from JAVA via any SMTP Server example
There are various ways to send email using JavaMail API. For this purpose, you must have SMTP server that is responsible to send mails.
You can use one of the following techniques to get the SMTP server: SMTP servers provides host, port, user/password to send emails.
- Install and use any SMTP server such as Postcast server, Apache James server, cmail server etc.
- Use the SMTP server provided by the host provider e.g. my SMTP server is mail.gopaldas.org
- Use the SMTP Server provided by other companies e.g. gmail etc.
Here, we are going to learn how to send email using javamail API. But we should learn the basic steps to send email from java application.
There are following three steps to send email using JavaMail. They are as follows:
- Get the session object that stores all the information of host like host name, username, password etc.
- compose the message
- send the message
There are two methods to get the session object by using the getDefaultInstance() method and getInstance().
public static Session getDefaultInstance(Properties props)
public static Session getDefaultInstance(Properties props,Authenticator auth)
public static Session getInstance(Properties props)
public static Session getInstance(Properties props,Authenticator auth)
Properties properties=new Properties();
//fill all the informations like host name etc.
Session session=Session.getInstance(properties,null);
create the message, you need to pass session object in MimeMessage class constructor.
For example:
MimeMessage message=new MimeMessage(session);
For sending the email using JavaMail API, you need to load the two jar files in your classpath –
- mail.jar
- activation.jar
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import java.util.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; public class SendEmail { public static void main(String [] args){ String to = "gopaldas.org@gmail.com";//change accordingly String from = "gopaldas.org@gmail.com";change accordingly String host = "localhost";//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 my example of sending email from java. "); // Send message Transport.send(message); System.out.println("message sent successfully...."); }catch (MessagingException ex) {ex.printStackTrace();} } } |
Gopal Das
Latest posts by Gopal Das (see all)
- Salesforce Certified Platform Developer I – Winter ’18 Release Exam - November 23, 2017
- Salesforce Certified Platform App Builder – Winter ’18 Release Exam - November 22, 2017
- Salesforce Certified Administrator – Winter’ 18 Release Exam - November 21, 2017