Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.13 KB | None | 0 0
  1. package com.cst.timeManagement.services;
  2.  
  3. import com.sun.mail.util.MailSSLSocketFactory;
  4. import org.apache.http.HttpEntity;
  5. import org.apache.http.entity.ByteArrayEntity;
  6. import org.json.JSONObject;
  7. import org.springframework.stereotype.Service;
  8.  
  9. import javax.mail.*;
  10. import javax.mail.internet.InternetAddress;
  11. import javax.mail.internet.MimeMessage;
  12. import java.io.IOException;
  13. import java.security.GeneralSecurityException;
  14. import java.util.Properties;
  15.  
  16. import java.io.BufferedReader;
  17. import java.io.InputStreamReader;
  18.  
  19. import org.apache.http.HttpResponse;
  20. import org.apache.http.client.HttpClient;
  21. import org.apache.http.client.methods.HttpPost;
  22. import org.apache.http.impl.client.DefaultHttpClient;
  23.  
  24. /**
  25. * Created by Alex on 7/11/2017.
  26. */
  27. @Service
  28. public class SmtpMailSenderService {
  29.  
  30. public void sendVerificationMail(String email, String message, String subject, String token, String purpose){
  31. SendMailTLS sendMailTLS = new SendMailTLS(email, message, subject, token, purpose);
  32. Thread th = new Thread(sendMailTLS);
  33. th.start();
  34. }
  35.  
  36. public void sendForgotPasswordMail(String email, String message, String subject, String token, String purpose){
  37. SendMailTLS sendMailTLS = new SendMailTLS(email, message, subject, token, purpose);
  38. Thread th = new Thread(sendMailTLS);
  39. th.start();
  40. }
  41.  
  42. public class SendMailTLS implements Runnable{
  43.  
  44. private String email;
  45. private String message;
  46. private String subject;
  47. private String token;
  48. private String purpose;
  49.  
  50. public SendMailTLS(String email, String message, String subject, String token, String purpose) {
  51. this.email = email;
  52. this.message = message;
  53. this.subject = subject;
  54. this.token = token;
  55. this.purpose = purpose;
  56. }
  57.  
  58. @Override
  59. public void run(){
  60.  
  61. final String username = "timemanagement@cst-dev.com";
  62. final String password = "12345678";
  63.  
  64. Properties props = new Properties();
  65. props.put("mail.smtp.auth", "true");
  66. props.put("mail.smtp.starttls.enable", "true");
  67. props.put("mail.smtp.host", "mail.cst-dev.com");
  68. props.put("mail.smtp.port", "587");
  69.  
  70. MailSSLSocketFactory sf = null;
  71. try {
  72. sf = new MailSSLSocketFactory();
  73. } catch (GeneralSecurityException e1) {
  74.  
  75. e1.printStackTrace();
  76. }
  77. sf.setTrustAllHosts(true);
  78. props.put("mail.smtp.ssl.socketFactory", sf);
  79.  
  80. Session session = Session.getInstance(props,
  81. new javax.mail.Authenticator() {
  82. protected PasswordAuthentication getPasswordAuthentication() {
  83. return new PasswordAuthentication(username, password);
  84. }
  85. });
  86.  
  87. try {
  88.  
  89. Message message = new MimeMessage(session);
  90. message.setFrom(new InternetAddress("timemanagement@cst-dev.com"));
  91. message.setRecipients(Message.RecipientType.TO,
  92. InternetAddress.parse(this.email));
  93. message.setSubject(this.subject);
  94.  
  95. String deepLink = null;
  96. try {
  97. deepLink = getLinkFromBranch(this.token);
  98. } catch (IOException e) {
  99. e.printStackTrace();
  100. }
  101.  
  102. if(this.purpose.equals("confirmation"))
  103. message.setContent( "<html>" + "<head>" +
  104. "<style>\n" +
  105. ".button {\n" +
  106. " background-color: #ee5d2c;\n" +
  107. " border: none;\n" +
  108. " color: white;\n" +
  109. " padding: 10px 12px;\n" +
  110. " text-align: center;\n" +
  111. " text-decoration: none;\n" +
  112. " display: inline-block;\n" +
  113. " font-size: 16px;\n" +
  114. " margin: 0px 2px;\n" +
  115. " -webkit-transition-duration: 0.4s; /* Safari */\n" +
  116. " transition-duration: 0.4s;\n" +
  117. " cursor: pointer;\n" +
  118. "}" + "\n" +
  119. ".button:hover {\n" +
  120. " background-color: #ef480c;\n" +
  121. " color: white;\n" +
  122. "}" + "\n" + "</style>" + "</head>" +
  123. "<body style='text-align:center;'>" + "<img src=\"http://imgur.com/xNGZjLW.png\" alt=\"W3Schools.com\" style=\"margin:0 auto;width:355px;height:80px;\">" + "<br>" + "<br>" +
  124. "<h2 style='text-align:center;'>" + "Your're almost there." + "</h2>" +
  125. "<a href=" + deepLink + "?token=" + this.token +">" + "Click here " + "</a>"+ "to confirm your account! Or click down below. " + "<br>" + "<br>" +
  126. "<a href=" + deepLink + "?token=" + this.token + " class='button'>" + "YES, SING ME UP!" +
  127. "</a>" + "<br>" + "<br>" +
  128. "You will need this " + "token: " + "<span style='color:#ef480c; font-weight: bold;'>" + this.token + "</span>" +
  129. " to confirm your account for our mobile app." + "<br>" + "</body>", "text/html");
  130. //Forgot password.
  131. else message.setContent("<body style='text-align:center;'>" +
  132. "<img src=\"http://imgur.com/xNGZjLW.png\" alt=\"W3Schools.com\" style=\"width:355px;height:80px;\">" + "<br>" +
  133. "<h2 style='text-align:center;'>" + "Forgot password?!" + "</h2>" +
  134. "<a href=http://cst-dev.com:8085/timeManagement/" + this.purpose + "?token=" +
  135. this.token +">" + "Click here" + "</a>" + " to reset your password!" +"<br>" + "<br>" + "You will need this " + "token: " + "<span style='color:#ef480c; font-weight: bold;'>" + this.token + "</span>" +
  136. " to reset your password for our mobile app." + "</body>", "text/html");
  137. Transport.send(message);
  138.  
  139. System.out.println("Done");
  140.  
  141. } catch (MessagingException e) {
  142. throw new RuntimeException(e);
  143. }
  144. }
  145. }
  146.  
  147. private static final String USER_AGENT = "Mozilla/5.0";
  148.  
  149. public static void main(String[] args) {
  150. try {
  151. String token = "token-data";
  152. System.out.println(getLinkFromBranch(token));
  153. } catch (IOException e) {
  154. e.printStackTrace();
  155. }
  156. }
  157.  
  158. private static String getLinkFromBranch(String token) throws IOException {
  159.  
  160. String url = "https://api.branch.io/v1/url";
  161.  
  162. HttpClient client = new DefaultHttpClient();
  163. HttpPost post = new HttpPost(url);
  164.  
  165. // add header
  166. post.setHeader("User-Agent", USER_AGENT);
  167.  
  168. String jsonRequestBody = "{\n" +
  169. "\t\"branch_key\":\"key_test_fhCv6EVT08JEKYWaVcZlIbmbBsefexn6\", \n" +
  170. "\t\"data\":\"{\\\"token\\\":\\\"" + token + "\\\"}\"\n" +
  171. "}";
  172. HttpEntity entity = new ByteArrayEntity(jsonRequestBody.getBytes("UTF-8"));
  173. post.setEntity(entity);
  174.  
  175. HttpResponse response = client.execute(post);
  176. System.out.println("\nSending 'POST' request to URL : " + url);
  177. System.out.println("Post parameters : " + post.getEntity());
  178. System.out.println("Response Code : " +
  179. response.getStatusLine().getStatusCode());
  180.  
  181. BufferedReader rd = new BufferedReader(
  182. new InputStreamReader(response.getEntity().getContent()));
  183.  
  184. StringBuffer result = new StringBuffer();
  185. String line = "";
  186. while ((line = rd.readLine()) != null) {
  187. result.append(line);
  188. }
  189.  
  190. JSONObject jsonRepose = new JSONObject(result.toString());
  191.  
  192. return jsonRepose.getString("url");
  193. }
  194.  
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement