SendMail Program in java

Published: 30 December 2017
on channel: Creative Technology
690
7

Program:

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class SendMail {

public static void main(String[] args) {
try{
String host ="smtp.gmail.com" ;
String user = "dubalmahesh8@gmail.com"; //Enter Your Gmail Username
String pass = "maheshdubal8"; //Enter Your Gmail Password
String to = "dubalmahesh8@gmail.com";
String from = "dubalmahesh8@gmail.com";
String subject = "Email Demo:";
String messageText = "Hello";
boolean sessionDebug = false;

Properties props = System.getProperties();

props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.required", "true");

java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(sessionDebug);
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject); msg.setSentDate(new Date());
msg.setText(messageText);

Transport transport=mailSession.getTransport("smtp");
transport.connect(host, user, pass);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
System.out.println("message send successfully");
}catch(Exception ex)
{
System.out.println(ex);
}
}

}



Jar File Download:
01)Activation Jar
http://www.java2s.com/Code/Jar/a/Down...
02)javax.mail-1.4.4.jar
http://www.java2s.com/Code/Jar/j/Down...


On this page of the site you can watch the video online SendMail Program in java with a duration of hours minute second in good quality, which was uploaded by the user Creative Technology 30 December 2017, share the link with friends and acquaintances, this video has already been watched 690 times on youtube and it was liked by 7 viewers. Enjoy your viewing!