public static Session getDefaultInstance(Properties props) public static Session getDefaultInstance(Properties props,Authenticator auth)Example of getDefaultInstance() method
Properties properties=new Properties(); //fill all the informations like host name etc. Session session=Session.getDefaultInstance(properties,null);2.Syntax of getInstance() method
public static Session getInstance(Properties props) public static Session getInstance(Properties props,Authenticator auth)Example of getDefaultInstance() method
Properties properties=new Properties(); //fill all the informations like host name etc. Session session=Session.getInstance(properties,null);
MimeMessage message=new MimeMessage(session);
MimeMessage message=new MimeMessage(session);
message.setFrom(new InternetAddress("dineshonjava@gmail.com"));
message.addRecipient(Message.RecipientType.To,
new InternetAddress("admin@dineshonjava.com"));
message.setHeader("Wecome to JAVAMail Tutorial");
message.setText("Hi Users, This is java mail tutorial...");
Transport.send(message);
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendEmail {
public static void main(String[] args) {
// Recipient's email ID needs to be mentioned.
String to = "dinesh.mca.jss@gmail.com";
// Sender's email ID needs to be mentioned
String from = "dineshonjava@gmail.com";
final String username = "dineshonjava";//change accordingly
final String password = "*****";//change accordingly
// Assuming you are sending email through relay.jangosmtp.net
String host = "smtp.gmail.com";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
// Get the Session object.
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
// Create a default MimeMessage object.
Message message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
// Set Subject: header field
message.setSubject("Testing Subject");
// Now set the actual message
message.setText("Hello, this is sample for to check send " +
"email using JavaMailAPI ");
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}


Labels: JavaMail