Guest User

Untitled

a guest
Jan 26th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. public class EmailUtility {
  2. public static void sendEmail(String host, String port,
  3. final String userName, final String password, String toAddress,
  4. String subject, String message) throws AddressException,
  5. MessagingException {
  6.  
  7. // sets SMTP
  8. Properties properties = new Properties();
  9. properties.put("mail.smtp.host", host);
  10. properties.put("mail.smtp.port", port);
  11. properties.put("mail.smtp.auth", "true");
  12. properties.put("mail.smtp.starttls.enable", "true");
  13.  
  14. // autenticador
  15. Authenticator auth = new Authenticator() {
  16. public PasswordAuthentication getPasswordAuthentication() {
  17. return new PasswordAuthentication(userName, password);
  18. }
  19. };
  20.  
  21. Session session = Session.getInstance(properties, auth);
  22.  
  23. // creando el mensaje de e-mail
  24. Message msg = new MimeMessage(session);
  25.  
  26. msg.setFrom(new InternetAddress(userName));
  27. InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
  28. msg.setRecipients(Message.RecipientType.TO, toAddresses);
  29. msg.setSubject(subject);
  30. msg.setSentDate(new Date());
  31. msg.setText(message);
  32.  
  33. // enviando el e-mail
  34. Transport.send(msg);
  35.  
  36. }
  37. }
  38.  
  39. public static void main(String[] args) {
  40. String host = "smtp.mail.yahoo.com";
  41. String port = "587";
  42. String user = "**************";
  43. String pass = "****";
  44.  
  45. String resultMessage = "";
  46. String content = "<!DOCTYPE html>n"
  47. + "<html>n"
  48. + "<head>n"
  49. + "<style>n"
  50. + "table {n"
  51. + " border-collapse: collapse;n"
  52. + " width: 100%;n"
  53. + "}n"
  54. + "n"
  55. + "th, td {n"
  56. + " text-align: left;n"
  57. + " padding: 8px;n"
  58. + "}n"
  59. + "n"
  60. + "tr:nth-child(even){background-color: #f2f2f2}n"
  61. + "n"
  62. + "th {n"
  63. + " background-color: #4CAF50;n"
  64. + " color: white;n"
  65. + "}n"
  66. + "</style>n"
  67. + "</head>n"
  68. + "<body>n"
  69. + "n"
  70. + "<h2>Colored Table Header</h2>n"
  71. + "n"
  72. + "<table>n"
  73. + " <tr>n"
  74. + " <th>Firstname</th>n"
  75. + " <th>Lastname</th>n"
  76. + " <th>---------</th>n"
  77. + " </tr>n"
  78. + " <tr>n"
  79. + " <td>Peter</td>n"
  80. + " <td>Griffin</td>n"
  81. + " <td>$100</td>n"
  82. + " </tr>n"
  83. + " <tr>n"
  84. + " <td>Lois</td>n"
  85. + " <td>Griffin</td>n"
  86. + " <td>$150</td>n"
  87. + " </tr>n"
  88. + " <tr>n"
  89. + " <td>Joe</td>n"
  90. + " <td>Swanson</td>n"
  91. + " <td>$300</td>n"
  92. + " </tr>n"
  93. + " <tr>n"
  94. + " <td>Cleveland</td>n"
  95. + " <td>Brown</td>n"
  96. + " <td>$250</td>n"
  97. + "</tr>n"
  98. + "</table>n"
  99. + "n"
  100. + "</body>n"
  101. + "</html>";
  102.  
  103. try {
  104. EmailUtility.sendEmail(host, port, user, pass, email, subject,
  105. content);
  106. resultMessage = "el email fue enviado";
  107. } catch (Exception ex) {
  108. ex.printStackTrace();
  109. resultMessage = " error: " + ex.getMessage();
  110. } finally {
  111. System.out.println("se envio el email");
  112.  
  113. }
  114. }
Add Comment
Please, Sign In to add comment