Guest User

Untitled

a guest
Dec 31st, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. import java.io.ByteArrayInputStream;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. import java.security.Security;
  6. import java.util.Properties;
  7.  
  8. import javax.activation.DataHandler;
  9. import javax.activation.DataSource;
  10. import javax.activation.FileDataSource;
  11. import javax.mail.BodyPart;
  12. import javax.mail.Message;
  13. import javax.mail.Multipart;
  14. import javax.mail.PasswordAuthentication;
  15. import javax.mail.Session;
  16. import javax.mail.Transport;
  17. import javax.mail.internet.InternetAddress;
  18. import javax.mail.internet.MimeBodyPart;
  19. import javax.mail.internet.MimeMessage;
  20. import javax.mail.internet.MimeMultipart;
  21.  
  22. public class GMailSender extends javax.mail.Authenticator {
  23.  
  24. private String mailhost = "smtp.gmail.com";
  25. private String user;
  26. private String password;
  27. private Session session;
  28.  
  29. private Multipart _multipart = new MimeMultipart();
  30.  
  31. static {
  32. Security.addProvider(new JSSEProvider());
  33. }
  34.  
  35. public GMailSender(String user, String password) {
  36. this.user = user;
  37. this.password = password;
  38. Properties props = new Properties();
  39. props.setProperty("mail.transport.protocol", "smtp");
  40. props.setProperty("mail.host", mailhost);
  41. props.put("mail.smtp.auth", "true");
  42. props.put("mail.smtp.port", "465");
  43. props.put("mail.smtp.socketFactory.port", "465");
  44. props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  45. props.put("mail.smtp.socketFactory.fallback", "false");
  46. props.setProperty("mail.smtp.quitwait", "false");
  47. session = Session.getDefaultInstance(props, this);
  48. }
  49.  
  50. protected PasswordAuthentication getPasswordAuthentication() {
  51. return new PasswordAuthentication(user, password);
  52. }
  53.  
  54. public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {
  55. try {
  56. MimeMessage message = new MimeMessage(session);
  57. DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
  58. message.setSender(new InternetAddress(sender));
  59. message.setSubject(subject);
  60. message.setDataHandler(handler);
  61. BodyPart messageBodyPart = new MimeBodyPart();
  62. messageBodyPart.setText(body);
  63. _multipart.addBodyPart(messageBodyPart);
  64.  
  65. message.setContent(_multipart);
  66.  
  67. if (recipients.indexOf(',') > 0)
  68. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
  69. else
  70. message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
  71.  
  72. Transport.send(message);
  73. } catch (Exception e) {
  74. e.printStackTrace();
  75. }
  76. }
  77.  
  78. public void addAttachment(String filename) throws Exception {
  79. BodyPart messageBodyPart = new MimeBodyPart();
  80. DataSource source = new FileDataSource(filename);
  81. messageBodyPart.setDataHandler(new DataHandler(source));
  82. messageBodyPart.setFileName("download image");
  83. _multipart.addBodyPart(messageBodyPart);
  84. }
  85.  
  86. public class ByteArrayDataSource implements DataSource {
  87.  
  88. private byte[] data;
  89. private String type;
  90.  
  91. public ByteArrayDataSource(byte[] data, String type) {
  92. super();
  93. this.data = data;
  94. this.type = type;
  95. }
  96.  
  97. public ByteArrayDataSource(byte[] data) {
  98. super();
  99. this.data = data;
  100. }
  101.  
  102.  
  103. public void setType(String type) {
  104. this.type = type;
  105. }
  106.  
  107.  
  108. public String getContentType() {
  109. if (type == null)
  110. return "application/octet-stream";
  111. else
  112. return type;
  113. }
  114.  
  115.  
  116. public InputStream getInputStream() throws IOException {
  117. return new ByteArrayInputStream(data);
  118. }
  119.  
  120.  
  121. public String getName() {
  122. return "ByteArrayDataSource";
  123. }
  124.  
  125.  
  126. public OutputStream getOutputStream() throws IOException {
  127. throw new IOException("Not Supported");
  128. }
  129. }
  130. }
Add Comment
Please, Sign In to add comment