Guest User

Untitled

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