Advertisement
Guest User

Untitled

a guest
Aug 27th, 2017
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. username: [__________]
  2. password: [__________]
  3. SMTP: [__________]
  4. Port: [__________]
  5. SSL/TLS: Dropdown box with 3 options, No, SSL, and TLS
  6.  
  7. import java.util.Date;
  8. import java.util.Properties;
  9. import javax.activation.CommandMap;
  10. import javax.activation.DataHandler;
  11. import javax.activation.DataSource;
  12. import javax.activation.FileDataSource;
  13. import javax.activation.MailcapCommandMap;
  14. import javax.mail.BodyPart;
  15. import javax.mail.Multipart;
  16. import javax.mail.PasswordAuthentication;
  17. import javax.mail.Session;
  18. import javax.mail.Transport;
  19. import javax.mail.internet.InternetAddress;
  20. import javax.mail.internet.MimeBodyPart;
  21. import javax.mail.internet.MimeMessage;
  22. import javax.mail.internet.MimeMultipart;
  23.  
  24.  
  25. public class Mail extends javax.mail.Authenticator {
  26. private String _user;
  27. private String _pass;
  28.  
  29. private String[] _to;
  30. private String _from;
  31.  
  32. private String _port;
  33. private String _sport;
  34.  
  35. private String _host;
  36.  
  37. private String _subject;
  38. private String _body;
  39.  
  40. private boolean _auth;
  41.  
  42. private boolean _debuggable;
  43.  
  44. private Multipart _multipart;
  45.  
  46.  
  47. public Mail() {
  48. _host = "smtp.gmail.com"; // default smtp server
  49. _port = "465"; // default smtp port
  50. _sport = "465"; // default socketfactory port
  51.  
  52. _user = ""; // username
  53. _pass = ""; // password
  54. _from = ""; // email sent from
  55. _subject = ""; // email subject
  56. _body = ""; // email body
  57.  
  58. _debuggable = false; // debug mode on or off - default off
  59. _auth = true; // smtp authentication - default on
  60.  
  61. _multipart = new MimeMultipart();
  62.  
  63. // There is something wrong with MailCap, javamail can not find a handler for the multipart/mixed part, so this bit needs to be added.
  64. MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
  65. mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
  66. mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
  67. mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
  68. mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
  69. mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
  70. CommandMap.setDefaultCommandMap(mc);
  71. }
  72.  
  73. public Mail(String user, String pass) {
  74. this();
  75.  
  76. _user = user;
  77. _pass = pass;
  78. }
  79.  
  80. public boolean send() throws Exception {
  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. }
  115.  
  116. public void addAttachment(String filename) throws Exception {
  117. BodyPart messageBodyPart = new MimeBodyPart();
  118. DataSource source = new FileDataSource(filename);
  119. messageBodyPart.setDataHandler(new DataHandler(source));
  120. messageBodyPart.setFileName(filename);
  121.  
  122. _multipart.addBodyPart(messageBodyPart);
  123. }
  124.  
  125. @Override
  126. public PasswordAuthentication getPasswordAuthentication() {
  127. return new PasswordAuthentication(_user, _pass);
  128. }
  129.  
  130. private Properties _setProperties() {
  131. Properties props = new Properties();
  132.  
  133. props.put("mail.smtp.host", _host);
  134.  
  135. if(_debuggable) {
  136. props.put("mail.debug", "true");
  137. }
  138.  
  139. if(_auth) {
  140. props.put("mail.smtp.auth", "true");
  141. }
  142.  
  143. props.put("mail.smtp.port", _port);
  144. props.put("mail.smtp.socketFactory.port", _sport);
  145. props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  146. props.put("mail.smtp.socketFactory.fallback", "false");
  147.  
  148. return props;
  149. }
  150.  
  151. // the getters and setters
  152. public String getBody() {
  153. return _body;
  154. }
  155.  
  156. public void setBody(String _body) {
  157. this._body = _body;
  158. }
  159.  
  160. // more of the getters and setters …..
  161. }
  162.  
  163. Intent mailClient = new Intent(Intent.ACTION_VIEW);
  164. mailClient.setClassName("com.google.android.gm", "com.google.android.gm.ConversationListActivity");
  165. startActivity(mailClient);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement