Search in sources :

Example 1 with MessagePart

use of org.mycore.common.MCRMailer.EMail.MessagePart in project mycore by MyCoRe-Org.

the class MCRMailer method send.

/**
 * Sends email. When sending email fails (for example, outgoing mail server
 * is not responding), sending will be retried after five minutes. This is
 * done up to 10 times.
 *
 * @param from
 *            the sender of the email
 * @param replyTo
 *            the reply-to addresses as a List of Strings, may be null
 * @param to
 *            the recipients of the email as a List of Strings
 * @param bcc
 *            the bcc recipients of the email as a List of Strings, may be
 *            null
 * @param subject
 *            the subject of the email
 * @param body
 *            the text of the email
 * @param parts
 *            a List of URL strings which should be added as parts, may be
 *            null
 */
public static void send(final String from, final List<String> replyTo, final List<String> to, final List<String> bcc, final String subject, final String body, final List<String> parts) {
    EMail mail = new EMail();
    mail.from = from;
    mail.replyTo = replyTo;
    mail.to = to;
    mail.bcc = bcc;
    mail.subject = subject;
    mail.msgParts = new ArrayList<>();
    mail.msgParts.add(new MessagePart(body));
    mail.parts = parts;
    send(mail);
}
Also used : MessagePart(org.mycore.common.MCRMailer.EMail.MessagePart)

Example 2 with MessagePart

use of org.mycore.common.MCRMailer.EMail.MessagePart in project mycore by MyCoRe-Org.

the class MCRMailer method trySending.

private static void trySending(EMail mail) throws Exception {
    MimeMessage msg = new MimeMessage(mailSession);
    msg.setFrom(EMail.buildAddress(mail.from));
    Optional<List<InternetAddress>> toList = EMail.buildAddressList(mail.to);
    if (toList.isPresent())
        msg.addRecipients(Message.RecipientType.TO, toList.get().toArray(new InternetAddress[toList.get().size()]));
    Optional<List<InternetAddress>> replyToList = EMail.buildAddressList(mail.replyTo);
    if (replyToList.isPresent())
        msg.setReplyTo((replyToList.get().toArray(new InternetAddress[replyToList.get().size()])));
    Optional<List<InternetAddress>> bccList = EMail.buildAddressList(mail.bcc);
    if (bccList.isPresent())
        msg.addRecipients(Message.RecipientType.BCC, bccList.get().toArray(new InternetAddress[bccList.get().size()]));
    msg.setSentDate(new Date());
    msg.setSubject(mail.subject, encoding);
    if (mail.parts != null && !mail.parts.isEmpty() || mail.msgParts != null && mail.msgParts.size() > 1) {
        Multipart multipart = new MimeMultipart();
        // Create the message part
        MimeBodyPart messagePart = new MimeBodyPart();
        if (mail.msgParts.size() > 1) {
            multipart = new MimeMultipart("mixed");
            MimeMultipart alternative = new MimeMultipart("alternative");
            for (MessagePart m : mail.msgParts) {
                messagePart = new MimeBodyPart();
                messagePart.setText(m.message, encoding, m.type.value());
                alternative.addBodyPart(messagePart);
            }
            messagePart = new MimeBodyPart();
            messagePart.setContent(alternative);
            multipart.addBodyPart(messagePart);
        } else {
            Optional<MessagePart> plainMsg = mail.getTextMessage();
            if (plainMsg.isPresent()) {
                messagePart.setText(plainMsg.get().message, encoding);
                multipart.addBodyPart(messagePart);
            }
        }
        if (mail.parts != null && !mail.parts.isEmpty()) {
            for (String part : mail.parts) {
                messagePart = new MimeBodyPart();
                URL url = new URL(part);
                DataSource source = new URLDataSource(url);
                messagePart.setDataHandler(new DataHandler(source));
                String fileName = url.getPath();
                if (fileName.contains("\\")) {
                    fileName = fileName.substring(fileName.lastIndexOf("\\") + 1);
                } else if (fileName.contains("/")) {
                    fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
                }
                messagePart.setFileName(fileName);
                multipart.addBodyPart(messagePart);
            }
        }
        msg.setContent(multipart);
    } else {
        Optional<MessagePart> plainMsg = mail.getTextMessage();
        if (plainMsg.isPresent()) {
            msg.setText(plainMsg.get().message, encoding);
        }
    }
    LOGGER.info("Sending e-mail to {}", mail.to);
    Transport.send(msg);
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) URLDataSource(javax.activation.URLDataSource) MessagePart(org.mycore.common.MCRMailer.EMail.MessagePart) DataHandler(javax.activation.DataHandler) Date(java.util.Date) URL(java.net.URL) URLDataSource(javax.activation.URLDataSource) DataSource(javax.activation.DataSource) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) List(java.util.List) ArrayList(java.util.ArrayList) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Aggregations

MessagePart (org.mycore.common.MCRMailer.EMail.MessagePart)2 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 DataHandler (javax.activation.DataHandler)1 DataSource (javax.activation.DataSource)1 URLDataSource (javax.activation.URLDataSource)1 Multipart (javax.mail.Multipart)1 InternetAddress (javax.mail.internet.InternetAddress)1 MimeBodyPart (javax.mail.internet.MimeBodyPart)1 MimeMessage (javax.mail.internet.MimeMessage)1 MimeMultipart (javax.mail.internet.MimeMultipart)1