Guest User

Untitled

a guest
Aug 28th, 2018
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.35 KB | None | 0 0
  1. package com.example.clc_construction;
  2. import java.io.File;
  3. import java.io.UnsupportedEncodingException;
  4. import java.util.Properties;
  5. import javax.activation.DataHandler;
  6. import javax.activation.DataSource;
  7. import javax.activation.FileDataSource;
  8. import javax.mail.Message;
  9. import javax.mail.MessagingException;
  10. import javax.mail.Multipart;
  11. import javax.mail.PasswordAuthentication;
  12. import javax.mail.Session;
  13. import javax.mail.Transport;
  14. import javax.mail.internet.AddressException;
  15. import javax.mail.internet.InternetAddress;
  16. import javax.mail.internet.MimeBodyPart;
  17. import javax.mail.internet.MimeMessage;
  18. import javax.mail.internet.MimeMultipart;
  19. import android.app.Activity;
  20. import android.app.ProgressDialog;
  21. import android.content.Intent;
  22. import android.graphics.Bitmap;
  23. import android.os.AsyncTask;
  24. import android.os.Bundle;
  25. import android.os.Environment;
  26.  
  27.  
  28. public class Email extends Activity
  29. {
  30. public String jobNo;
  31. public String teamNo;
  32. private static final String username = "abc@gmail.com";
  33. private static final String password = "000000";
  34. private static final String emailid = "xyz@outlook.com";
  35. private static final String subject = "Photo";
  36. private static final String message = "Hello";
  37. private Multipart multipart = new MimeMultipart();
  38. private MimeBodyPart messageBodyPart = new MimeBodyPart();
  39. public File mediaFile;
  40.  
  41. @Override
  42. protected void onCreate(Bundle savedInstanceState)
  43. {
  44. super.onCreate(savedInstanceState);
  45. setContentView(R.layout.camera_screen);
  46. Intent intent = getIntent();
  47. jobNo = intent.getStringExtra("Job_No");
  48. teamNo = intent.getStringExtra("Team_No");
  49. sendMail(emailid,subject,message);
  50.  
  51. }
  52. private void sendMail(String email, String subject, String messageBody)
  53. {
  54. Session session = createSessionObject();
  55.  
  56. try {
  57. Message message = createMessage(email, subject, messageBody, session);
  58. new SendMailTask().execute(message);
  59. }
  60. catch (AddressException e)
  61. {
  62. e.printStackTrace();
  63. }
  64. catch (MessagingException e)
  65. {
  66. e.printStackTrace();
  67. }
  68. catch (UnsupportedEncodingException e)
  69. {
  70. e.printStackTrace();
  71. }
  72. }
  73.  
  74.  
  75. private Session createSessionObject()
  76. {
  77. Properties properties = new Properties();
  78. properties.put("mail.smtp.auth", "true");
  79. properties.put("mail.smtp.starttls.enable", "true");
  80. properties.put("mail.smtp.host", "smtp.gmail.com");
  81. properties.put("mail.smtp.port", "587");
  82.  
  83. return Session.getInstance(properties, new javax.mail.Authenticator()
  84. {
  85. protected PasswordAuthentication getPasswordAuthentication()
  86. {
  87. return new PasswordAuthentication(username, password);
  88. }
  89. });
  90. }
  91.  
  92. private Message createMessage(String email, String subject, String messageBody, Session session) throws
  93.  
  94. MessagingException, UnsupportedEncodingException
  95. {
  96. Message message = new MimeMessage(session);
  97. message.setFrom(new InternetAddress("xzy@outlook.com", "Naveed Qureshi"));
  98. message.addRecipient(Message.RecipientType.TO, new InternetAddress(email, email));
  99. message.setSubject(subject);
  100. message.setText(messageBody);
  101. return message;
  102. }
  103.  
  104.  
  105.  
  106. public class SendMailTask extends AsyncTask<Message, Void, Void>
  107. {
  108. private ProgressDialog progressDialog;
  109.  
  110. @Override
  111. protected void onPreExecute()
  112. {
  113. super.onPreExecute();
  114. progressDialog = ProgressDialog.show(Email.this, "Please wait", "Sending mail", true, false);
  115. }
  116.  
  117. @Override
  118. protected void onPostExecute(Void aVoid)
  119. {
  120. super.onPostExecute(aVoid);
  121. progressDialog.dismiss();
  122. }
  123.  
  124. protected Void doInBackground(javax.mail.Message... messages)
  125. {
  126. try
  127. {
  128. Transport.send(messages[0]);
  129. } catch (MessagingException e)
  130. {
  131. e.printStackTrace();
  132. }
  133. return null;
  134. }
  135. }
  136. }
  137.  
  138. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  139.  
  140. public boolean isOnline() {
  141. ConnectivityManager cm =
  142. (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  143. NetworkInfo netInfo = cm.getActiveNetworkInfo();
  144. if (netInfo != null && netInfo.isConnectedOrConnecting()) {
  145. return true;
  146. }
  147. return false;
  148. }
  149.  
  150. final String username = "username@gmail.com";
  151. final String password = "password";
  152.  
  153. Properties props = new Properties();
  154. props.put("mail.smtp.auth", "true");
  155. props.put("mail.smtp.starttls.enable", "true");
  156. props.put("mail.smtp.host", "smtp.gmail.com");
  157. props.put("mail.smtp.port", "587");
  158.  
  159. Session session = Session.getInstance(props,
  160. new javax.mail.Authenticator() {
  161. protected PasswordAuthentication getPasswordAuthentication() {
  162. return new PasswordAuthentication(username, password);
  163. }
  164. });
  165. try {
  166. Message message = new MimeMessage(session);
  167. message.setFrom(new InternetAddress("from-email@gmail.com"));
  168. message.setRecipients(Message.RecipientType.TO,
  169. InternetAddress.parse("to-email@gmail.com"));
  170. message.setSubject("Testing Subject");
  171. message.setText("Dear Mail Crawler,"
  172. + "nn No spam to my email, please!");
  173.  
  174. MimeBodyPart messageBodyPart = new MimeBodyPart();
  175.  
  176. Multipart multipart = new MimeMultipart();
  177.  
  178. messageBodyPart = new MimeBodyPart();
  179. String file = "path of file to be attached";
  180. String fileName = "attachmentName"
  181. DataSource source = new FileDataSource(file);
  182. messageBodyPart.setDataHandler(new DataHandler(source));
  183. messageBodyPart.setFileName(fileName);
  184. multipart.addBodyPart(messageBodyPart);
  185.  
  186. message.setContent(multipart);
  187.  
  188. Transport.send(message);
  189.  
  190. System.out.println("Done");
  191.  
  192. } catch (MessagingException e) {
  193. throw new RuntimeException(e);
  194. }
  195.  
  196. private Session createSessionObject()
  197. {
  198. Properties properties = new Properties();
  199. properties.setProperty("mail.smtp.auth", "true");
  200. properties.setProperty("mail.smtp.starttls.enable", "true");
  201. properties.setProperty("mail.smtp.host", "smtp.gmail.com");
  202. properties.setProperty("mail.smtp.port", "465");
  203.  
  204. return Session.getInstance(properties, new javax.mail.Authenticator()
  205. {
  206. protected PasswordAuthentication getPasswordAuthentication()
  207. {
  208. return new PasswordAuthentication(username, password);
  209. }
  210. });
  211. }
Add Comment
Please, Sign In to add comment