SHARE
TWEET

Untitled

a guest Aug 30th, 2015 126 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public static void main (String[] args) throws Exception{
  2.                 //intialize logger
  3. protected static Logger logger = LogManager.getLogger(JavaEmail.class.getName());
  4.  
  5. //smtp related parameters
  6. private static String smtpUseremail;
  7. private static String smtpReceiverEmailAddress;
  8. private static String smtpUserpassword;
  9. private static String smtpPortnumber;
  10. private static String smtpHost;
  11.  
  12. private static String emailSubject;
  13.  
  14. //stack error message variable
  15. private static String message;
  16.  
  17.         try {
  18.  
  19.             logger.info("-----------------------------");
  20.             logger.info("---------Starting up---------");
  21.             logger.info("-----------------------------");
  22.  
  23.             initialize();
  24.  
  25.             String body = "This is a test";
  26.  
  27.             logger.info("Start to connect to email server");
  28.  
  29.             sendFromGmail(smtpUseremail, smtpUserpassword, smtpReceiverEmailAddress, emailSubject, body);
  30.  
  31.  
  32.  
  33.            // System.out.println("error message: " + message);
  34.  
  35.             logger.info("Email Sent");
  36.  
  37.  
  38.         }
  39.         catch (Exception e){
  40.             logger.error(e);
  41.  
  42.             // message = e.toString();
  43.  
  44.             throw e;
  45.  
  46.  
  47.         }
  48.     }
  49.  
  50.     //Setting up Email Server
  51.     private static void sendFromGmail(String from, String pass, String to, String subject, String body) {
  52.  
  53.  
  54.         Properties props = PropertiesUtil.loadProperties("main.properties");
  55.  
  56.         props.put("mail.smtp.auth", "true");
  57.         props.put("mail.smtp.starttls.enable", "true");
  58.         props.put("mail.smtp.host", smtpHost);
  59.         props.put("mail.smtp.port", smtpPortnumber);
  60.  
  61.         Session session = Session.getInstance(props,
  62.                   new javax.mail.Authenticator() {
  63.                     protected PasswordAuthentication getPasswordAuthentication() {
  64.                         return new PasswordAuthentication(smtpUseremail, smtpUserpassword);
  65.                     }
  66.                   });
  67.  
  68.         MimeMessage message = new MimeMessage(session);
  69.  
  70.  
  71.  
  72.         try{
  73.  
  74.  
  75.             message.setFrom(new InternetAddress(from));
  76.  
  77.             message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
  78.  
  79.  
  80.             message.setSubject(subject);
  81.  
  82.             message.setText(body);
  83.  
  84.             Transport.send(message);
  85.  
  86.             logger.info("Message has been delievered, check your mail");
  87.  
  88.         }
  89.  
  90.         catch (MessagingException e) {
  91.             logger.error("Messaging Exception");
  92.  
  93.             throw new RuntimeException(e);
  94.         }
  95.     }
  96.  
  97.  
  98.     //Initializing Properties
  99.     private static void initialize() throws Exception  {
  100.  
  101.         Properties props = PropertiesUtil.loadProperties("main.properties");
  102.  
  103.         //smtp set up
  104.         smtpPortnumber = props.getProperty("portNumber");
  105.         smtpReceiverEmailAddress = props.getProperty("recipientEmailAddress");
  106.         smtpUseremail = props.getProperty("userEmailAddress");
  107.         smtpUserpassword = props.getProperty("userPassword");
  108.         smtpHost = props.getProperty("emailHost");
  109.         emailSubject = props.getProperty("");
  110.  
  111.     }
  112.  
  113. }
  114.        
  115. portNumber = 465
  116. recipientEmailAddress = my@gmail.com
  117. userPassword = secert
  118. userEmailAddress = my@gmail.com
  119. emailHost = smtp.gmail.com
  120.        
  121. Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbvWE
  122.     534-5.7.14 HIW4hBxefM-JI-wxqEK6ru2lJstC10IedXvhU_Tzbft1IqPFsot0wfjAWkrX2-gLIEG0NZ
  123.     534-5.7.14 V45KWaeagzWTb_v1nAe-BOZSR6YzPHQ7-cYVGYplvcaexmFziL3IMRgkCalmn-5FjreRkm
  124.     534-5.7.14 PjtRPd9nmjcosX0ce4DXSGn_y-qGOsW-ehOp2U3eQkSReObXi-a80ywNWiy3QuiV56f61h
  125.     534-5.7.14 yz8bmLpugqIYhh7DiNd6HpwvNHAo> Please log in via your web browser and
  126.     534-5.7.14 then try again.
  127.     534-5.7.14  Learn more at
  128.     534 5.7.14  https://support.google.com/mail/answer/78754 ob15sm12759947pdb.52 - gsmtp
  129.  
  130.         at emailNotification.JavaEmail.sendFromGmail(JavaEmail.java:140)
  131.         at emailNotification.JavaEmail.main(JavaEmail.java:63)
  132.     Caused by: javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbvWE
  133.     534-5.7.14 HIW4hBxefM-JI-wxqEK6ru2lJstC10IedXvhU_Tzbft1IqPFsot0wfjAWkrX2-gLIEG0NZ
  134.     534-5.7.14 V45KWaeagzWTb_v1nAe-BOZSR6YzPHQ7-cYVGYplvcaexmFziL3IMRgkCalmn-5FjreRkm
  135.     534-5.7.14 PjtRPd9nmjcosX0ce4DXSGn_y-qGOsW-ehOp2U3eQkSReObXi-a80ywNWiy3QuiV56f61h
  136.     534-5.7.14 yz8bmLpugqIYhh7DiNd6HpwvNHAo> Please log in via your web browser and
  137.     534-5.7.14 then try again.
  138.     534-5.7.14  Learn more at
  139.     534 5.7.14  https://support.google.com/mail/answer/78754 ob15sm12759947pdb.52 - gsmtp
  140.  
  141.         at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:809)
  142.         at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:752)
  143.         at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:669)
  144.         at javax.mail.Service.connect(Service.java:317)
  145.         at javax.mail.Service.connect(Service.java:176)
  146.         at javax.mail.Service.connect(Service.java:125)
  147.         at javax.mail.Transport.send0(Transport.java:194)
  148.         at javax.mail.Transport.send(Transport.java:124)
  149.         at emailNotification.JavaEmail.sendFromGmail(JavaEmail.java:130)
  150.         ... 1 more
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