Search in sources :

Example 1 with IEmailAttachmentDataSource

use of com.helger.smtp.data.IEmailAttachmentDataSource in project ph-web by phax.

the class MailConverter method fillMimeMessageUnsafe.

public static void fillMimeMessageUnsafe(@Nonnull final MimeMessage aMIMEMessage, @Nonnull final IMutableEmailData aMailData, @Nullable final Charset aCharset) throws MessagingException {
    if (aMailData.getFrom() != null)
        aMIMEMessage.setFrom(InternetAddressHelper.getAsInternetAddress(aMailData.getFrom(), aCharset));
    aMIMEMessage.setReplyTo(InternetAddressHelper.getAsInternetAddressArray(aMailData.replyTo(), aCharset));
    aMIMEMessage.setRecipients(Message.RecipientType.TO, InternetAddressHelper.getAsInternetAddressArray(aMailData.to(), aCharset));
    aMIMEMessage.setRecipients(Message.RecipientType.CC, InternetAddressHelper.getAsInternetAddressArray(aMailData.cc(), aCharset));
    aMIMEMessage.setRecipients(Message.RecipientType.BCC, InternetAddressHelper.getAsInternetAddressArray(aMailData.bcc(), aCharset));
    if (aMailData.getSentDateTime() != null)
        aMIMEMessage.setSentDate(TypeConverter.convert(aMailData.getSentDateTime(), Date.class));
    if (aMailData.getSubject() != null)
        if (aCharset != null)
            setSubject(aMIMEMessage, aMailData.getSubject(), aCharset);
        else
            aMIMEMessage.setSubject(aMailData.getSubject());
    final MimeMultipart aMixedMultipart = new MimeMultipart();
    // build text part
    {
        final MimeBodyPart aBodyPart = new MimeBodyPart();
        if (aMailData.getEmailType().isHTML()) {
            // HTML text
            if (aCharset != null) {
                aBodyPart.setContent(aMailData.getBody(), new MimeType(CMimeType.TEXT_HTML).addParameter(CMimeType.PARAMETER_NAME_CHARSET, aCharset.name()).getAsString());
            } else
                aBodyPart.setContent(aMailData.getBody(), CMimeType.TEXT_HTML.getAsString());
        } else {
            // Plain text
            if (aCharset != null)
                setText(aBodyPart, aMailData.getBody(), aCharset);
            else
                aBodyPart.setText(aMailData.getBody());
        }
        aMixedMultipart.addBodyPart(aBodyPart);
    }
    // Does the mail data contain attachments?
    final IMutableEmailAttachmentList aAttachments = aMailData.getAttachments();
    if (aAttachments != null)
        for (final IEmailAttachmentDataSource aDS : aAttachments.getAsDataSourceList()) {
            final MimeBodyPart aAttachmentPart = new MimeBodyPart();
            aAttachmentPart.setDisposition(aDS.getDisposition().getID());
            aAttachmentPart.setFileName(aDS.getName());
            aAttachmentPart.setDataHandler(aDS.getAsDataHandler());
            aMixedMultipart.addBodyPart(aAttachmentPart);
        }
    // set mixed multipart content
    aMIMEMessage.setContent(aMixedMultipart);
}
Also used : MimeMultipart(javax.mail.internet.MimeMultipart) IMutableEmailAttachmentList(com.helger.smtp.data.IMutableEmailAttachmentList) IEmailAttachmentDataSource(com.helger.smtp.data.IEmailAttachmentDataSource) MimeBodyPart(javax.mail.internet.MimeBodyPart) CMimeType(com.helger.commons.mime.CMimeType) MimeType(com.helger.commons.mime.MimeType)

Aggregations

CMimeType (com.helger.commons.mime.CMimeType)1 MimeType (com.helger.commons.mime.MimeType)1 IEmailAttachmentDataSource (com.helger.smtp.data.IEmailAttachmentDataSource)1 IMutableEmailAttachmentList (com.helger.smtp.data.IMutableEmailAttachmentList)1 MimeBodyPart (javax.mail.internet.MimeBodyPart)1 MimeMultipart (javax.mail.internet.MimeMultipart)1