Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. public class Mail {
  2.  
  3. public Mail() {
  4. _host = "smtp.gmail.com"; // default smtp server
  5. _port = "587"; // default smtp port587
  6.  
  7. _user = "myname@gmail.com"; // username
  8. _pass = "password"; // password
  9. _from = "myname@gmail.com"; // email sent from
  10. _to = {"yourname@gmail.com"};
  11. _subject = "go"; // email subject
  12. _body = "mail"; // email body
  13.  
  14. _debuggable = true; // debug mode on or off - default off
  15. _auth = true; // smtp authentication - default on
  16.  
  17. _multipart = new MimeMultipart();
  18.  
  19. // There is something wrong with MailCap, javamail can not find a handler for the multipart/mixed part, so this bit needs to be added.
  20. MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
  21. mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
  22. mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
  23. mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
  24. mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
  25. mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
  26. CommandMap.setDefaultCommandMap(mc);
  27. }
  28.  
  29. public Mail(String user, String pass) {
  30. this();
  31.  
  32. _user = user;
  33. _pass = pass;
  34. }
  35.  
  36. public boolean send() throws Exception {
  37. Properties props = _setProperties();
  38.  
  39. if(!_user.equals("") && !_pass.equals("") && _to.length > 0 && !_from.equals("") && !_subject.equals("") && !_body.equals("")) {
  40.  
  41. Session session = Session.getInstance(props, new GMailAuthenticator(_user,_pass));
  42.  
  43. MimeMessage msg = new MimeMessage(session);
  44.  
  45. msg.setFrom(new InternetAddress(_from));
  46.  
  47. InternetAddress[] addressTo = new InternetAddress[_to.length];
  48. for (int i = 0; i < _to.length; i++) {
  49. addressTo[i] = new InternetAddress(_to[i]);
  50. }
  51. msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);
  52. msg.setSubject(_subject);
  53. msg.setSentDate(new Date());
  54.  
  55. // setup message body
  56. BodyPart messageBodyPart = new MimeBodyPart();
  57. messageBodyPart.setText(_body);
  58. _multipart.addBodyPart(messageBodyPart);
  59.  
  60. // Put parts in message
  61. msg.setContent(_multipart);
  62.  
  63. // send email
  64.  
  65. Transport t = session.getTransport("smtp");
  66. try {
  67. t.connect(_host, _user, _pass);
  68. t.sendMessage(msg, msg.getAllRecipients());
  69.  
  70. } finally {
  71. t.close();
  72. }
  73.  
  74.  
  75. return true;
  76. } else {
  77. return false;
  78. }
  79. }
  80.  
  81. private Properties _setProperties() {
  82. Properties props = new Properties();
  83.  
  84. props.put("mail.smtp.host", _host);
  85.  
  86. if(_debuggable) {
  87. props.put("mail.debug", "true");
  88. }
  89.  
  90. if(_auth) {
  91. props.put("mail.smtp.auth", "true");
  92. }
  93.  
  94. props.put("mail.smtp.port", _port);
  95. props.put("mail.smtp.starttls.enable", "true");
  96. props.put("mail.debug", "true");
  97.  
  98. return props;
  99. }
  100.  
  101.  
  102. // class for secure access gmail
  103.  
  104. class GMailAuthenticator extends Authenticator {
  105. String user;
  106. String pw;
  107. public GMailAuthenticator (String username, String password)
  108. {
  109. super();
  110. this.user = username;
  111. this.pw = password;
  112. }
  113. public PasswordAuthentication getPasswordAuthentication()
  114. {
  115. return new PasswordAuthentication(user, pw);
  116. }
  117. }
  118.  
  119. final Button send = (Button) this.findViewById(R.id.send);
  120. send.setOnClickListener(new View.OnClickListener() {
  121.  
  122. public void onClick(View v) {
  123. // TODO Auto-generated method stub
  124.  
  125. new SendClass().execute();
  126.  
  127. }
  128. });
  129.  
  130. private class SendClass extends AsyncTask{
  131.  
  132. @Override
  133. protected Object doInBackground(Object[] objects) {
  134. Mail m = new Mail();
  135.  
  136. m.send();
  137. return null;
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement