Search in sources :

Example 1 with MailjetRequest

use of com.mailjet.client.MailjetRequest in project java-docs-samples by GoogleCloudPlatform.

the class MailjetServlet method doPost.

@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
    String recipient = req.getParameter("to");
    String sender = req.getParameter("from");
    MailjetRequest email = new MailjetRequest(Emailv31.resource).property(Emailv31.MESSAGES, new JSONArray().put(new JSONObject().put(Emailv31.Message.FROM, new JSONObject().put("Email", sender).put("Name", "pandora")).put(Emailv31.Message.TO, new JSONArray().put(new JSONObject().put("Email", recipient))).put(Emailv31.Message.SUBJECT, "Your email flight plan!").put(Emailv31.Message.TEXTPART, "Dear passenger, welcome to Mailjet! May the delivery force be with you!").put(Emailv31.Message.HTMLPART, "<h3>Dear passenger, welcome to Mailjet!</h3><br /> " + "May the delivery force be with you!")));
    try {
        // trigger the API call
        MailjetResponse response = client.post(email);
        // Read the response data and status
        resp.getWriter().print(response.getStatus());
        resp.getWriter().print(response.getData());
    } catch (MailjetException e) {
        throw new ServletException("Mailjet Exception", e);
    } catch (MailjetSocketTimeoutException e) {
        throw new ServletException("Mailjet socket timed out", e);
    }
}
Also used : ServletException(javax.servlet.ServletException) JSONObject(org.json.JSONObject) MailjetResponse(com.mailjet.client.MailjetResponse) MailjetRequest(com.mailjet.client.MailjetRequest) MailjetSocketTimeoutException(com.mailjet.client.errors.MailjetSocketTimeoutException) JSONArray(org.json.JSONArray) MailjetException(com.mailjet.client.errors.MailjetException)

Example 2 with MailjetRequest

use of com.mailjet.client.MailjetRequest in project java-docs-samples by GoogleCloudPlatform.

the class MailjetServlet method doPost.

@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
    String recipient = req.getParameter("to");
    String sender = req.getParameter("from");
    MailjetRequest email = new MailjetRequest(Emailv31.resource).property(Emailv31.MESSAGES, new JSONArray().put(new JSONObject().put(Emailv31.Message.FROM, new JSONObject().put("Email", sender).put("Name", "Mailjet Pilot")).put(Emailv31.Message.TO, new JSONArray().put(new JSONObject().put("Email", recipient))).put(Emailv31.Message.SUBJECT, "Your email flight plan!").put(Emailv31.Message.TEXTPART, "Dear passenger, welcome to Mailjet! May the delivery force be with you!").put(Emailv31.Message.HTMLPART, "<h3>Dear passenger, welcome to Mailjet!</h3><br />" + "May the delivery force be with you!")));
    try {
        // trigger the API call
        MailjetResponse response = client.post(email);
        // Read the response data and status
        resp.getWriter().print(response.getStatus());
        resp.getWriter().print(response.getData());
    } catch (MailjetException e) {
        throw new ServletException("Mailjet Exception", e);
    } catch (MailjetSocketTimeoutException e) {
        throw new ServletException("Mailjet socket timed out", e);
    }
}
Also used : ServletException(javax.servlet.ServletException) JSONObject(org.json.JSONObject) MailjetResponse(com.mailjet.client.MailjetResponse) MailjetRequest(com.mailjet.client.MailjetRequest) MailjetSocketTimeoutException(com.mailjet.client.errors.MailjetSocketTimeoutException) JSONArray(org.json.JSONArray) MailjetException(com.mailjet.client.errors.MailjetException)

Example 3 with MailjetRequest

use of com.mailjet.client.MailjetRequest in project teammates by TEAMMATES.

the class MailjetService method parseToEmail.

/**
 * {@inheritDoc}
 */
@Override
public MailjetRequest parseToEmail(EmailWrapper wrapper) {
    MailjetRequest request = new MailjetRequest(Email.resource);
    request.property(Email.FROMEMAIL, wrapper.getSenderEmail());
    if (wrapper.getSenderName() != null && !wrapper.getSenderName().isEmpty()) {
        request.property(Email.FROMNAME, wrapper.getSenderName());
    }
    request.property(Email.RECIPIENTS, new JSONArray().put(new JSONObject().put("Email", wrapper.getRecipient())));
    if (wrapper.getBcc() != null && !wrapper.getBcc().isEmpty()) {
        request.append(Email.RECIPIENTS, new JSONObject().put("Email", wrapper.getBcc()));
    }
    request.property(Email.HEADERS, new JSONObject().put("Reply-To", wrapper.getReplyTo()));
    request.property(Email.SUBJECT, wrapper.getSubject());
    request.property(Email.HTMLPART, wrapper.getContent());
    request.property(Email.TEXTPART, Jsoup.parse(wrapper.getContent()).text());
    return request;
}
Also used : JSONObject(org.json.JSONObject) MailjetRequest(com.mailjet.client.MailjetRequest) JSONArray(org.json.JSONArray)

Example 4 with MailjetRequest

use of com.mailjet.client.MailjetRequest in project java-docs-samples by GoogleCloudPlatform.

the class MailjetSender method sendMailjet.

public MailjetResponse sendMailjet(String recipient, String sender, MailjetClient client) throws MailjetException, MailjetSocketTimeoutException {
    MailjetRequest email = new MailjetRequest(Emailv31.resource).property(Emailv31.MESSAGES, new JSONArray().put(new JSONObject().put(Emailv31.Message.FROM, new JSONObject().put("Email", sender).put("Name", "pandora")).put(Emailv31.Message.TO, new JSONArray().put(new JSONObject().put("Email", recipient))).put(Emailv31.Message.SUBJECT, "Your email flight plan!").put(Emailv31.Message.TEXTPART, "Dear passenger, welcome to Mailjet! May the delivery force be with you!").put(Emailv31.Message.HTMLPART, "<h3>Dear passenger, welcome to Mailjet!</h3>" + "<br />May the delivery force be with you!")));
    try {
        // trigger the API call
        MailjetResponse response = client.post(email);
        // Read the response data and status
        System.out.println(response.getStatus());
        System.out.println(response.getData());
        return response;
    } catch (MailjetException e) {
        System.out.println("Mailjet Exception: " + e);
        return null;
    }
}
Also used : JSONObject(org.json.JSONObject) MailjetResponse(com.mailjet.client.MailjetResponse) MailjetRequest(com.mailjet.client.MailjetRequest) JSONArray(org.json.JSONArray) MailjetException(com.mailjet.client.errors.MailjetException)

Example 5 with MailjetRequest

use of com.mailjet.client.MailjetRequest in project teammates by TEAMMATES.

the class EmailSenderTest method testConvertToMailjet.

@Test
public void testConvertToMailjet() {
    EmailWrapper wrapper = getTypicalEmailWrapper();
    MailjetRequest request = new MailjetService().parseToEmail(wrapper);
    JSONObject email = new JSONObject(request.getBody());
    assertEquals(wrapper.getSenderEmail(), email.get(Email.FROMEMAIL));
    assertEquals(wrapper.getSenderName(), email.get(Email.FROMNAME));
    assertEquals(wrapper.getRecipient(), ((JSONArray) email.get(Email.RECIPIENTS)).getJSONObject(0).get("Email"));
    assertEquals(wrapper.getBcc(), ((JSONArray) email.get(Email.RECIPIENTS)).getJSONObject(1).get("Email"));
    assertEquals(wrapper.getReplyTo(), ((JSONObject) email.get(Email.HEADERS)).getString("Reply-To"));
    assertEquals(wrapper.getSubject(), email.get(Email.SUBJECT));
    assertEquals(wrapper.getContent(), email.get(Email.HTMLPART));
}
Also used : MailjetService(teammates.logic.core.MailjetService) JSONObject(org.json.JSONObject) MailjetRequest(com.mailjet.client.MailjetRequest) JSONArray(org.json.JSONArray) EmailWrapper(teammates.common.util.EmailWrapper) Test(org.testng.annotations.Test)

Aggregations

MailjetRequest (com.mailjet.client.MailjetRequest)6 JSONArray (org.json.JSONArray)5 JSONObject (org.json.JSONObject)5 MailjetResponse (com.mailjet.client.MailjetResponse)4 MailjetException (com.mailjet.client.errors.MailjetException)3 MailjetSocketTimeoutException (com.mailjet.client.errors.MailjetSocketTimeoutException)2 ServletException (javax.servlet.ServletException)2 MailjetClient (com.mailjet.client.MailjetClient)1 Test (org.testng.annotations.Test)1 EmailWrapper (teammates.common.util.EmailWrapper)1 MailjetService (teammates.logic.core.MailjetService)1