· 9 years ago · Mar 03, 2017, 03:18 PM
1public static void main(String [] args) {
2 Properties properties = new Properties();
3 properties.put("mail.smtp.host", "smtp.gmail.com");
4 properties.put("mail.smtp.socketFactory.port","465");
5 properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
6 properties.put("mail.smtp.auth", "true");
7 properties.put("mail.smtp.port","465");
8 // Recipient's email ID needs to be mentioned.
9 String to = "*****@gmail.com";
10 // Sender's email ID needs to be mentioned
11 String from = "******@gmail.com";
12
13 Session session = Session.getDefaultInstance(properties,
14 new Authenticator() {
15 @Override
16 protected PasswordAuthentication getPasswordAuthentication() {
17 return new PasswordAuthentication("***@gmail.com","******");
18 }
19 });
20
21 try {
22 // Create a default MimeMessage object.
23 MimeMessage message = new MimeMessage(session);
24
25 // Set From: header field of the header.
26 message.setFrom(new InternetAddress(from));
27
28 // Set To: header field of the header.
29 message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
30
31 // Set Subject: header field
32 message.setSubject("This is the Subject Line!");
33
34 // Now set the actual message
35 message.setText("This is actual message");
36
37 // Send message
38 Transport.send(message);
39 System.out.println("Sent message successfully....");
40 }catch (MessagingException mex) {
41 mex.printStackTrace();
42 }
43}