Advertisement
Guest User

Untitled

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