Advertisement
Guest User

JavaMailSender.java

a guest
Feb 18th, 2020
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.91 KB | None | 0 0
  1. //JavaMailSender.java
  2. package com.Velo.EmailService;
  3.  
  4. import javax.mail.*;
  5. import javax.mail.internet.InternetAddress;
  6. import javax.mail.internet.MimeMessage;
  7. import java.util.Properties;
  8.  
  9. public class JavaMailSender {
  10.  
  11.     public static void main(String[] args) {
  12.         String host="→myemailaddress@gmail.com←";  //← my email address
  13.         final String user="→myemailaddress@gmail.com←";//← my email address
  14.         final String password="→mypassword←";//change accordingly   //← my email password
  15.  
  16.         String to="→Destination-emailaddress@gmail.com←";//→ the EMAIL i want to send TO →
  17.  
  18.         // session object
  19.         Properties props = new Properties();
  20.         props.put("mail.smtp.ssl.trust", "*");
  21.         props.put("mail.smtp.auth", "true");
  22.         props.put("mail.smtp.port", "587");
  23.         props.put("mail.smtp.host", "smtp.gmail.com");
  24.         props.put("mail.smtp.starttls.enable", "true");
  25.  
  26.         Session session = Session.getDefaultInstance(props,
  27.                 new javax.mail.Authenticator() {
  28.                     protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
  29.                         return new PasswordAuthentication(user,password);
  30.                     }
  31.                 });
  32.  
  33.         //My message :
  34.         try {
  35.             MimeMessage message = new MimeMessage(session);
  36.             message.setFrom(new InternetAddress(user));
  37.             message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
  38.             message.setSubject(" NOTIFICATION APPOINTEMENTT !!! ");
  39.             //Text in emial :
  40.             //message.setText("  → Text message for Your Appointement ← ");
  41.        
  42.             //send the message
  43.             Transport.send(message);
  44.  
  45.             System.out.println("message sent successfully via mail ... !!! ");
  46.  
  47.         } catch (MessagingException e) {e.printStackTrace();}
  48.  
  49.     }
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement