20
20
import com .sun .jersey .api .client .ClientResponse ;
21
21
import com .sun .jersey .api .client .WebResource ;
22
22
import com .sun .jersey .api .client .filter .HTTPBasicAuthFilter ;
23
+ import com .sun .jersey .core .util .MultivaluedMapImpl ;
23
24
import com .sun .jersey .multipart .FormDataMultiPart ;
24
25
import com .sun .jersey .multipart .file .FileDataBodyPart ;
25
26
38
39
public class MailgunServlet extends HttpServlet {
39
40
40
41
private static final String MAILGUN_DOMAIN_NAME = System .getenv ("MAILGUN_DOMAIN_NAME" );
41
- private static final String MAILGUN_API_KEY = System .getenv ("MAILGUN_API_KEY" );
42
+ private static final String MAILGUN_API_KEY = System .getenv ("MAILGUN_API_KEY" );
42
43
43
44
@ Override
44
45
public void doPost (HttpServletRequest req , HttpServletResponse resp ) throws IOException {
46
+ String type = req .getParameter ("submit" );
45
47
String recipient = req .getParameter ("to" );
48
+ ClientResponse clientResponse ;
49
+ if (type .equals ("Send simple email" )) {
50
+ clientResponse = sendSimpleMessage (recipient );
51
+ } else {
52
+ clientResponse = sendComplexMessage (recipient );
53
+ }
54
+ if (clientResponse .getStatus () == 200 ) {
55
+ resp .getWriter ().print ("Email sent." );
56
+ }
57
+ }
58
+
59
+ private ClientResponse sendSimpleMessage (String recipient ) {
46
60
Client client = Client .create ();
47
61
client .addFilter (new HTTPBasicAuthFilter ("api" , MAILGUN_API_KEY ));
48
- WebResource webResource =
49
- client .resource ("https://api.mailgun.net/v3/" + MAILGUN_DOMAIN_NAME + "/messages" );
62
+ WebResource webResource = client .resource ("https://api.mailgun.net/v3/" + MAILGUN_DOMAIN_NAME
63
+ + "/messages" );
64
+ MultivaluedMapImpl formData = new MultivaluedMapImpl ();
65
+ formData .add ("from" , "Mailgun User <mailgun@" + MAILGUN_DOMAIN_NAME + ">" );
66
+ formData .add ("to" , recipient );
67
+ formData .add ("subject" , "Simple Mailgun Example" );
68
+ formData .add ("text" , "Plaintext content" );
69
+ return webResource .type (MediaType .APPLICATION_FORM_URLENCODED ).post (ClientResponse .class ,
70
+ formData );
71
+ }
72
+
73
+ private ClientResponse sendComplexMessage (String recipient ) {
74
+ Client client = Client .create ();
75
+ client .addFilter (new HTTPBasicAuthFilter ("api" , MAILGUN_API_KEY ));
76
+ WebResource webResource = client .resource ("https://api.mailgun.net/v3/" + MAILGUN_DOMAIN_NAME
77
+ + "/messages" );
50
78
FormDataMultiPart formData = new FormDataMultiPart ();
51
79
formData .field ("from" , "Mailgun User <mailgun@" + MAILGUN_DOMAIN_NAME + ">" );
52
80
formData .field ("to" , recipient );
@@ -55,13 +83,8 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOEx
55
83
ClassLoader classLoader = getClass ().getClassLoader ();
56
84
File txtFile = new File (classLoader .getResource ("example-attachment.txt" ).getFile ());
57
85
formData .bodyPart (new FileDataBodyPart ("attachment" , txtFile , MediaType .TEXT_PLAIN_TYPE ));
58
- ClientResponse clientResponse =
59
- webResource .type (MediaType .MULTIPART_FORM_DATA_TYPE ).post (ClientResponse .class , formData );
60
- if (clientResponse .getStatus () == 200 ) {
61
- resp .getWriter ().print ("Email sent." );
62
- } else {
63
- resp .getWriter ().print ("An error was encountered" );
64
- }
86
+ return webResource .type (MediaType .MULTIPART_FORM_DATA_TYPE )
87
+ .post (ClientResponse .class , formData );
65
88
}
66
89
}
67
90
// [END example]
0 commit comments