Advertisement
Guest User

Untitled

a guest
Jan 25th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.19 KB | None | 0 0
  1. package com.szewczyk.kuba.pipek_dreczyciel;
  2.  
  3. /**
  4.  * Created by Kuba on 2017-01-17.
  5.  */
  6.  
  7. import java.util.Date;
  8. import java.util.Properties;
  9. import javax.activation.CommandMap;
  10. import javax.activation.DataHandler;
  11. import javax.activation.DataSource;
  12. import javax.activation.FileDataSource;
  13. import javax.activation.MailcapCommandMap;
  14. import javax.mail.BodyPart;
  15. import javax.mail.MessagingException;
  16. import javax.mail.Multipart;
  17. import javax.mail.PasswordAuthentication;
  18. import javax.mail.Session;
  19. import javax.mail.Transport;
  20. import javax.mail.internet.InternetAddress;
  21. import javax.mail.internet.MimeBodyPart;
  22. import javax.mail.internet.MimeMessage;
  23. import javax.mail.internet.MimeMultipart;
  24.  
  25.  
  26. public class Mail extends javax.mail.Authenticator {
  27.     private String _user;
  28.     private String _pass;
  29.  
  30.     private String[] _to;
  31.     private String _from;
  32.  
  33.     private String _port;
  34.     private String _sport;
  35.  
  36.     private String _host;
  37.  
  38.     private String _subject;
  39.     private String _body;
  40.  
  41.     private boolean _auth;
  42.  
  43.     private boolean _debuggable;
  44.  
  45.     private Multipart _multipart;
  46.  
  47.     MimeMessage msg;
  48.  
  49.  
  50.     public Mail() {
  51.         _host = "smtp.gmail.com"; // default smtp server
  52.         _port = "465"; // default smtp port
  53.         _sport = "465"; // default socketfactory port
  54.  
  55.         _user = ""; // username
  56.         _pass = ""; // password
  57.         _from = ""; // email sent from
  58.         _subject = ""; // email subject
  59.         _body = ""; // email body
  60.  
  61.         _debuggable = false; // debug mode on or off - default off
  62.         _auth = true; // smtp authentication - default on
  63.  
  64.         _multipart = new MimeMultipart();
  65.  
  66.         MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
  67.         mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
  68.         mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
  69.         mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
  70.         mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
  71.         mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
  72.         CommandMap.setDefaultCommandMap(mc);
  73.     }
  74.  
  75.     public Mail(String user, String pass) {
  76.         this();
  77.  
  78.         _user = user;
  79.         _pass = pass;
  80.     }
  81.  
  82.     public boolean send() throws Exception {
  83.  
  84.         Properties props = _setProperties();
  85.  
  86.         if(!_user.equals("") && !_pass.equals("") && _to.length > 0 && !_from.equals("") && !_subject.equals("") && !_body.equals("")) {
  87.             Session session = Session.getInstance(props, this);
  88.  
  89.             msg = new MimeMessage(session);
  90.  
  91.             msg.setFrom(new InternetAddress(_from));
  92.  
  93.             InternetAddress[] addressTo = new InternetAddress[_to.length];
  94.             for (int i = 0; i < _to.length; i++) {
  95.                 addressTo[i] = new InternetAddress(_to[i]);
  96.             }
  97.             msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);
  98.  
  99.             msg.setSubject(_subject);
  100.             msg.setSentDate(new Date());
  101.             BodyPart messageBodyPart = new MimeBodyPart();
  102.             messageBodyPart.setText(_body);
  103.             _multipart.addBodyPart(messageBodyPart);
  104.             msg.setContent(_multipart);
  105.  
  106.             new Thread(new Runnable() {
  107.                 public void run() {
  108.                     try {
  109.                         Transport.send(msg);
  110.                     } catch (MessagingException e) {
  111.                         e.printStackTrace();
  112.                     }
  113.                 }
  114.             }).start();
  115.  
  116.             return true;
  117.         } else {
  118.             return false;
  119.         }
  120.     }
  121.  
  122.     public void addAttachment(String filename) throws Exception {
  123.         BodyPart messageBodyPart = new MimeBodyPart();
  124.         DataSource source = new FileDataSource(filename);
  125.         messageBodyPart.setDataHandler(new DataHandler(source));
  126.         messageBodyPart.setFileName(filename);
  127.  
  128.         _multipart.addBodyPart(messageBodyPart);
  129.     }
  130.  
  131.     @Override
  132.     public PasswordAuthentication getPasswordAuthentication() {
  133.         return new PasswordAuthentication(_user, _pass);
  134.     }
  135.  
  136.     private Properties _setProperties() {
  137.         Properties props = new Properties();
  138.  
  139.         props.put("mail.smtp.host", _host);
  140.  
  141.         if(_debuggable) {
  142.             props.put("mail.debug", "true");
  143.         }
  144.  
  145.         if(_auth) {
  146.             props.put("mail.smtp.auth", "true");
  147.         }
  148.  
  149.         props.put("mail.smtp.port", _port);
  150.         props.put("mail.smtp.socketFactory.port", _sport);
  151.         props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  152.         props.put("mail.smtp.socketFactory.fallback", "false");
  153.  
  154.         return props;
  155.     }
  156.  
  157.     public String getBody() {
  158.         return _body;
  159.     }
  160.  
  161.     public void setBody(String _body) {
  162.         this._body = _body;
  163.     }
  164.  
  165.     public void setTo(String[] _to) {
  166.         this._to = _to;
  167.     }
  168.  
  169.     public void setFrom(String _from) {
  170.         this._from = _from;
  171.     }
  172.  
  173.     public void setSubject(String _subject) {
  174.         this._subject = _subject;
  175.     }
  176.  
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement