Advertisement
Guest User

Untitled

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