Advertisement
Guest User

Untitled

a guest
Mar 13th, 2019
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.44 KB | None | 0 0
  1. public class Sender extends Activity {
  2.  
  3. Button Send;
  4. TextView text;
  5.  
  6. @Override
  7. public void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_sender);
  10.  
  11. Send = (Button) findViewById(R.id.mail);
  12. text = (TextView) findViewById(R.id.textView1);
  13.  
  14. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
  15. .permitAll().build();
  16. StrictMode.setThreadPolicy(policy);
  17.  
  18. Send.setOnClickListener(new View.OnClickListener() {
  19.  
  20. @Override
  21. public void onClick(View v) {
  22. // TODO Auto-generated method stub
  23. new SendMail().execute();
  24. }
  25. });
  26.  
  27. }
  28.  
  29. private class SendMail extends AsyncTask<String, Void, Integer>
  30. {
  31. ProgressDialog pd = null;
  32. String error = null;
  33. Integer result;
  34.  
  35. @Override
  36. protected void onPreExecute() {
  37. // TODO Auto-generated method stub
  38. super.onPreExecute();
  39. pd = new ProgressDialog(Sender.this);
  40. pd.setTitle("Sending Mail");
  41. pd.setMessage("Please wait...");
  42. pd.setCancelable(false);
  43. pd.show();
  44. }
  45.  
  46. @Override
  47. protected Integer doInBackground(String... params) {
  48. // TODO Auto-generated method stub
  49.  
  50. MailSender sender = new MailSender("me@mysmtp.com", "password");
  51.  
  52. sender.setTo(new String[]{"you@mysmtp.com", "me@gmail.com"});
  53. sender.setFrom("me@mysmtp.com");
  54. sender.setSubject("Test mail");
  55. sender.setBody("This is the mail body");
  56. try {
  57. if(sender.send()) {
  58. System.out.println("Message sent");
  59. return 1;
  60. } else {
  61. return 2;
  62. }
  63. } catch (Exception e) {
  64. error = e.getMessage();
  65. Log.e("SendMail", e.getMessage(), e);
  66. }
  67. return 3;
  68. }
  69.  
  70. protected void onPostExecute(Integer result) {
  71. pd.dismiss();
  72. if(error!=null) {
  73. text.setText(error);
  74. }
  75. if(result==1) {
  76. Toast.makeText(Sender.this,
  77. "Email was sent successfully.", Toast.LENGTH_LONG)
  78. .show();
  79. } else if(result==2) {
  80. Toast.makeText(Sender.this,
  81. "Email was not sent.", Toast.LENGTH_LONG).show();
  82. } else if(result==3) {
  83. Toast.makeText(Sender.this,
  84. "There was a problem sending the email.",
  85. Toast.LENGTH_LONG).show();
  86. }
  87. }
  88. }
  89. }
  90.  
  91. public class MailSender extends Authenticator {
  92. private String user;
  93. private String password;
  94.  
  95. private String [] to;
  96. private String from;
  97.  
  98. private String port;
  99. private String sport;
  100.  
  101. private String host;
  102.  
  103. private String subject;
  104. private String body;
  105.  
  106. private boolean auth;
  107. private boolean debuggable;
  108.  
  109. private Multipart multi;
  110.  
  111. public MailSender(){
  112. host = "web.mysmtp.com";
  113. port = "25";
  114. sport = "25";
  115.  
  116. user = "";
  117. password = "";
  118. from = "";
  119. subject = "";
  120. body = "";
  121.  
  122. debuggable = false;
  123. auth = true;
  124.  
  125. multi = new MimeMultipart();
  126.  
  127. // There is something wrong with MailCap, javamail can not find a handler for the multipart/mixed part, so this bit needs to be added.
  128. MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
  129. mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
  130. mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
  131. mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
  132. mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
  133. CommandMap.setDefaultCommandMap(mc);
  134. }
  135.  
  136. public MailSender(String user, String password){
  137. this();
  138. this.user = user;
  139. this.password = password;
  140. }
  141.  
  142. public boolean send() throws Exception {
  143. Properties props = setProperties();
  144.  
  145. try{
  146. Session session = Session.getInstance(props, this);
  147. session.setDebug(true);
  148.  
  149. MimeMessage msg = new MimeMessage(session);
  150.  
  151. msg.setFrom(new InternetAddress(from));
  152.  
  153. InternetAddress[] addressTo = new InternetAddress[to.length];
  154. for(int i=0; i<to.length; i++){
  155. addressTo[i] = new InternetAddress(to[i]);
  156. }
  157.  
  158. msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);
  159. msg.setSubject(subject);
  160. msg.setSentDate(new Date());
  161.  
  162. BodyPart messageBodyPart = new MimeBodyPart();
  163. messageBodyPart.setText(body);
  164. multi.addBodyPart(messageBodyPart);
  165.  
  166. msg.setContent(multi);
  167.  
  168. Transport transport = session.getTransport("smtps");
  169. transport.connect(host, 25, user, password);
  170. transport.sendMessage(msg, msg.getAllRecipients());
  171. transport.close();
  172. return true;
  173. } catch (Exception e) {
  174. e.printStackTrace();
  175. return false;
  176. }
  177. }
  178.  
  179. public void addAttachment(String filename) throws Exception {
  180. BodyPart messageBodyPart = new MimeBodyPart();
  181. DataSource source = new FileDataSource(filename);
  182. messageBodyPart.setDataHandler(new DataHandler(source));
  183. messageBodyPart.setFileName(filename);
  184.  
  185. multi.addBodyPart(messageBodyPart);
  186. }
  187.  
  188. @Override
  189. public PasswordAuthentication getPasswordAuthentication() {
  190. return new PasswordAuthentication(user, password);
  191. }
  192.  
  193. private Properties setProperties() {
  194. Properties props = new Properties();
  195.  
  196. props.put("mail.smtp.host", host);
  197.  
  198. if(debuggable) {
  199. props.put("mail.debug", "true");
  200. }
  201.  
  202. if(auth) {
  203. props.put("mail.smtp.auth", "true");
  204. }
  205.  
  206. props.put("mail.smtp.port", port);
  207. props.put("mail.smtp.socketFactory.port", sport);
  208. props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  209. props.put("mail.smtp.socketFactory.fallback", "false");
  210.  
  211. return props;
  212. }
  213.  
  214. public void setTo(String[] toAddress) {
  215. this.to = toAddress;
  216. }
  217.  
  218. public void setFrom(String fromAddress) {
  219. this.from = fromAddress;
  220. }
  221.  
  222. public void setSubject(String subject) {
  223. this.subject = subject;
  224. }
  225.  
  226. public void setBody(String body) {
  227. this.body = body;
  228. }
  229. }
  230.  
  231. 08-15 11:21:37.284: W/System.err(2553): javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x5c075548: Failure in SSL library, usually a protocol error
  232. 08-15 11:21:37.291: W/System.err(2553): Caused by: javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x5c075548: Failure in SSL library, usually a protocol error
  233. 08-15 11:21:37.299: W/System.err(2553): Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x5c075548: Failure in SSL library, usually a protocol error
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement