Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.14 KB | None | 0 0
  1. package hiasm.hiasmproject;
  2.  
  3. import android.os.Handler;
  4. import android.os.HandlerThread;
  5.  
  6. import java.io.File;
  7. import java.util.List;
  8. import java.util.Properties;
  9.  
  10. import javax.activation.DataHandler;
  11. import javax.activation.FileDataSource;
  12. import javax.mail.Authenticator;
  13. import javax.mail.Message;
  14. import javax.mail.MessagingException;
  15. import javax.mail.Multipart;
  16. import javax.mail.PasswordAuthentication;
  17. import javax.mail.Session;
  18. import javax.mail.Transport;
  19. import javax.mail.internet.InternetAddress;
  20. import javax.mail.internet.MimeBodyPart;
  21. import javax.mail.internet.MimeMessage;
  22. import javax.mail.internet.MimeMultipart;
  23.  
  24. public class SmtpMailer {
  25.  
  26.     public static class Params {
  27.  
  28.         private String username;
  29.         private String password;
  30.         private String host;
  31.         private int port;
  32.         private String recipientAddress;
  33.         private String subject;
  34.         private String text;
  35.         private List<String> attachmentFileNames;
  36.  
  37.         public String getUsername() {
  38.             return username;
  39.         }
  40.  
  41.         public void setUsername(String username) {
  42.             this.username = username;
  43.         }
  44.  
  45.         public String getPassword() {
  46.             return password;
  47.         }
  48.  
  49.         public void setPassword(String password) {
  50.             this.password = password;
  51.         }
  52.  
  53.         public String getHost() {
  54.             return host;
  55.         }
  56.  
  57.         public void setHost(String host) {
  58.             this.host = host;
  59.         }
  60.  
  61.         public int getPort() {
  62.             return port;
  63.         }
  64.  
  65.         public void setPort(int port) {
  66.             this.port = port;
  67.         }
  68.  
  69.         public String getRecipientAddress() {
  70.             return recipientAddress;
  71.         }
  72.  
  73.         public void setRecipientAddress(String recipientAddress) {
  74.             this.recipientAddress = recipientAddress;
  75.         }
  76.  
  77.         public String getSubject() {
  78.             return subject;
  79.         }
  80.  
  81.         public void setSubject(String subject) {
  82.             this.subject = subject;
  83.         }
  84.  
  85.         public String getText() {
  86.             return text;
  87.         }
  88.  
  89.         public void setText(String text) {
  90.             this.text = text;
  91.         }
  92.  
  93.         public List<String> getAttachmentFileNames() {
  94.             return attachmentFileNames;
  95.         }
  96.  
  97.         public void setAttachmentFileNames(List<String> attachmentFileNames) {
  98.             this.attachmentFileNames = attachmentFileNames;
  99.         }
  100.     }
  101.  
  102.     public static void sendMail(final Params params) {
  103.         HandlerThread handlerThread = new HandlerThread("SmtpMailerHandlerThread");
  104.         handlerThread.start();
  105.         Handler handler = new Handler(handlerThread.getLooper());
  106.         handler.post(new Runnable() {
  107.             @Override
  108.             public void run() {
  109.                 try {
  110.                     Properties props = new Properties();
  111.                     props.put("mail.smtp.auth", "true");
  112.                     props.put("mail.smtp.starttls.enable", "true");
  113.                     props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  114.                     props.put("mail.smtp.host", params.getHost());
  115.                     props.put("mail.smtp.port", params.getPort());
  116.                     props.put("mail.smtp.socketFactory.port", params.getPort());
  117.  
  118.                     Authenticator authenticator = new javax.mail.Authenticator() {
  119.                         protected PasswordAuthentication getPasswordAuthentication() {
  120.                             return new PasswordAuthentication(params.getUsername(), params.getPassword());
  121.                         }
  122.                     };
  123.                     Session session = Session.getInstance(props, authenticator);
  124.  
  125.                     Message message = new MimeMessage(session);
  126.                     message.setFrom(new InternetAddress(params.getUsername()));
  127.                     message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(params.getRecipientAddress()));
  128.                     message.setSubject(params.getSubject());
  129.                     message.setText(params.getText());
  130.  
  131.                     if (params.getAttachmentFileNames() != null && params.getAttachmentFileNames().size() > 0) {
  132.                         addAttachments(message, params);
  133.                     }
  134.  
  135.                     Transport.send(message);
  136.                 } catch(Exception e) {
  137.                     e.printStackTrace();
  138.                 }
  139.             }
  140.         });
  141.     }
  142.  
  143.     private static void addAttachments(Message message, Params params) throws MessagingException {
  144.         Multipart multipart = new MimeMultipart();
  145.         for (String filePath : params.getAttachmentFileNames()) {
  146.             MimeBodyPart messageBodyPart = new MimeBodyPart();
  147.             messageBodyPart.setDataHandler(new DataHandler(new FileDataSource(filePath)));
  148.             messageBodyPart.setFileName(new File(filePath).getName());
  149.             multipart.addBodyPart(messageBodyPart);
  150.         }
  151.         message.setContent(multipart);
  152.     }
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement