use of software.amazon.awssdk.core.SdkBytes in project aws-doc-sdk-examples by awsdocs.
the class SendMessages method send.
public void send(byte[] attachment, String emailAddress) throws MessagingException, IOException {
MimeMessage message = null;
Session session = Session.getDefaultInstance(new Properties());
// Create a new MimeMessage object.
message = new MimeMessage(session);
// Add subject, from and to lines.
message.setSubject(subject, "UTF-8");
message.setFrom(new InternetAddress(sender));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailAddress));
// Create a multipart/alternative child container.
MimeMultipart msgBody = new MimeMultipart("alternative");
// Create a wrapper for the HTML and text parts.
MimeBodyPart wrap = new MimeBodyPart();
// Define the text part.
MimeBodyPart textPart = new MimeBodyPart();
textPart.setContent(bodyText, "text/plain; charset=UTF-8");
// Define the HTML part.
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(bodyHTML, "text/html; charset=UTF-8");
// Add the text and HTML parts to the child container.
msgBody.addBodyPart(textPart);
msgBody.addBodyPart(htmlPart);
// Add the child container to the wrapper object.
wrap.setContent(msgBody);
// Create a multipart/mixed parent container.
MimeMultipart msg = new MimeMultipart("mixed");
// Add the parent container to the message.
message.setContent(msg);
// Add the multipart/alternative part to the message.
msg.addBodyPart(wrap);
// Define the attachment
MimeBodyPart att = new MimeBodyPart();
DataSource fds = new ByteArrayDataSource(attachment, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
att.setDataHandler(new DataHandler(fds));
String reportName = "PhotoReport.xls";
att.setFileName(reportName);
// Add the attachment to the message.
msg.addBodyPart(att);
// Try to send the email.
try {
System.out.println("Attempting to send an email through Amazon SES " + "using the AWS SDK for Java...");
Region region = Region.US_WEST_2;
SesClient client = SesClient.builder().region(region).build();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
message.writeTo(outputStream);
ByteBuffer buf = ByteBuffer.wrap(outputStream.toByteArray());
byte[] arr = new byte[buf.remaining()];
buf.get(arr);
SdkBytes data = SdkBytes.fromByteArray(arr);
RawMessage rawMessage = RawMessage.builder().data(data).build();
SendRawEmailRequest rawEmailRequest = SendRawEmailRequest.builder().rawMessage(rawMessage).build();
client.sendRawEmail(rawEmailRequest);
} catch (SesException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
System.out.println("Email sent with attachment");
}
use of software.amazon.awssdk.core.SdkBytes in project aws-doc-sdk-examples by awsdocs.
the class SendMessages method send.
public void send(byte[] attachment, String emailAddress) throws MessagingException, IOException {
MimeMessage message = null;
Session session = Session.getDefaultInstance(new Properties());
// Create a new MimeMessage object.
message = new MimeMessage(session);
// Add subject, from and to lines.
message.setSubject(subject, "UTF-8");
message.setFrom(new InternetAddress(sender));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailAddress));
// Create a multipart/alternative child container.
MimeMultipart msgBody = new MimeMultipart("alternative");
// Create a wrapper for the HTML and text parts.
MimeBodyPart wrap = new MimeBodyPart();
// Define the text part.
MimeBodyPart textPart = new MimeBodyPart();
textPart.setContent(bodyText, "text/plain; charset=UTF-8");
// Define the HTML part.
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(bodyHTML, "text/html; charset=UTF-8");
// Add the text and HTML parts to the child container.
msgBody.addBodyPart(textPart);
msgBody.addBodyPart(htmlPart);
// Add the child container to the wrapper object.
wrap.setContent(msgBody);
// Create a multipart/mixed parent container.
MimeMultipart msg = new MimeMultipart("mixed");
// Add the parent container to the message.
message.setContent(msg);
// Add the multipart/alternative part to the message.
msg.addBodyPart(wrap);
// Define the attachment
MimeBodyPart att = new MimeBodyPart();
DataSource fds = new ByteArrayDataSource(attachment, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
att.setDataHandler(new DataHandler(fds));
String reportName = "VideoReport.xls";
att.setFileName(reportName);
// Add the attachment to the message.
msg.addBodyPart(att);
// Try to send the email.
try {
System.out.println("Attempting to send an email through Amazon SES " + "using the AWS SDK for Java...");
Region region = Region.US_WEST_2;
SesClient client = SesClient.builder().credentialsProvider(EnvironmentVariableCredentialsProvider.create()).region(region).build();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
message.writeTo(outputStream);
ByteBuffer buf = ByteBuffer.wrap(outputStream.toByteArray());
byte[] arr = new byte[buf.remaining()];
buf.get(arr);
SdkBytes data = SdkBytes.fromByteArray(arr);
RawMessage rawMessage = RawMessage.builder().data(data).build();
SendRawEmailRequest rawEmailRequest = SendRawEmailRequest.builder().rawMessage(rawMessage).build();
client.sendRawEmail(rawEmailRequest);
} catch (SesException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
System.out.println("Email sent with attachment");
}
use of software.amazon.awssdk.core.SdkBytes in project aws-doc-sdk-examples by awsdocs.
the class CreateFunction method createNewFunction.
// snippet-start:[cloudfront.java2.function.main]
public static String createNewFunction(CloudFrontClient cloudFrontClient, String functionName, String filePath) {
try {
InputStream is = new FileInputStream(filePath);
SdkBytes functionCode = SdkBytes.fromInputStream(is);
FunctionConfig config = FunctionConfig.builder().comment("Created by using the CloudFront Java API").runtime(FunctionRuntime.CLOUDFRONT_JS_1_0).build();
CreateFunctionRequest functionRequest = CreateFunctionRequest.builder().name(functionName).functionCode(functionCode).functionConfig(config).build();
CreateFunctionResponse response = cloudFrontClient.createFunction(functionRequest);
return response.functionSummary().functionMetadata().functionARN();
} catch (CloudFrontException | FileNotFoundException e) {
System.err.println(e.getMessage());
System.exit(1);
}
return "";
}
use of software.amazon.awssdk.core.SdkBytes in project aws-doc-sdk-examples by awsdocs.
the class DisplayFacesFrame method displayAllFaces.
// snippet-start:[rekognition.java2.display_faces.main]
public static void displayAllFaces(S3Client s3, RekognitionClient rekClient, String sourceImage, String bucketName) {
int height = 0;
int width = 0;
byte[] data = getObjectBytes(s3, bucketName, sourceImage);
InputStream is = new ByteArrayInputStream(data);
try {
SdkBytes sourceBytes = SdkBytes.fromInputStream(is);
image = ImageIO.read(sourceBytes.asInputStream());
width = image.getWidth();
height = image.getHeight();
// Create an Image object for the source image
software.amazon.awssdk.services.rekognition.model.Image souImage = Image.builder().bytes(sourceBytes).build();
DetectFacesRequest facesRequest = DetectFacesRequest.builder().attributes(Attribute.ALL).image(souImage).build();
result = rekClient.detectFaces(facesRequest);
// Show the bounding box info for each face.
List<FaceDetail> faceDetails = result.faceDetails();
for (FaceDetail face : faceDetails) {
BoundingBox box = face.boundingBox();
float left = width * box.left();
float top = height * box.top();
System.out.println("Face:");
System.out.println("Left: " + (int) left);
System.out.println("Top: " + (int) top);
System.out.println("Face Width: " + (int) (width * box.width()));
System.out.println("Face Height: " + (int) (height * box.height()));
System.out.println();
}
// Create the frame and panel.
JFrame frame = new JFrame("RotateImage");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DisplayFacesFrame panel = new DisplayFacesFrame(image);
panel.setPreferredSize(new Dimension(image.getWidth() / scale, image.getHeight() / scale));
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
} catch (RekognitionException | FileNotFoundException e) {
System.out.println(e.getMessage());
System.exit(1);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
use of software.amazon.awssdk.core.SdkBytes in project aws-doc-sdk-examples by awsdocs.
the class RecognizeCelebrities method recognizeAllCelebrities.
// snippet-start:[rekognition.java2.recognize_celebs.main]
public static void recognizeAllCelebrities(RekognitionClient rekClient, String sourceImage) {
try {
InputStream sourceStream = new FileInputStream(sourceImage);
SdkBytes sourceBytes = SdkBytes.fromInputStream(sourceStream);
Image souImage = Image.builder().bytes(sourceBytes).build();
RecognizeCelebritiesRequest request = RecognizeCelebritiesRequest.builder().image(souImage).build();
RecognizeCelebritiesResponse result = rekClient.recognizeCelebrities(request);
List<Celebrity> celebs = result.celebrityFaces();
System.out.println(celebs.size() + " celebrity(s) were recognized.\n");
for (Celebrity celebrity : celebs) {
System.out.println("Celebrity recognized: " + celebrity.name());
System.out.println("Celebrity ID: " + celebrity.id());
System.out.println("Further information (if available):");
for (String url : celebrity.urls()) {
System.out.println(url);
}
System.out.println();
}
System.out.println(result.unrecognizedFaces().size() + " face(s) were unrecognized.");
} catch (RekognitionException | FileNotFoundException e) {
System.out.println(e.getMessage());
System.exit(1);
}
}
Aggregations