Advertisement
Guest User

Untitled

a guest
May 12th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. package utilities;
  2.  
  3. /**
  4. * Created by Анна on 08.05.2017.
  5. */
  6.  
  7. import java.util.Properties;
  8. import javax.mail.*;
  9. import javax.mail.internet.*;
  10.  
  11. import java.util.Properties;
  12.  
  13.  
  14. /**
  15. * Created by Анна on 28.04.2017.
  16. */
  17. public class Sender {
  18. private String username;
  19. private String password;
  20. private Properties properties;
  21.  
  22. public Sender(String username, String password) {
  23. this.username = username;
  24. this.password = password;
  25. this.properties = new Properties();
  26.  
  27. properties.put("mail.smtp.auth", "true");
  28. properties.put("mail.smtp.starttls.enable", "true");
  29. properties.put("mail.smtp.host", "smtp-mail.outlook.com");
  30. properties.put("mail.smtp.port", "587");
  31. }
  32. public void send(String subject, String text, String toEmail) throws MessagingException {
  33. Session session = Session.getInstance(properties, new Authenticator() {
  34. protected PasswordAuthentication getPasswordAuthentication() {
  35. return new PasswordAuthentication(username, password);
  36. }
  37. });
  38.  
  39. Message message = new MimeMessage(session);
  40. //от кого
  41. message.setFrom(new InternetAddress(username));
  42. //кому
  43. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
  44. //Заголовок письма
  45. message.setSubject(subject);
  46. //Содержимое
  47. message.setText(text);
  48. Transport.send(message);
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement