Send an email using SMTP with Java
We required below API are :
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)
{
}
}
}
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.