Advertisement
Guest User

Untitled

a guest
Jun 5th, 2018
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. * https://www.google.com/settings/security/lesssecureapps
  5. */
  6.  
  7. package SendEmail;
  8.  
  9. /**
  10. *
  11. * @author Naveen
  12. */
  13. import java.util.*;
  14. import javax.mail.*;
  15. import javax.mail.internet.*;
  16. import javax.mail.internet.MimeMessage;
  17. public class EmailSend {
  18.  
  19. public static void main(String args[]){
  20. try{
  21. String host ="smtp.gmail.com" ;
  22. String user = "your email address";
  23. String pass = "your password";
  24. String to = "receiever email ";
  25. String from = "sender email";
  26. String subject = "This is confirmation number for your expertprogramming account. Please insert this number to activate your account.";
  27. String messageText = "Your Is Test Email :";
  28. boolean sessionDebug = false;
  29.  
  30. Properties props = System.getProperties();
  31.  
  32. props.put("mail.smtp.starttls.enable", "true");
  33. props.put("mail.smtp.host", host);
  34. props.put("mail.smtp.port", "587");
  35. props.put("mail.smtp.auth", "true");
  36. props.put("mail.smtp.starttls.required", "true");
  37.  
  38. java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
  39. Session mailSession = Session.getDefaultInstance(props, null);
  40. mailSession.setDebug(sessionDebug);
  41. Message msg = new MimeMessage(mailSession);
  42. msg.setFrom(new InternetAddress(from));
  43. InternetAddress[] address = {new InternetAddress(to)};
  44. msg.setRecipients(Message.RecipientType.TO, address);
  45. msg.setSubject(subject); msg.setSentDate(new Date());
  46. msg.setText(messageText);
  47.  
  48. Transport transport=mailSession.getTransport("smtp");
  49. transport.connect(host, user, pass);
  50. transport.sendMessage(msg, msg.getAllRecipients());
  51. transport.close();
  52. System.out.println("message send successfully");
  53. }catch(Exception ex)
  54. {
  55. System.out.println(ex);
  56. }
  57.  
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement