package firstmail; import java.util.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; public class sendhtml { public static void main(String [] args) { String host="9saireddy@gmail.com";//change accordingly String to="9saireddy@gmail.com";//change accordingly final String user="9saireddy@gmail.com";//change accordingly final String password="*******";//change accordingly Properties props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "465"); Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user,password); } }); try{ MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(user)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject("HTML Message"); message.setContent("<h1>This message has been sent from splessons.com</h1>","text/html" ); Transport.send(message); System.out.println("Message has been Sent.."); }catch (MessagingException ex) {ex.printStackTrace();} } }
Output
When compile the program following is the message will be displayed on the console.
Message has been Sent..
Now check the mail.