Guest User

Untitled

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