Advertisement
Guest User

Untitled

a guest
Apr 4th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. package com.nura.mail;
  2.  
  3. import constants.Constants;
  4. import java.util.Properties;
  5. import javax.mail.Message;
  6. import javax.mail.MessagingException;
  7. import javax.mail.PasswordAuthentication;
  8. import javax.mail.Session;
  9. import javax.mail.Transport;
  10. import javax.mail.internet.InternetAddress;
  11. import javax.mail.internet.MimeMessage;
  12. import logger.LoggerUtil;
  13.  
  14. /**
  15. *
  16. * @author Arun kumar
  17. */
  18. public class SendMail implements Constants {
  19.  
  20. private final static LoggerUtil log = new LoggerUtil();
  21.  
  22. public static void main(String receiver, String msg, String header) {
  23.  
  24. log.addLog("Entry in SendMail");
  25. final String username = "sendmailnotification";
  26. final String password = "version5.0";
  27.  
  28. Properties props = new Properties();
  29. props.put("mail.smtp.auth", "true");
  30. props.put("mail.smtp.starttls.enable", "true");
  31. props.put("mail.smtp.host", "smtp.gmail.com");
  32. props.put("mail.smtp.port", "587");
  33.  
  34. Session session = Session.getInstance(props,
  35. new javax.mail.Authenticator() {
  36. protected PasswordAuthentication getPasswordAuthentication() {
  37. return new PasswordAuthentication(username, password);
  38. }
  39. });
  40. try {
  41. Message message = new MimeMessage(session);
  42. message.setFrom(new InternetAddress(username));
  43. message.setRecipients(Message.RecipientType.TO,
  44. InternetAddress.parse(receiver));
  45. message.setSubject(header);
  46. message.setText(msg);
  47. Transport.send(message);
  48. log.addLog("Your mail has been sent");
  49. log.addLog("Exit from Send Mail");
  50. } catch (MessagingException e) {
  51. log.addLog(e.getLocalizedMessage());
  52. }
  53. }
  54. public static void main(String[] args) {
  55. SendMail.main("aprodigalboy@gmail.com", "test", "test");
  56. System.out.println("End of the program");
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement