Guest User

Untitled

a guest
Jan 10th, 2018
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  2. throws ServletException, IOException {
  3. //processRequest(request, response);
  4.  
  5.  
  6. String login = request.getParameter("login");
  7. String senha = request.getParameter("senha");
  8.  
  9. String email = request.getParameter("email");
  10.  
  11. String assunto = "Cadastro de dados do EccomeceJSP2";
  12. EnviarEmailGmail gm = new EnviarEmailGmail();
  13. gm.enviarGmail(email, assunto, login, senha);
  14.  
  15. public class EnviarEmailGmail {
  16.  
  17. static Session session;
  18.  
  19. public void enviarGmail(String email, String assunto, String login, String senha){
  20. try {
  21.  
  22. final String username = "meuemail@gmail.com";
  23. final String password = "minhasenha";
  24.  
  25. Properties props = new Properties();
  26. props.put("mail.smtp.auth", "true");
  27. //props.put("mail.smtp.starttls.enable", "true");
  28. props.put("mail.smtp.host", "smtp.gmail.com");
  29. props.put("mail.smtp.port", "587");
  30. props.put("mail.smtp.socketFactory.port", "465");
  31. props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
  32. props.put("mail.smtp.socketFactory.fallback", "false");
  33.  
  34. session = Session.getInstance(props,
  35. new javax.mail.Authenticator() {
  36. protected PasswordAuthentication getPasswordAuthentication() {
  37. return new PasswordAuthentication(username, password);
  38. }
  39. });
  40.  
  41. Message message = new MimeMessage(session);
  42. message.setFrom(new InternetAddress("meuemail@gmail.com"));
  43. message.setRecipients(Message.RecipientType.TO,
  44. InternetAddress.parse(email));
  45. message.setSubject(assunto);
  46. //message.setText("Seu cadastro foi realizado com sucesso e seu login e senha será <br> " + "Login:" + login + "Senha:" + senha);
  47.  
  48.  
  49. MimeMultipart multipart2 = new MimeMultipart("related");
  50.  
  51. // first part (the html)
  52. BodyPart messageBodyPart = new MimeBodyPart();
  53. String htmlText = "<H3>Seu cadastro foi realizado com sucesso e seu login e senha será:</H3><br><b>Login:+login+</b><br><b>Senha:+senha+</b><br><br><img src="cid:image">";
  54. messageBodyPart.setContent(htmlText, "text/html");
  55.  
  56. // add it
  57. multipart2.addBodyPart(messageBodyPart);
  58.  
  59. // second part (the image)
  60. messageBodyPart = new MimeBodyPart();
  61. DataSource fds = new FileDataSource
  62. ("C:\imagens\eccomerce.JPG");
  63. messageBodyPart.setDataHandler(new DataHandler(fds));
  64. messageBodyPart.setHeader("Content-ID","<image>");
  65.  
  66. // add it
  67. multipart2.addBodyPart(messageBodyPart);
  68.  
  69. // put everything together
  70. message.setContent(multipart2);
  71.  
  72.  
  73. Transport.send(message);
  74.  
  75. System.out.println("Done");
  76.  
  77. } catch (MessagingException e) {
  78. throw new RuntimeException(e);
  79. }
  80. }
  81.  
  82. }
Add Comment
Please, Sign In to add comment