Guest User

Untitled

a guest
Jul 14th, 2018
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.11 KB | None | 0 0
  1. > package com.ixpenseit.email;
  2. >
  3. > import java.util.Date;
  4. > import java.util.Properties;
  5. >
  6. > import javax.activation.CommandMap;
  7. > import javax.activation.DataHandler;
  8. > import javax.activation.DataSource;
  9. > import javax.activation.FileDataSource;
  10. > import javax.activation.MailcapCommandMap;
  11. > import javax.mail.BodyPart;
  12. > import javax.mail.MessagingException;
  13. > import javax.mail.Multipart;
  14. > import javax.mail.PasswordAuthentication;
  15. > import javax.mail.Session;
  16. > import javax.mail.Transport;
  17. > import javax.mail.internet.AddressException;
  18. > import javax.mail.internet.InternetAddress;
  19. > import javax.mail.internet.MimeBodyPart;
  20. > import javax.mail.internet.MimeMessage;
  21. > import javax.mail.internet.MimeMultipart;
  22. >
  23. > public class Mail extends javax.mail.Authenticator {
  24. > private String _user;
  25. > private String _pass;
  26. >
  27. > private String[] _to;
  28. > private String _from;
  29. >
  30. > private String _port;
  31. > private String _sport;
  32. >
  33. > private String _host;
  34. >
  35. > private String _subject;
  36. > private String _body;
  37. >
  38. > private boolean _auth;
  39. >
  40. > private Multipart _multipart;
  41. >
  42. > private Mail() {
  43. > _auth = true; // smtp authentication - default on
  44. > _multipart = new MimeMultipart();
  45. > }
  46. >
  47. > public Mail(String smtpserver, String smtpport, String
  48. > smtpsocketfactoryport) {
  49. > _host = smtpserver; // default smtp server
  50. > _port = smtpport; // default smtp port
  51. > _sport = smtpsocketfactoryport; // default socketfactory port
  52. > _auth = true; // smtp authentication - default on
  53. > _multipart = new MimeMultipart();
  54. > MailcapCommandMap mc = (MailcapCommandMap) CommandMap
  55. > .getDefaultCommandMap();
  56. > mc
  57. > .addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
  58. > mc
  59. > .addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
  60. > mc
  61. > .addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
  62. > mc
  63. > .addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
  64. > mc
  65. > .addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
  66. > CommandMap.setDefaultCommandMap(mc);
  67. >
  68. > }
  69. >
  70. > public void setCredential(String username, String password) {
  71. > _user = username;
  72. > _pass = password;
  73. > }
  74. >
  75. > public boolean send() throws AddressException, MessagingException
  76. > {
  77. > Properties props = _setProperties();
  78. >
  79. > if (Utility.isValidString(_user) && Utility.isValidString(_pass)
  80. > && _to != null && Utility.isValidString(_from)
  81. > && Utility.isValidString(_body)
  82. > && Utility.isValidString(_subject) &&
  83. > _to.length > 0
  84. >
  85. > ) {
  86. > Session session = Session.getInstance(props, this);
  87. >
  88. > MimeMessage msg = new MimeMessage(session);
  89. > msg.setFrom(new InternetAddress(_from));
  90. >
  91. > InternetAddress[] addressTo = new InternetAddress[_to.length];
  92. > for (int i = 0; i < _to.length; i++) {
  93. > addressTo[i] = new InternetAddress(_to[i]);
  94. > }
  95. > msg.setRecipients(MimeMessage.RecipientType.TO,
  96. > addressTo);
  97. >
  98. > msg.setSubject(_subject);
  99. > msg.setSentDate(new Date());
  100. >
  101. > // setup message body
  102. > BodyPart messageBodyPart = new MimeBodyPart();
  103. > messageBodyPart.setText(_body);
  104. > _multipart.addBodyPart(messageBodyPart);
  105. >
  106. > // Put parts in message
  107. > msg.setContent(_multipart);
  108. >
  109. > // send email
  110. > Transport.send(msg);
  111. >
  112. > return true;
  113. > } else {
  114. > return false;
  115. > }
  116. > }
  117. >
  118. > public void setEmail(String[] to, String from, String subject, String
  119. > body) {
  120. > _to = to;
  121. > _body = body;
  122. > _subject = subject;
  123. > _from = from;
  124. >
  125. > }
  126. >
  127. > public void addAttachment(String filename) throws Exception {
  128. > BodyPart messageBodyPart = new MimeBodyPart();
  129. > DataSource source = new FileDataSource(filename);
  130. > messageBodyPart.setDataHandler(new
  131. > DataHandler(source));
  132. > messageBodyPart.setFileName(filename);
  133. > _multipart.addBodyPart(messageBodyPart);
  134. > }
  135. >
  136. > public void addAttachment(String folder, String filename) throws
  137. > Exception {
  138. > BodyPart messageBodyPart = new MimeBodyPart();
  139. > DataSource source = new FileDataSource(folder + "" +
  140. > filename);
  141. > messageBodyPart.setDataHandler(new
  142. > DataHandler(source));
  143. > messageBodyPart.setFileName(filename);
  144. > _multipart.addBodyPart(messageBodyPart);
  145. > }
  146. >
  147. > @Override
  148. > public PasswordAuthentication getPasswordAuthentication() {
  149. > return new PasswordAuthentication(_user, _pass);
  150. > }
  151. >
  152. > private Properties _setProperties() {
  153. > Properties props = new Properties();
  154. > props.put("mail.smtp.host", _host);
  155. > props.put("mail.debug", "true");
  156. > if (_auth) {
  157. > props.put("mail.smtp.auth", "true");
  158. > }
  159. > props.put("mail.smtp.port", _port);
  160. > props.put("mail.smtp.socketFactory.port",
  161. > _sport);
  162. > props.put("mail.smtp.socketFactory.class",
  163. > "javax.net.ssl.SSLSocketFactory");
  164. > props.put("mail.smtp.socketFactory.fallback",
  165. > "false");
  166. > return props;
  167. > }
  168. >
  169. > }
  170.  
  171. public void onCreate(Bundle savedInstanceState) {
  172. super.onCreate(savedInstanceState);
  173. setContentView(R.layout.main);
  174. sendEmail();
  175. }
  176. private void sendEmail() {
  177. //Mail m = new Mail("smtp.gmail.com", "465", "465");
  178.  
  179. Mail m = new Mail("smtp.dummy.com", "27", "27");
  180. m.setCredential("abc@dummy", "dummy");
  181.  
  182. String[] toArr = { "to@gmail.com" };
  183. m.setEmail(toArr, "from@no-spam.com", "Auto Generated Expense Report",
  184. "Auto Generated Body.");
  185.  
  186. try {
  187. if (m.send()) {
  188. Toast.makeText(Email.this, "Email was sent successfully.",
  189. Toast.LENGTH_LONG).show();
  190. } else {
  191. Toast.makeText(Email.this, "Email was not sent.",
  192. Toast.LENGTH_LONG).show();
  193. }
  194. } catch (AddressException e) {
  195. // TODO Auto-generated catch block
  196. Log.e("MailApp", "Could not send email", e);
  197. } catch (MessagingException e) {
  198. // TODO Auto-generated catch block
  199. Log.e("MailApp", "Could not send email", e);
  200. }
Add Comment
Please, Sign In to add comment