Advertisement
Guest User

EmailSend

a guest
May 9th, 2018
556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. package org.ite.recruting;
  2.  
  3. import java.util.*;
  4. import javax.mail.*;
  5. import javax.mail.internet.*;
  6. public class EmailSend {
  7.  
  8. public static void main(String args[]){
  9.  
  10. String receiverEmail ="kingsleychukwumezie@gmail.com" ;
  11. String message="Hello. How are u";
  12. sendEmail(receiverEmail,message);
  13.  
  14.  
  15. }
  16. public static void sendEmail(String receiver, String message){
  17. try{
  18. String host ="smtp.gmail.com" ;
  19. String user = "ivanantov80@gmail.com";
  20. String pass = "ivanantov1234@";
  21. String to = "kingsleychukwumezie@gmail.com";
  22. receiver=to;
  23. String from = "ivanantov80@gmail.com";
  24. String subject = "hmhm";
  25. String messageText = message;
  26. boolean sessionDebug = false;
  27.  
  28. Properties props = System.getProperties();
  29.  
  30. props.put("mail.smtp.starttls.enable", "true");
  31. props.put("mail.smtp.host", host);
  32. props.put("mail.smtp.port", "587");
  33. props.put("mail.smtp.auth", "true");
  34. props.put("mail.smtp.starttls.required", "true");
  35.  
  36. // java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
  37. Session mailSession = Session.getDefaultInstance(props, null);
  38. mailSession.setDebug(sessionDebug);
  39. Message msg = new MimeMessage(mailSession);
  40. msg.setFrom(new InternetAddress(from));
  41. InternetAddress[] address = {new InternetAddress(to)};
  42. msg.setRecipients(Message.RecipientType.TO, address);
  43. msg.setSubject(subject); msg.setSentDate(new Date());
  44. msg.setText(messageText);
  45.  
  46. Transport transport=mailSession.getTransport("smtp");
  47. transport.connect(host, user, pass);
  48. transport.sendMessage(msg, msg.getAllRecipients());
  49. transport.close();
  50. System.out.println("message send successfully");
  51. }catch(Exception ex)
  52. {
  53. System.out.println(ex);
  54. }
  55.  
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement