2020import com .sun .jersey .api .client .ClientResponse ;
2121import com .sun .jersey .api .client .WebResource ;
2222import com .sun .jersey .api .client .filter .HTTPBasicAuthFilter ;
23+ import com .sun .jersey .core .util .MultivaluedMapImpl ;
2324import com .sun .jersey .multipart .FormDataMultiPart ;
2425import com .sun .jersey .multipart .file .FileDataBodyPart ;
2526
3839public class MailgunServlet extends HttpServlet {
3940
4041 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" );
4243
4344 @ Override
4445 public void doPost (HttpServletRequest req , HttpServletResponse resp ) throws IOException {
46+ String type = req .getParameter ("submit" );
4547 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 ) {
4660 Client client = Client .create ();
4761 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" );
5078 FormDataMultiPart formData = new FormDataMultiPart ();
5179 formData .field ("from" , "Mailgun User <mailgun@" + MAILGUN_DOMAIN_NAME + ">" );
5280 formData .field ("to" , recipient );
@@ -55,13 +83,8 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOEx
5583 ClassLoader classLoader = getClass ().getClassLoader ();
5684 File txtFile = new File (classLoader .getResource ("example-attachment.txt" ).getFile ());
5785 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 );
6588 }
6689}
6790// [END example]
0 commit comments