|
| 1 | +/** |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.compute.mailjet; |
| 18 | + |
| 19 | +// [START mailjet_imports] |
| 20 | +import com.mailjet.client.MailjetClient; |
| 21 | +import com.mailjet.client.MailjetRequest; |
| 22 | +import com.mailjet.client.MailjetResponse; |
| 23 | +import com.mailjet.client.errors.MailjetException; |
| 24 | +import com.mailjet.client.resource.Email; |
| 25 | +// [END mailjet_imports] |
| 26 | + |
| 27 | +import org.json.JSONArray; |
| 28 | +import org.json.JSONObject; |
| 29 | + |
| 30 | +// [START app] |
| 31 | +public class MailjetSender{ |
| 32 | + |
| 33 | + public static void main(String[] args) { |
| 34 | + final String MAILJET_API_KEY = "YOUR-MAILJET-API-KEY"; |
| 35 | + final String MAILJET_SECRET_KEY = "YOUR-MAILJET-SECRET-KEY"; |
| 36 | + MailjetClient client = new MailjetClient(MAILJET_API_KEY, MAILJET_SECRET_KEY); |
| 37 | + |
| 38 | + MailjetSender sender = new MailjetSender(); |
| 39 | + sender.sendMailjet(args[0], args[1], client); |
| 40 | + } |
| 41 | + |
| 42 | + public MailjetResponse sendMailjet(String recipient, String sender, MailjetClient client) { |
| 43 | + MailjetRequest email = new MailjetRequest(Email.resource) |
| 44 | + .property(Email.FROMEMAIL, sender) |
| 45 | + .property(Email.FROMNAME, "pandora") |
| 46 | + .property(Email.SUBJECT, "Your email flight plan!") |
| 47 | + .property(Email.TEXTPART, |
| 48 | + "Dear passenger, welcome to Mailjet! May the delivery force be with you!") |
| 49 | + .property(Email.HTMLPART, |
| 50 | + "<h3>Dear passenger, welcome to Mailjet!</h3><br/>May the delivery force be with you!") |
| 51 | + .property(Email.RECIPIENTS, new JSONArray().put(new JSONObject().put("Email", recipient))); |
| 52 | + |
| 53 | + try { |
| 54 | + // trigger the API call |
| 55 | + MailjetResponse response = client.post(email); |
| 56 | + // Read the response data and status |
| 57 | + System.out.println(response.getStatus()); |
| 58 | + System.out.println(response.getData()); |
| 59 | + return response; |
| 60 | + } catch (MailjetException e) { |
| 61 | + System.out.println("Mailjet Exception: " + e); |
| 62 | + return null; |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | +// [END app] |
0 commit comments