Advertisement
Guest User

Untitled

a guest
Aug 14th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. private void sendMail(String email, String subject, String messageBody,File file) {
  2. Session session = createSessionObject();
  3. // new UpdateTask().execute();
  4. try {
  5. Message message = createMessage(email, subject, messageBody, session,file);
  6.  
  7. new UpdateTask().execute(message);
  8.  
  9. } catch (AddressException e) {
  10. e.printStackTrace();
  11. } catch (MessagingException e) {
  12. e.printStackTrace();
  13. } catch (UnsupportedEncodingException e) {
  14. e.printStackTrace();
  15. }
  16. }
  17.  
  18. private Message createMessage(String email, String subject, String messageBody, Session session,File file) throws MessagingException, UnsupportedEncodingException {
  19. Message message = new MimeMessage(session);
  20. message.setFrom(new InternetAddress("XXXXXXXX@gmail.com", "Sound Check"));
  21. message.addRecipient(Message.RecipientType.TO, new InternetAddress(email, email));
  22. message.setSubject(subject);
  23. message.setText(messageBody);
  24. /**
  25. * Attach a file in mail
  26. */
  27. Multipart multipart = new MimeMultipart();
  28. BodyPart messageBodyPart = new MimeBodyPart();
  29. DataSource source = new FileDataSource(file);
  30. messageBodyPart.setDataHandler(new DataHandler(source));
  31. messageBodyPart.setFileName(file.getName());
  32.  
  33. multipart.addBodyPart(messageBodyPart);
  34. message.setContent(multipart);
  35. return message;
  36. }
  37.  
  38. private Session createSessionObject() {
  39. Properties props = new Properties();
  40. props.setProperty("mail.transport.protocol", "smtp");
  41. props.setProperty("mail.host", "smtp.gmail.com");
  42. props.put("mail.smtp.auth", "true");
  43. props.put("mail.smtp.port", "465");
  44. props.put("mail.smtp.socketFactory.port", "465");
  45. props.put("mail.smtp.socketFactory.class",
  46. "javax.net.ssl.SSLSocketFactory");
  47. props.put("mail.smtp.socketFactory.fallback", "false");
  48. props.setProperty("mail.smtp.quitwait", "false");
  49.  
  50. return Session.getInstance(props, new javax.mail.Authenticator() {
  51. protected PasswordAuthentication getPasswordAuthentication() {
  52. return new PasswordAuthentication("xxxxxxxxxx@gmail.com", "*********");
  53. }
  54. });
  55. }
  56. }
  57. class UpdateTask extends AsyncTask<Message,String,String> {
  58.  
  59.  
  60.  
  61. @Override
  62. protected String doInBackground(Message... params) {
  63. // TODO Auto-generated method stub
  64. Message message = params[0];
  65. try {
  66.  
  67. Transport.send(message);
  68.  
  69. } catch (AddressException e) {
  70. e.printStackTrace();
  71. } catch (MessagingException e) {
  72. e.printStackTrace();
  73. }
  74.  
  75. return null;
  76. }
  77.  
  78. 08-14 12:09:23.365: W/System.err(30695): javax.mail.AuthenticationFailedException
  79. 08-14 12:09:23.365: W/System.err(30695): at javax.mail.Service.connect(Service.java:319)
  80. 08-14 12:09:23.365: W/System.err(30695): at javax.mail.Service.connect(Service.java:169)
  81. 08-14 12:09:23.365: W/System.err(30695): at javax.mail.Service.connect(Service.java:118)
  82. 08-14 12:09:23.365: W/System.err(30695): at javax.mail.Transport.send0(Transport.java:188)
  83. 08-14 12:09:23.365: W/System.err(30695): at javax.mail.Transport.send(Transport.java:118)
  84. 08-14 12:09:23.365: W/System.err(30695): at com.example.callrecoder.UpdateTask.doInBackground(RecordService.java:384)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement