Advertisement
Guest User

Untitled

a guest
Jan 28th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.40 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() {
  80.         try{
  81.         Properties props = _setProperties();
  82.  
  83.         if(!_user.equals("") && !pass.equals("") && to.length > 0 && !from.equals("") && !subject.equals("") && !_body.equals("")) {
  84.             Session session = Session.getInstance(props, this);
  85.  
  86.             MimeMessage msg = new MimeMessage(session);
  87.  
  88.             msg.setFrom(new InternetAddress(_from));
  89.  
  90.             InternetAddress[] addressTo = new InternetAddress[_to.length];
  91.             for (int i = 0; i < _to.length; i++) {
  92.                 addressTo[i] = new InternetAddress(_to[i]);
  93.             }
  94.             msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);
  95.  
  96.             msg.setSubject(_subject);
  97.             msg.setSentDate(new Date());
  98.  
  99.             // setup message body
  100.             BodyPart messageBodyPart = new MimeBodyPart();
  101.             messageBodyPart.setText(_body);
  102.             _multipart.addBodyPart(messageBodyPart);
  103.  
  104.             // Put parts in message
  105.             msg.setContent(_multipart);
  106.  
  107.             // send email
  108.             Transport.send(msg);
  109.  
  110.             return true;
  111.         } else {
  112.             return false;
  113.         }
  114.     }catch (ClassCastException e) {
  115.     e.printStackTrace();
  116.     }
  117.  
  118.     public void addAttachment(String filename) {
  119.  try {
  120.     BodyPart messageBodyPart = new MimeBodyPart();
  121.         DataSource source = new FileDataSource(filename);
  122.         messageBodyPart.setDataHandler(new DataHandler(source));
  123.         messageBodyPart.setFileName(filename);
  124.  
  125.         _multipart.addBodyPart(messageBodyPart);
  126.   } catch (ClassCastException e) {
  127.     e.printStackTrace();
  128.        
  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.     // the getters and setters
  158.     public String getBody() {
  159.         return _body;
  160.     }
  161.  
  162.     public void setBody(String _body) {
  163.         this._body = _body;
  164.     }
  165.  
  166.     public String[] getTo(){
  167.         return _to;
  168.     }
  169.  
  170.     public void setTo(String[] _to){
  171.         this._to = _to;
  172.     }
  173.  
  174.     public String getFrom(){
  175.         return _from;
  176.     }
  177.  
  178.     public void setFrom(String _from){
  179.         this._from = _from;
  180.     }
  181.  
  182.     public String getSubject(){
  183.         return _subject;
  184.     }
  185.  
  186.     public void setSubject(String _subject){
  187.         this._subject = _subject;
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement