Advertisement
Guest User

Untitled

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