Advertisement
Guest User

Untitled

a guest
Jun 26th, 2016
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. package email;
  2.  
  3. import java.util.Properties;
  4. import javax.mail.Message;
  5. import javax.mail.MessagingException;
  6. import javax.mail.Session;
  7. import javax.mail.Transport;
  8. import javax.mail.internet.InternetAddress;
  9. import javax.mail.internet.MimeMessage;
  10.  
  11. /**
  12. * A singleton class for sending mail messages.
  13. * @author tcolburn
  14. */
  15. public class MailService {
  16.  
  17. /**
  18. * Sends a subject and message to a recipient
  19. * @param recipient Internet address of the recipient
  20. * @param subject the subject of the message
  21. * @param message the message
  22. * @throws MessagingException
  23. */
  24. public static void sendMessage(String subject, String message) throws MessagingException {
  25.  
  26.  
  27.  
  28. if ( theService == null ) {
  29. theService = new MailService();
  30. }
  31.  
  32. MimeMessage mimeMessage = new MimeMessage(mailSession);
  33.  
  34. mimeMessage.setFrom(new InternetAddress(FROM));
  35. mimeMessage.setSender(new InternetAddress(FROM));
  36. mimeMessage.setSubject(subject);
  37. mimeMessage.setContent(message, "text/plain");
  38.  
  39. mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("delegations.corpo@gmail.com"));
  40.  
  41. Transport transport = mailSession.getTransport("smtps");
  42. transport.connect(HOST, PORT, USER, PASSWORD);
  43.  
  44. transport.sendMessage(mimeMessage, mimeMessage.getRecipients(Message.RecipientType.TO));
  45. transport.close();
  46.  
  47. }
  48.  
  49. private MailService() {
  50. Properties props = new Properties();
  51.  
  52. props.put("mail.transport.protocol", "smtps");
  53. props.put("mail.smtps.host", HOST);
  54. props.put("mail.smtps.auth", "true");
  55. props.put("mail.smtp.from", FROM);
  56. props.put("mail.smtps.quitwait", "false");
  57.  
  58. mailSession = Session.getDefaultInstance(props);
  59. mailSession.setDebug(true);
  60. }
  61. private static MailService theService = null;
  62.  
  63. private static Session mailSession;
  64.  
  65. private static final String HOST = "poczta.o2.pl";
  66. private static final int PORT = 465;
  67. private static final String USER = "kowalskijan5151"; // Must be valid user in d.umn.edu domain, e.g. "smit0012"
  68. private static final String PASSWORD = "kowalskijan5151"; // Must be valid password for smit0012
  69. private static final String FROM = "PAI PROJEKT<kowalskijan5151@o2.pl>"; // Full info for user, e.g. "Fred Smith <smit0012@d.umn.edu>"
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement