|
2 | 2 |
|
3 | 3 | import com.github.kevinsawicki.http.HttpRequest;
|
4 | 4 |
|
5 |
| -public class Sendgrid { |
6 |
| - protected String username; |
7 |
| - protected String password; |
8 |
| - |
9 |
| - private String to = ""; |
10 |
| - private String from = ""; |
11 |
| - private String subject = ""; |
12 |
| - private String text = ""; |
13 |
| - private String html = ""; |
14 |
| - |
15 |
| - public Sendgrid(String username, String password) { |
16 |
| - this.username = username; |
17 |
| - this.password = password; |
18 |
| - } |
19 |
| - |
20 |
| - public void send() { |
21 |
| - this.web(); |
22 |
| - } |
23 |
| - |
24 |
| - public void web() { |
25 |
| - HttpRequest request = HttpRequest.post("https://sendgrid.com/api/mail.send.json"); |
26 |
| - |
27 |
| - request.part("api_user", this.username); |
28 |
| - request.part("api_key", this.password); |
29 |
| - request.part("to", this.getTo()); |
30 |
| - request.part("subject", this.getSubject()); |
31 |
| - request.part("from", this.getFrom()); |
32 |
| - request.part("text", this.getText()); |
33 |
| - request.part("html", this.getHtml()); |
34 |
| - |
35 |
| - String response = request.body(); |
36 |
| - |
37 |
| - System.out.println(response); |
38 |
| - } |
39 |
| - |
40 |
| - public String getTo() { |
41 |
| - return this.to; |
42 |
| - } |
43 |
| - |
44 |
| - public String getFrom() { |
45 |
| - return this.from; |
46 |
| - } |
47 |
| - |
48 |
| - public String getSubject() { |
49 |
| - return this.subject; |
50 |
| - } |
51 |
| - |
52 |
| - public String getText() { |
53 |
| - return this.text; |
54 |
| - } |
| 5 | +import java.io.File; |
55 | 6 |
|
56 |
| - public String getHtml() { |
57 |
| - return this.html; |
58 |
| - } |
59 |
| - |
60 |
| - public Sendgrid setTo(String email) { |
61 |
| - this.to = email; |
62 |
| - |
63 |
| - return this; |
64 |
| - } |
65 |
| - |
66 |
| - public Sendgrid setFrom(String email) { |
67 |
| - this.from = email; |
68 |
| - |
69 |
| - return this; |
70 |
| - } |
71 |
| - |
72 |
| - public Sendgrid setSubject(String subject) { |
73 |
| - this.subject = subject; |
74 |
| - |
75 |
| - return this; |
76 |
| - } |
77 |
| - |
78 |
| - public Sendgrid setText(String text) { |
79 |
| - this.text = text; |
80 |
| - |
81 |
| - return this; |
82 |
| - } |
83 |
| - |
84 |
| - public Sendgrid setHtml(String html) { |
85 |
| - this.html = html; |
86 |
| - |
87 |
| - return this; |
88 |
| - } |
| 7 | +public class Sendgrid { |
| 8 | +// ------------------------------ FIELDS ------------------------------ |
| 9 | + |
| 10 | + private static final String PARAM_API_KEY = "api_key"; |
| 11 | + private static final String PARAM_API_USER = "api_user"; |
| 12 | + private static final String PARAM_BCC = "bcc"; |
| 13 | + private static final String PARAM_FILES = "files[%s]"; |
| 14 | + private static final String PARAM_FROM = "from"; |
| 15 | + private static final String PARAM_HTML = "html"; |
| 16 | + private static final String PARAM_SUBJECT = "subject"; |
| 17 | + private static final String PARAM_TEXT = "text"; |
| 18 | + private static final String PARAM_TO = "to"; |
| 19 | + |
| 20 | + private String[] bcc; |
| 21 | + private File[] file; |
| 22 | + private String from; |
| 23 | + private String html; |
| 24 | + private String password; |
| 25 | + private String subject; |
| 26 | + private String text; |
| 27 | + private String[] to; |
| 28 | + private String username; |
| 29 | + |
| 30 | +// -------------------------- STATIC METHODS -------------------------- |
| 31 | + |
| 32 | + public static Sendgrid withCredentials(String username, String password) { |
| 33 | + return new Sendgrid(username, password); |
| 34 | + } |
| 35 | + |
| 36 | +// --------------------------- CONSTRUCTORS --------------------------- |
| 37 | + |
| 38 | + public Sendgrid(final String username, final String password) { |
| 39 | + this.username = username; |
| 40 | + this.password = password; |
| 41 | + } |
| 42 | + |
| 43 | +// -------------------------- OTHER METHODS -------------------------- |
| 44 | + |
| 45 | + public Sendgrid bcc(final String... bcc) { |
| 46 | + this.bcc = bcc; |
| 47 | + return this; |
| 48 | + } |
| 49 | + |
| 50 | + public Sendgrid from(final String email) { |
| 51 | + this.from = email; |
| 52 | + return this; |
| 53 | + } |
| 54 | + |
| 55 | + public String send() { |
| 56 | + HttpRequest request = HttpRequest.post("https://sendgrid.com/api/mail.send.json"); |
| 57 | + if (username != null) { |
| 58 | + request.part(PARAM_API_USER, username); |
| 59 | + } |
| 60 | + if (password != null) { |
| 61 | + request.part(PARAM_API_KEY, password); |
| 62 | + } |
| 63 | + if (from != null) { |
| 64 | + request.part(PARAM_FROM, from); |
| 65 | + } |
| 66 | + if (to != null) { |
| 67 | + for (String s : to) { |
| 68 | + request.part(PARAM_TO, s); |
| 69 | + } |
| 70 | + } |
| 71 | + if (bcc != null) { |
| 72 | + for (String s : bcc) { |
| 73 | + request.part(PARAM_BCC, s); |
| 74 | + } |
| 75 | + } |
| 76 | + if (subject != null) { |
| 77 | + request.part(PARAM_SUBJECT, subject); |
| 78 | + } |
| 79 | + if (text != null) { |
| 80 | + request.part(PARAM_TEXT, text); |
| 81 | + } |
| 82 | + if (html != null) { |
| 83 | + request.part(PARAM_HTML, html); |
| 84 | + } |
| 85 | + if (file != null) { |
| 86 | + for (File f : file) { |
| 87 | + request.part(String.format(PARAM_FILES, f.getName()), f); |
| 88 | + } |
| 89 | + } |
| 90 | + return request.body(); |
| 91 | + } |
| 92 | + |
| 93 | + public Sendgrid to(final String... to) { |
| 94 | + this.to = to; |
| 95 | + return this; |
| 96 | + } |
| 97 | + |
| 98 | + public Sendgrid withAttachment(final File... file) { |
| 99 | + this.file = file; |
| 100 | + return this; |
| 101 | + } |
| 102 | + |
| 103 | + public Sendgrid withHtml(final String html) { |
| 104 | + this.html = html; |
| 105 | + return this; |
| 106 | + } |
| 107 | + |
| 108 | + public Sendgrid withSubject(final String subject) { |
| 109 | + this.subject = subject; |
| 110 | + return this; |
| 111 | + } |
| 112 | + |
| 113 | + public Sendgrid withText(final String text) { |
| 114 | + this.text = text; |
| 115 | + return this; |
| 116 | + } |
89 | 117 | }
|
0 commit comments