Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.aironman;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- public class SendEmail {
- public static void main(String[] args) throws IOException {
- if (args.length != 3) {
- 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");
- System.exit(-1);
- }
- String path_to_winners = args[0];
- String path_to_stars = args[1];
- String email = args[2];
- String content_path_to_winners = new String(Files.readAllBytes(Paths.get(path_to_winners)));
- String content_path_to_stars = new String(Files.readAllBytes(Paths.get(path_to_stars)));
- StringBuffer sb = new StringBuffer();
- sb.append(content_path_to_winners);
- sb.append(content_path_to_stars);
- String subject = "Resultados_" + new java.util.Date();
- boolean ret = EmailSenderService.sendEmail(sb.toString(),subject ,email);
- System.out.println("Done! " + ret );
- System.exit(0);
- }
- }
- package com.aironman;
- import java.io.IOException;
- import com.sendgrid.Content;
- import com.sendgrid.Email;
- import com.sendgrid.Mail;
- import com.sendgrid.Method;
- import com.sendgrid.Request;
- import com.sendgrid.Response;
- import com.sendgrid.SendGrid;
- public class EmailSenderService {
- public final static boolean sendEmail(String body, String _subject, String _to) {
- boolean ret = true;
- Email from = new Email("alonsoir@gmail.com");
- String subject = _subject;
- Email to = new Email(_to);
- Content content = new Content("text/plain", body);
- Mail mail = new Mail(from, subject, to, content);
- SendGrid sg = new SendGrid("MY-API-KEY");
- Request request = new Request();
- try {
- request.setMethod(Method.POST);
- request.setEndpoint("mail/send");
- request.setBody(mail.build());
- Response response = sg.api(request);
- System.out.println("StatusCode: " + response.getStatusCode());
- System.out.println("response.getBody: " + response.getBody());
- System.out.println("response.getHeaders: " + response.getHeaders());
- } catch (IOException ex) {
- System.out.println(ex.getLocalizedMessage());
- ret = false;
- }
- return ret;
- }
- }
- I am using this dependency in the pom.xml file
- <!-- https://mvnrepository.com/artifact/com.sendgrid/sendgrid-java -->
- <dependency>
- <groupId>com.sendgrid</groupId>
- <artifactId>sendgrid-java</artifactId>
- <version>4.3.0</version>
- </dependency>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement