Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2015
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. public void enviar(){
  2. Properties props = System.getProperties();
  3. props.put("mail.smtp.starttls.enable", true); // added this line
  4. props.put("mail.smtp.host", "smtp.gmail.com");
  5. props.put("mail.smtp.user", "xxxx@gmail.com");
  6. props.put("mail.smtp.password", "xxxx");
  7. props.put("mail.smtp.port", "587");
  8. props.put("mail.smtp.auth", true);
  9.  
  10. Session session = Session.getInstance(props,null);
  11. MimeMessage message = new MimeMessage(session);
  12. // Create the email addresses involved
  13. try {
  14. InternetAddress from = new InternetAddress("xxxx@gmail.com");
  15. message.setSubject("Informe fichadas");
  16. message.setFrom(from);
  17. message.addRecipients(Message.RecipientType.TO, InternetAddress.parse("xxxx@gmail.com"));
  18.  
  19. // Create a multi-part to combine the parts
  20. Multipart multipart = new MimeMultipart("alternative");
  21.  
  22. // Create your text message part
  23. BodyPart messageBodyPart = new MimeBodyPart();
  24. messageBodyPart.setText("some text to send");
  25.  
  26. // Add the text part to the multipart
  27. multipart.addBodyPart(messageBodyPart);
  28.  
  29. // Create the html part
  30. messageBodyPart = new MimeBodyPart();
  31. String htmlMessage = "Informe fichadas";
  32. messageBodyPart.setContent(htmlMessage, "text/html");
  33. multipart.addBodyPart(messageBodyPart);
  34.  
  35. // Associate multi-part with message
  36. message.setContent(multipart);
  37.  
  38. // Send message
  39. Transport transport = session.getTransport("smtp");
  40. transport.connect("smtp.gmail.com", "user", "pass");
  41. System.out.println("Transport: "+transport.toString());
  42. transport.sendMessage(message, message.getAllRecipients());
  43.  
  44.  
  45. } catch (AddressException e) {
  46. // TODO Auto-generated catch block
  47. e.printStackTrace();
  48. } catch (MessagingException e) {
  49. // TODO Auto-generated catch block
  50. e.printStackTrace();
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement