Search in sources :

Example 81 with DataSource

use of javax.activation.DataSource in project Activiti by Activiti.

the class MailActivityBehavior method execute.

@Override
public void execute(DelegateExecution execution) {
    boolean doIgnoreException = Boolean.parseBoolean(getStringFromField(ignoreException, execution));
    String exceptionVariable = getStringFromField(exceptionVariableName, execution);
    Email email = null;
    try {
        String toStr = getStringFromField(to, execution);
        String fromStr = getStringFromField(from, execution);
        String ccStr = getStringFromField(cc, execution);
        String bccStr = getStringFromField(bcc, execution);
        String subjectStr = getStringFromField(subject, execution);
        String textStr = textVar == null ? getStringFromField(text, execution) : getStringFromField(getExpression(execution, textVar), execution);
        String htmlStr = htmlVar == null ? getStringFromField(html, execution) : getStringFromField(getExpression(execution, htmlVar), execution);
        String charSetStr = getStringFromField(charset, execution);
        List<File> files = new LinkedList<File>();
        List<DataSource> dataSources = new LinkedList<DataSource>();
        getFilesFromFields(attachments, execution, files, dataSources);
        email = createEmail(textStr, htmlStr, attachmentsExist(files, dataSources));
        addTo(email, toStr);
        setFrom(email, fromStr, execution.getTenantId());
        addCc(email, ccStr);
        addBcc(email, bccStr);
        setSubject(email, subjectStr);
        setMailServerProperties(email, execution.getTenantId());
        setCharset(email, charSetStr);
        attach(email, files, dataSources);
        email.send();
    } catch (ActivitiException e) {
        handleException(execution, e.getMessage(), e, doIgnoreException, exceptionVariable);
    } catch (EmailException e) {
        handleException(execution, "Could not send e-mail in execution " + execution.getId(), e, doIgnoreException, exceptionVariable);
    }
    leave(execution);
}
Also used : ActivitiException(org.activiti.engine.ActivitiException) Email(org.apache.commons.mail.Email) HtmlEmail(org.apache.commons.mail.HtmlEmail) MultiPartEmail(org.apache.commons.mail.MultiPartEmail) SimpleEmail(org.apache.commons.mail.SimpleEmail) EmailException(org.apache.commons.mail.EmailException) File(java.io.File) LinkedList(java.util.LinkedList) DataSource(javax.activation.DataSource)

Example 82 with DataSource

use of javax.activation.DataSource 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 = "WorkReport.xls";
    att.setFileName(reportName);
    // Add the attachment to the message.
    msg.addBodyPart(att);
    // 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");
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) SesClient(software.amazon.awssdk.services.ses.SesClient) DataHandler(javax.activation.DataHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Properties(java.util.Properties) ByteBuffer(java.nio.ByteBuffer) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) DataSource(javax.activation.DataSource) SdkBytes(software.amazon.awssdk.core.SdkBytes) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) Region(software.amazon.awssdk.regions.Region) SendRawEmailRequest(software.amazon.awssdk.services.ses.model.SendRawEmailRequest) MimeBodyPart(javax.mail.internet.MimeBodyPart) RawMessage(software.amazon.awssdk.services.ses.model.RawMessage) SesException(software.amazon.awssdk.services.ses.model.SesException) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) Session(javax.mail.Session)

Example 83 with DataSource

use of javax.activation.DataSource in project aws-doc-sdk-examples by awsdocs.

the class SendMessage 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 = "WorkReport.xls";
    att.setFileName(reportName);
    // Add the attachment to the message
    msg.addBodyPart(att);
    // 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");
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) SesClient(software.amazon.awssdk.services.ses.SesClient) DataHandler(javax.activation.DataHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Properties(java.util.Properties) ByteBuffer(java.nio.ByteBuffer) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) DataSource(javax.activation.DataSource) SdkBytes(software.amazon.awssdk.core.SdkBytes) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) Region(software.amazon.awssdk.regions.Region) SendRawEmailRequest(software.amazon.awssdk.services.ses.model.SendRawEmailRequest) MimeBodyPart(javax.mail.internet.MimeBodyPart) RawMessage(software.amazon.awssdk.services.ses.model.RawMessage) SesException(software.amazon.awssdk.services.ses.model.SesException) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) Session(javax.mail.Session)

Example 84 with DataSource

use of javax.activation.DataSource in project aws-doc-sdk-examples by awsdocs.

the class SendMessageAttachment method sendemailAttachment.

// snippet-start:[ses.java2.sendmessageattachment.main]
public static void sendemailAttachment(SesClient client, String sender, String recipient, String subject, String bodyText, String bodyHTML, String fileLocation) throws AddressException, MessagingException, IOException {
    java.io.File theFile = new java.io.File(fileLocation);
    byte[] fileContent = Files.readAllBytes(theFile.toPath());
    Session session = Session.getDefaultInstance(new Properties());
    // Create a new MimeMessage object
    MimeMessage 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(recipient));
    // 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(fileContent, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    att.setDataHandler(new DataHandler(fds));
    String reportName = "WorkReport.xls";
    att.setFileName(reportName);
    // Add the attachment to the message.
    msg.addBodyPart(att);
    try {
        System.out.println("Attempting to send an email through Amazon SES " + "using the AWS SDK for Java...");
        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");
// snippet-end:[ses.java2.sendmessageattachment.main]
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) DataHandler(javax.activation.DataHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Properties(java.util.Properties) ByteBuffer(java.nio.ByteBuffer) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) DataSource(javax.activation.DataSource) SdkBytes(software.amazon.awssdk.core.SdkBytes) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) SendRawEmailRequest(software.amazon.awssdk.services.ses.model.SendRawEmailRequest) MimeBodyPart(javax.mail.internet.MimeBodyPart) RawMessage(software.amazon.awssdk.services.ses.model.RawMessage) SesException(software.amazon.awssdk.services.ses.model.SesException) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) Session(javax.mail.Session)

Example 85 with DataSource

use of javax.activation.DataSource in project SpringStepByStep by JavaProgrammerLB.

the class SendAttachmentInEmail method main.

public static void main(String[] args) {
    // Recipient's email ID needs to be mentioned.
    String to = "destinationemail@gmail.com";
    // Sender's email ID needs to be mentioned
    String from = "fromemail@gmail.com";
    // change accordingly
    final String username = "manishaspatil";
    // change accordingly
    final String password = "******";
    // Assuming you are sending email through relay.jangosmtp.net
    String host = "relay.jangosmtp.net";
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.port", "25");
    // Get the Session object.
    Session session = Session.getInstance(props, new javax.mail.Authenticator() {

        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    });
    try {
        // Create a default MimeMessage object.
        Message message = new MimeMessage(session);
        // Set From: header field of the header.
        message.setFrom(new InternetAddress(from));
        // Set To: header field of the header.
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
        // Set Subject: header field
        message.setSubject("Testing Subject");
        // Create the message part
        BodyPart messageBodyPart = new MimeBodyPart();
        // Now set the actual message
        messageBodyPart.setText("This is message body");
        // Create a multipar message
        Multipart multipart = new MimeMultipart();
        // Set text message part
        multipart.addBodyPart(messageBodyPart);
        // Part two is attachment
        messageBodyPart = new MimeBodyPart();
        String filename = "/home/manisha/file.txt";
        DataSource source = new FileDataSource(filename);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(filename);
        multipart.addBodyPart(messageBodyPart);
        // Send the complete message parts
        message.setContent(multipart);
        // Send message
        Transport.send(message);
        System.out.println("Sent message successfully....");
    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) DataHandler(javax.activation.DataHandler) Properties(java.util.Properties) FileDataSource(javax.activation.FileDataSource) DataSource(javax.activation.DataSource) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) FileDataSource(javax.activation.FileDataSource) MimeBodyPart(javax.mail.internet.MimeBodyPart) Session(javax.mail.Session) PasswordAuthentication(javax.mail.PasswordAuthentication)

Aggregations

DataSource (javax.activation.DataSource)196 DataHandler (javax.activation.DataHandler)114 FileDataSource (javax.activation.FileDataSource)60 MimeBodyPart (javax.mail.internet.MimeBodyPart)53 MimeMultipart (javax.mail.internet.MimeMultipart)48 MimeMessage (javax.mail.internet.MimeMessage)39 InputStream (java.io.InputStream)37 IOException (java.io.IOException)36 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)35 ByteArrayInputStream (java.io.ByteArrayInputStream)33 File (java.io.File)33 ByteArrayOutputStream (java.io.ByteArrayOutputStream)30 Test (org.junit.Test)30 InternetAddress (javax.mail.internet.InternetAddress)27 Properties (java.util.Properties)26 MessagingException (javax.mail.MessagingException)25 Multipart (javax.mail.Multipart)24 Session (javax.mail.Session)21 BodyPart (javax.mail.BodyPart)19 ArrayList (java.util.ArrayList)16