Advertisement
aironman

sendgrid-java

Jan 18th, 2019
356
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.aironman;
  2.  
  3. import java.io.IOException;
  4. import java.nio.file.Files;
  5. import java.nio.file.Paths;
  6.  
  7. public class SendEmail {
  8.  
  9. public static void main(String[] args) throws IOException {
  10.  
  11. if (args.length != 3) {
  12. System.out.println("USAGE: java -cp target/MyStreamjava8-1.0.1-RELEASE.jar com.aironman.SendEmail PATH_TO_final_output_winners.txt PATH_TO_final_output_star.txt correo_destino");
  13. System.exit(-1);
  14. }
  15. String path_to_winners = args[0];
  16. String path_to_stars = args[1];
  17. String email = args[2];
  18.  
  19. String content_path_to_winners = new String(Files.readAllBytes(Paths.get(path_to_winners)));
  20. String content_path_to_stars = new String(Files.readAllBytes(Paths.get(path_to_stars)));
  21. StringBuffer sb = new StringBuffer();
  22. sb.append(content_path_to_winners);
  23. sb.append(content_path_to_stars);
  24. String subject = "Resultados_" + new java.util.Date();
  25. boolean ret = EmailSenderService.sendEmail(sb.toString(),subject ,email);
  26. System.out.println("Done! " + ret );
  27. System.exit(0);
  28. }
  29. }
  30.  
  31. package com.aironman;
  32.  
  33. import java.io.IOException;
  34.  
  35. import com.sendgrid.Content;
  36. import com.sendgrid.Email;
  37. import com.sendgrid.Mail;
  38. import com.sendgrid.Method;
  39. import com.sendgrid.Request;
  40. import com.sendgrid.Response;
  41. import com.sendgrid.SendGrid;
  42.  
  43. public class EmailSenderService {
  44.  
  45. public final static boolean sendEmail(String body, String _subject, String _to) {
  46.  
  47. boolean ret = true;
  48. Email from = new Email("alonsoir@gmail.com");
  49. String subject = _subject;
  50. Email to = new Email(_to);
  51. Content content = new Content("text/plain", body);
  52. Mail mail = new Mail(from, subject, to, content);
  53.  
  54. SendGrid sg = new SendGrid("MY-API-KEY");
  55. Request request = new Request();
  56. try {
  57. request.setMethod(Method.POST);
  58. request.setEndpoint("mail/send");
  59. request.setBody(mail.build());
  60. Response response = sg.api(request);
  61. System.out.println("StatusCode: " + response.getStatusCode());
  62. System.out.println("response.getBody: " + response.getBody());
  63. System.out.println("response.getHeaders: " + response.getHeaders());
  64. } catch (IOException ex) {
  65. System.out.println(ex.getLocalizedMessage());
  66. ret = false;
  67. }
  68. return ret;
  69. }
  70. }
  71.  
  72. I am using this dependency in the pom.xml file
  73.  
  74. <!-- https://mvnrepository.com/artifact/com.sendgrid/sendgrid-java -->
  75. <dependency>
  76. <groupId>com.sendgrid</groupId>
  77. <artifactId>sendgrid-java</artifactId>
  78. <version>4.3.0</version>
  79. </dependency>
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement