Send an email using SMTP with Java

9/13/2021
All Articles

#Send an email using SMTP with Java #java #mail

Send an email using SMTP with Java

Send an email using SMTP with Java

We required below API are :

  • Java Mail API 
  • Java Activation Framework (JAF)

Above two should be installed on your machine


 import java.util.*;
 import javax.mail.*;
 import javax.activation.*;
 import javax.mail.internet.*;
 class SendHTMLEmail  //DeveloperIndian
 {
 public static void main(String []args)
 {
	String to ="developerIndian@gmail.com";
	String from="BigdataDeveloper@yahoo.com";
	String host="localhost";
	Properties properties=System.getProperties();
	properties.setProperty("mail.smpt.host",host);
	Session session=Session.getDefaultInstance(properties);
		try{ 
          MimeMessage message =new MimeMessage(session);
                message.setrecipient(Message.RecipientType.To,new InternetAddress(to));
                message.setSubject("This is the subject");
                message.setContent("<h1>this is actual message</h1>");
                Transport.send(message);
                System.out.println("sent message successfully.....");   
        }
	catch(MessageException ex)
		{
		}
 
 }
 }

In conclusion

we've seen how to use the native Java mail library to send emails even with attachment.

As always, the complete source code is available over on Github.


This Solution is provided by Shubham mishra

This article is contributed by Developer Indian team. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Also folllow our instagram , linkedIn , Facebook , twiter account for more

Article