Guest User

Untitled

a guest
Jan 12th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. package com.spring.mail.service.impl;
  2.  
  3. import com.spring.mail.service.MailService;
  4. import org.springframework.stereotype.Service;
  5. import org.springframework.web.bind.annotation.RequestBody;
  6.  
  7. import javax.activation.DataHandler;
  8. import javax.activation.DataSource;
  9. import javax.activation.FileDataSource;
  10. import javax.mail.BodyPart;
  11. import javax.mail.Multipart;
  12. import javax.mail.Session;
  13. import javax.mail.Transport;
  14. import javax.mail.internet.*;
  15. import java.io.UnsupportedEncodingException;
  16. import java.util.Properties;
  17.  
  18. @Service
  19. public class MailServiceImpl implements MailService {
  20.  
  21. @Override
  22. public Object sendMail(@RequestBody String to) {
  23.  
  24. // Set required configs
  25. String from = "from_mail@gmail.com";
  26. String host = "smtp.gmail.com";
  27. String port = "587";
  28. String user = "from_mail_username@gmail.com";
  29. String password = "from_mail_password";
  30.  
  31. // Set system properties
  32. Properties properties = System.getProperties();
  33. properties.put("mail.smtp.auth", "true");
  34. properties.setProperty("mail.smtp.host", host);
  35. properties.setProperty("mail.smtp.port", port);
  36. properties.setProperty("mail.smtp.user", user);
  37. properties.setProperty("mail.smtp.password", password);
  38. properties.setProperty("mail.smtp.starttls.enable", "true");
  39.  
  40. // Get the default Session object.
  41. Session session = Session.getDefaultInstance(properties);
  42.  
  43. try {
  44. // Create a default MimeMessage object.
  45. MimeMessage message = new MimeMessage(session);
  46. // Set from email address
  47. message.setFrom(new InternetAddress(from, "TechPool"));
  48. // Set the recipient email address
  49. message.addRecipient(MimeMessage.RecipientType.TO, new InternetAddress(to));
  50. // Set email subject
  51. message.setSubject("Mail Subject");
  52. // Initiate body of email address
  53. BodyPart messageBodyPart = new MimeBodyPart();
  54. // Set email body
  55. messageBodyPart.setText("This is a message from TechPool");
  56. // Initiate class to send media
  57. Multipart multipart = new MimeMultipart();
  58. multipart.addBodyPart(messageBodyPart);
  59. messageBodyPart = new MimeBodyPart();
  60. // Set path for attachment
  61. String filename = "ubuntu.jpg";
  62. // Bind the attachment
  63. DataSource source = new FileDataSource(filename);
  64. messageBodyPart.setDataHandler(new DataHandler(source));
  65. // Set file name into email
  66. messageBodyPart.setFileName(filename);
  67. multipart.addBodyPart(messageBodyPart);
  68. message.setContent(multipart);
  69. // Set configs for sending email
  70. Transport transport = session.getTransport("smtp");
  71. transport.connect(host, from, password);
  72. // Send email
  73. transport.sendMessage(message, message.getAllRecipients());
  74. transport.close();
  75. System.out.println("done");
  76. return "Email Sent! Check Inbox!";
  77.  
  78. } catch (UnsupportedEncodingException e) {
  79. e.printStackTrace();
  80. } catch (AddressException e) {
  81. e.printStackTrace();
  82. } catch (javax.mail.MessagingException e) {
  83. e.printStackTrace();
  84. }
  85. return null;
  86. }
  87. }
Add Comment
Please, Sign In to add comment