Advertisement
Guest User

Untitled

a guest
May 14th, 2018
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. package org.ite.recruting;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.util.*;
  7. import javax.mail.*;
  8. import javax.mail.internet.*;
  9. public class EmailSend {
  10.  
  11. public static void main(String args[]){
  12.  
  13. String receiverEmail ="kingsleychukwumezie@gmail.com" ;
  14. String message="Hello. How are u";
  15. sendEmail(receiverEmail,message);
  16.  
  17.  
  18. }
  19. public static void sendEmail(String receiver, String message){
  20. try{
  21. String host ="smtp.gmail.com" ;
  22. String user = "ivanantov80@gmail.com";
  23. String pass = "pass";
  24. String to = "kingsleychukwumezie@gmail.com";
  25. receiver=to;
  26. String from = "ivanantov80@gmail.com";
  27. String subject = "hmhm";
  28. String messageText = message;
  29. boolean sessionDebug = false;
  30.  
  31. // Properties props = System.getProperties();
  32.  
  33. Properties props = new Properties();
  34. InputStream input = null;
  35.  
  36.  
  37.  
  38. input = new FileInputStream("D:\\Ivan\\Desktop\\Camunda\\recruting\\src\\main\\resources\\META-INF\\email.properties");
  39.  
  40. // load a properties file
  41. props.load(input);
  42.  
  43. // get the property value and print it out
  44. //System.out.println(prop.getProperty("database"));
  45. //System.out.println(prop.getProperty("dbuser"));
  46. //System.out.println(prop.getProperty("dbpassword"));
  47.  
  48.  
  49.  
  50. if (input != null) {
  51. try {
  52. input.close();
  53. } catch (IOException e) {
  54. e.printStackTrace();
  55. }
  56. }
  57.  
  58.  
  59. /*
  60. props.put("mail.smtp.starttls.enable", "true");
  61. props.put("mail.smtp.host", host);
  62. props.put("mail.smtp.port", "587");
  63. props.put("mail.smtp.auth", "true");
  64. props.put("mail.smtp.starttls.required", "true");
  65. */
  66.  
  67. // java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
  68. Session mailSession = Session.getDefaultInstance(props, null);
  69. mailSession.setDebug(sessionDebug);
  70. Message msg = new MimeMessage(mailSession);
  71. msg.setFrom(new InternetAddress(from));
  72. InternetAddress[] address = {new InternetAddress(to)};
  73. msg.setRecipients(Message.RecipientType.TO, address);
  74. msg.setSubject(subject); msg.setSentDate(new Date());
  75. msg.setText(messageText);
  76.  
  77. Transport transport=mailSession.getTransport("smtp");
  78. transport.connect(host, user, pass);
  79. transport.sendMessage(msg, msg.getAllRecipients());
  80. transport.close();
  81. System.out.println("message send successfully");
  82. }catch(Exception ex)
  83. {
  84. System.out.println(ex);
  85. }
  86.  
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement