Content subjContent = new Content().withData("Test of Amazon SES"); Message msg = new Message().withSubject(subjContent); // Include a body in both text and HTML formats Content textContent = new Content().withData("Hello - I hope you're having a good day."); Content htmlContent = new Content().withData("<h1>Hello - I hope you're having a good day.</h1>"); Body body = new Body().withHtml(htmlContent).withText(textContent); msg.setBody(body);
public Content unmarshall(StaxUnmarshallerContext context) throws Exception { Content content = new Content(); int originalDepth = context.getCurrentDepth(); int targetDepth = originalDepth + 1; if (context.isStartOfDocument()) targetDepth += 1; while (true) { XMLEvent xmlEvent = context.nextEvent(); if (xmlEvent.isEndDocument()) return content; if (xmlEvent.isAttribute() || xmlEvent.isStartElement()) { if (context.testExpression("Data", targetDepth)) { content.setData(StringStaxUnmarshaller.getInstance().unmarshall(context)); continue; } if (context.testExpression("Charset", targetDepth)) { content.setCharset(StringStaxUnmarshaller.getInstance().unmarshall(context)); continue; } } else if (xmlEvent.isEndElement()) { if (context.getCurrentDepth() < originalDepth) { return content; } } } }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (obj instanceof Body == false) return false; Body other = (Body) obj; if (other.getText() == null ^ this.getText() == null) return false; if (other.getText() != null && other.getText().equals(this.getText()) == false) return false; if (other.getHtml() == null ^ this.getHtml() == null) return false; if (other.getHtml() != null && other.getHtml().equals(this.getHtml()) == false) return false; return true; }
@Override public int hashCode() { final int prime = 31; int hashCode = 1; hashCode = prime * hashCode + ((getData() == null) ? 0 : getData().hashCode()); hashCode = prime * hashCode + ((getCharset() == null) ? 0 : getCharset().hashCode()); return hashCode; }
@Override public boolean sendEmail(List<String> emails, String subject, String body) { if (emails != null && !emails.isEmpty() && !StringUtils.isBlank(body)) { final SendEmailRequest request = new SendEmailRequest().withSource(Config.SUPPORT_EMAIL); Destination dest = new Destination().withToAddresses(emails); request.setDestination(dest); Content subjContent = new Content().withData(subject); Message msg = new Message().withSubject(subjContent); // Include a body in both text and HTML formats Content textContent = new Content().withData(body).withCharset(Config.DEFAULT_ENCODING); msg.setBody(new Body().withHtml(textContent)); request.setMessage(msg); Utils.asyncExecute(new Runnable() { public void run() { sesclient.sendEmail(request); } }); return true; } return false; } }
private Body bodyOfEmail(Email email) { if (email.getHtmlBody() != null) { Content htmlBody = new Content().withData(email.getHtmlBody()); return new Body().withHtml(htmlBody); } if (email.getTextBody() != null) { Content textBody = new Content().withData(email.getTextBody()); return new Body().withHtml(textBody); } return new Body(); }
import java.util.*; import javax.xml.bind.*; public class Demo { public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance(Content.class); List<String> strings = new ArrayList<String>(2); strings.add("foo"); strings.add("bar"); Content content = new Content(); content.setKeywords(strings); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(content, System.out); } }
Content c = new Content(); c.setData(data); c.setData(data); c.setContent(content); c.setStyleCode(styleCode);
@Override public CompletableFuture<Boolean> send(Mail mailMessage) throws MailException { Message message = new Message(); message.setSubject(new Content(mailMessage.getSubject()).withCharset(Charsets.UTF_8.toString())); message.setBody(new Body(new Content(mailMessage.getText()).withCharset(Charsets.UTF_8.toString())));
class Merge { public static Collector<Content, ?, Content> collector() { return Collector.of(Merge::new, Merge::accept, Merge::combiner, Merge::finisher); } private StringJoiner a = new StringJoiner(", "); private StringJoiner b = new StringJoiner(", "); private StringJoiner c = new StringJoiner(", "); private void accept(Content content) { a.add(content.getA()); b.add(content.getB()); c.add(content.getC()); } private Merge combiner(Merge second) { a.merge(second.a); b.merge(second.b); c.merge(second.c); return this; } private Content finisher() { return new Content(a.toString(), b.toString(), c.toString()); } }
String apiKey = ""; //API key provided by Google Console String deviceID="";//Device Id Content content = new Content(); //POJO class as above for standard message format content.addRegId(deviceID); content.createData("Title", "Notification Message"); URL url = new URL("https://android.googleapis.com/gcm/send"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("Authorization", "key="+apiKey); conn.setDoOutput(true); ObjectMapper mapper = new ObjectMapper(); DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); mapper.writeValue(wr, content); wr.flush(); wr.close(); responseCode = conn.getResponseCode();
/** * Constructs a new Content object. Callers should use the setter or fluent setter (with...) methods to initialize * any additional object members. * * @param data * The textual data of the content. */ public Content(String data) { setData(data); }
@Override public boolean sendEmail(List<String> emails, String subject, String body) { if (emails != null && !emails.isEmpty() && !StringUtils.isBlank(body)) { final SendEmailRequest request = new SendEmailRequest().withSource(Config.SUPPORT_EMAIL); Destination dest = new Destination().withToAddresses(emails); request.setDestination(dest); Content subjContent = new Content().withData(subject); Message msg = new Message().withSubject(subjContent); // Include a body in both text and HTML formats Content textContent = new Content().withData(body).withCharset(Config.DEFAULT_ENCODING); msg.setBody(new Body().withHtml(textContent)); request.setMessage(msg); Para.asyncExecute(new Runnable() { public void run() { sesclient.sendEmail(request); } }); return true; } return false; } }
private void sendSimpleMail(List<String> to, List<String> cc, List<String> ci, String object, String content, boolean htmlContent) { Destination destination = new Destination().withToAddresses(to).withBccAddresses(ci).withCcAddresses(cc); Content subject = new Content().withData(object); Content bodyContent = new Content().withData(content); Body body; if (htmlContent) { body = new Body().withHtml(bodyContent); } else { body = new Body().withText(bodyContent); } Message message = new Message().withSubject(subject).withBody(body); SendEmailRequest request = new SendEmailRequest().withSource(from).withDestination(destination).withMessage(message); try { AmazonSimpleEmailServiceClient client = new AmazonSimpleEmailServiceClient(); client.setRegion(region); client.sendEmail(request); } catch (Exception e) { LOGGER.error("Unable to send email to {} with subject '{}'", StringUtils.join(to, ","), subject, e); } }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (obj instanceof Content == false) return false; Content other = (Content) obj; if (other.getData() == null ^ this.getData() == null) return false; if (other.getData() != null && other.getData().equals(this.getData()) == false) return false; if (other.getCharset() == null ^ this.getCharset() == null) return false; if (other.getCharset() != null && other.getCharset().equals(this.getCharset()) == false) return false; return true; }
/** * <p> * The textual data of the content. * </p> * * @param data * The textual data of the content. * @return Returns a reference to this object so that method calls can be chained together. */ public Content withData(String data) { setData(data); return this; }
[DisableCache] public ActionResult YourMethod() { return new Content("This is not cached"); }
@Override public boolean sendEmail(List<String> emails, String subject, String body) { if (emails != null && !emails.isEmpty() && !StringUtils.isBlank(body)) { final SendEmailRequest request = new SendEmailRequest().withSource(Config.SUPPORT_EMAIL); Destination dest = new Destination().withToAddresses(emails); request.setDestination(dest); Content subjContent = new Content().withData(subject); Message msg = new Message().withSubject(subjContent); // Include a body in both text and HTML formats Content textContent = new Content().withData(body).withCharset(Config.DEFAULT_ENCODING); msg.setBody(new Body().withHtml(textContent)); request.setMessage(msg); Para.asyncExecute(new Runnable() { public void run() { sesclient.sendEmail(request); } }); return true; } return false; } }
@Override void makeSend(String to, String subject, String msg) { String accessKey = config.readString(ConfigProperty.SES_ACCESS_KEY); String secretKey = config.readString(ConfigProperty.SES_SECRET_KEY); if (!Strings.isNullOrEmpty(accessKey)) { Destination destination = new Destination().withToAddresses(to); Content subj = new Content().withData(subject); Content textBody = new Content().withData(msg); Body body = new Body().withHtml(textBody); Message message = new Message().withSubject(subj).withBody(body); SendEmailRequest req = new SendEmailRequest().withSource(config.readString(ConfigProperty.EMAIL_DEFAULT_FROM_NAME) + " <" + config.readString(ConfigProperty.EMAIL_DEFAULT_FROM) + ">") .withDestination(destination).withMessage(message); AmazonSimpleEmailService client = AmazonSimpleEmailServiceClientBuilder.standard() .withRegion(config.readString(ConfigProperty.SES_REGION)) .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey))) .build(); try { client.sendEmail(req); } finally { client.shutdown(); } } else { throw new IllegalStateException("SES Mail provider wasn't configured well."); } }
public Content unmarshall(StaxUnmarshallerContext context) throws Exception { Content content = new Content(); int originalDepth = context.getCurrentDepth(); int targetDepth = originalDepth + 1; if (context.isStartOfDocument()) targetDepth += 2; while (true) { int xmlEvent = context.nextEvent(); if (xmlEvent == XmlPullParser.END_DOCUMENT) break; if (xmlEvent == XmlPullParser.START_TAG) { if (context.testExpression("Data", targetDepth)) { content.setData(StringStaxUnmarshaller.getInstance().unmarshall(context)); continue; } if (context.testExpression("Charset", targetDepth)) { content.setCharset(StringStaxUnmarshaller.getInstance().unmarshall(context)); continue; } } else if (xmlEvent == XmlPullParser.END_TAG) { if (context.getCurrentDepth() < originalDepth) { break; } } } return content; }