daily pastebin goal
10%
SHARE
TWEET

Untitled

a guest Apr 27th, 2016 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import javax.mail.Message;
  2. import javax.mail.MessagingException;
  3. import javax.mail.Session;
  4. import javax.mail.Transport;
  5. import javax.mail.internet.InternetAddress;
  6. import javax.mail.internet.MimeMessage;
  7.  
  8.  
  9. public class MailDemo {
  10.     public static void main(String[] args) {
  11.         Properties properties=new Properties();
  12.         properties.put("mail.smtp.auth", "true");
  13.         properties.put("mail.smtp.starttls.enable","true");
  14.         properties.put("mail.smtp.host","smtp.gmail.com");
  15.         properties.put("mail.smtp.port", "587");
  16.         Scanner scn=new Scanner(System.in);
  17.         System.out.println("Username for Authentication :");
  18.         final String username=scn.nextLine();
  19.         System.out.println("Password for Authentication :");
  20.         final String password=scn.nextLine();
  21.         System.out.println("From Email..");
  22.         String fromEmailAddrs=scn.nextLine();
  23.         System.out.println("To Email..");
  24.         String toEmail=scn.nextLine();
  25.         System.out.println("Subject..");
  26.         String subject=scn.nextLine();
  27.         System.out.println("Message..");
  28.         String textMessage=scn.nextLine();
  29.         Session session=Session.getDefaultInstance(properties,new Authenticator() {
  30.             @Override
  31.             protected javax.mail.PasswordAuthentication getPasswordAuthentication(){
  32.                 return new javax.mail.PasswordAuthentication(username, password);
  33.             }
  34.         });
  35.         try{
  36.             Message msg=new MimeMessage(session);
  37.             msg.setFrom(new InternetAddress(fromEmailAddrs));
  38.             msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
  39.             msg.setSubject(subject);
  40.             msg.setText(textMessage);
  41.             Transport.send(msg);
  42.             System.out.println("/n Your Message Delivered Succesfully");
  43.         }
  44.         catch(MessagingException m){
  45.             throw new RuntimeException(m);
  46.         }
  47.  
  48.     }
  49. }
  50.    
  51. Username for Authentication :
  52. bijaybhaskar01
  53. Password for Authentication :
  54. abcdef@456789
  55. From Email..
  56. bijaybhaskar01@gmail.com
  57. To Email..
  58. bbswain1001@gmail.com
  59. Subject..
  60. Hello
  61. Message..
  62. hai
  63.    
  64. Exception in thread "main" java.lang.RuntimeException: javax.mail.SendFailedException: Sending failed;
  65.   nested exception is:
  66.     class javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. fh14sm3131583pab.31 - gsmtp
  67.  
  68.     at com.mail.bhaskar.MailDemo.main(MailDemo.java:52)
  69. Caused by: javax.mail.SendFailedException: Sending failed;
  70.   nested exception is:
  71.     class javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. fh14sm3131583pab.31 - gsmtp
  72.  
  73.     at javax.mail.Transport.send0(Transport.java:218)
  74.     at javax.mail.Transport.send(Transport.java:80)
  75.     at com.mail.bhaskar.MailDemo.main(MailDemo.java:48)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top