Search in sources :

Example 56 with Multipart

use of javax.mail.Multipart in project fess-crawler by codelibs.

the class EmlExtractor method getBodyText.

protected String getBodyText(final MimeMessage message) {
    StringBuilder buf = new StringBuilder(1000);
    try {
        final Object content = message.getContent();
        if (content instanceof Multipart) {
            final Multipart multipart = (Multipart) content;
            final int count = multipart.getCount();
            for (int i = 0; i < count; i++) {
                final BodyPart bodyPart = multipart.getBodyPart(i);
                if (Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) {
                    appendAttachment(buf, bodyPart);
                } else if (bodyPart.isMimeType("text/plain")) {
                    buf.append(bodyPart.getContent().toString()).append(' ');
                } else if (bodyPart.isMimeType("text/html")) {
                    buf.append(bodyPart.getContent().toString()).append(' ');
                } else if (bodyPart.isMimeType("multipart/alternative") && bodyPart.getContent() instanceof Multipart) {
                    final Multipart alternativePart = (Multipart) bodyPart.getContent();
                    for (int j = 0; j < alternativePart.getCount(); j++) {
                        final BodyPart innerBodyPart = alternativePart.getBodyPart(j);
                        if (innerBodyPart.isMimeType("text/plain")) {
                            buf.append(innerBodyPart.getContent().toString()).append(' ');
                            break;
                        }
                    }
                }
            }
        } else if (content instanceof String) {
            buf.append(content.toString());
        }
    } catch (MessagingException | IOException e) {
        throw new ExtractException(e);
    }
    return buf.toString();
}
Also used : BodyPart(javax.mail.BodyPart) Multipart(javax.mail.Multipart) ExtractException(org.codelibs.fess.crawler.exception.ExtractException) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException)

Example 57 with Multipart

use of javax.mail.Multipart in project java_Projects by listar2000.

the class MailGenerator method creatMessage.

public Message creatMessage(Session session) throws Exception {
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(MailProperties.getEmail(), "BestBook.net", "UTF-8"));
    message.setRecipient(Message.RecipientType.TO, new InternetAddress(receiver, "customer", "UTF-8"));
    message.setSubject(mailTitle);
    Multipart multipart = new MimeMultipart();
    BodyPart textBody = new MimeBodyPart();
    textBody.setContent(mailContent, "text/html; charset=utf-8");
    multipart.addBodyPart(textBody);
    if (!file.exists()) {
        textBody.setContent("�޷���ʾ����", "text/html; charset=utf-8");
    } else {
        BodyPart fileBody = new MimeBodyPart();
        FileDataSource dataSource = new FileDataSource(file);
        fileBody.setDataHandler(new DataHandler(dataSource));
        multipart.addBodyPart(fileBody);
    }
    message.setContent(multipart);
    message.saveChanges();
    return message;
}
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) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) FileDataSource(javax.activation.FileDataSource) DataHandler(javax.activation.DataHandler) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 58 with Multipart

use of javax.mail.Multipart in project serverless by bluenimble.

the class SmtpMessenger method send.

@Override
public void send(Sender sender, Recipient[] recipients, String subject, String content, ApiStreamSource... attachments) throws MessengerException {
    ClassLoader pcl = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
    // creates a new e-mail message
    try {
        if (sender == null) {
            sender = NoSender;
        }
        Message message = new MimeMessage(session);
        String senderEmail = sender.id();
        if (Lang.isNullOrEmpty(senderEmail)) {
            senderEmail = user;
        }
        String senderName = sender.name();
        if (Lang.isNullOrEmpty(senderName)) {
            message.setFrom(new InternetAddress(senderEmail));
        } else {
            message.setFrom(new InternetAddress(senderEmail, senderName));
        }
        InternetAddress[] toAddresses = new InternetAddress[recipients.length];
        for (int i = 0; i < recipients.length; i++) {
            toAddresses[i] = new InternetAddress(recipients[i].id());
        }
        message.setRecipients(Message.RecipientType.TO, toAddresses);
        message.setSubject(subject);
        message.setSentDate(new Date());
        // creates message part
        MimeBodyPart messageText = new MimeBodyPart();
        messageText.setContent(content, ApiContentTypes.Html);
        // creates multi-part
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageText);
        // adds attachments
        if (attachments != null && attachments.length > 0) {
            for (final ApiStreamSource attachment : attachments) {
                MimeBodyPart mbd = new MimeBodyPart();
                DataSource ds = new DataSource() {

                    @Override
                    public String getContentType() {
                        return attachment.contentType();
                    }

                    @Override
                    public InputStream getInputStream() throws IOException {
                        return attachment.stream();
                    }

                    @Override
                    public String getName() {
                        return attachment.name();
                    }

                    @Override
                    public OutputStream getOutputStream() throws IOException {
                        throw new UnsupportedOperationException("getOutputStream not supported");
                    }
                };
                mbd.setDataHandler(new DataHandler(ds));
                multipart.addBodyPart(mbd);
            }
        }
        // sets the multi-part as e-mail's content
        message.setContent(multipart);
        // sends the e-mail
        Transport.send(message);
    } catch (Exception ex) {
        throw new MessengerException(ex.getMessage(), ex);
    } finally {
        Thread.currentThread().setContextClassLoader(pcl);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MessengerException(com.bluenimble.platform.messaging.MessengerException) DataHandler(javax.activation.DataHandler) ApiStreamSource(com.bluenimble.platform.api.ApiStreamSource) Date(java.util.Date) IOException(java.io.IOException) MessengerException(com.bluenimble.platform.messaging.MessengerException) DataSource(javax.activation.DataSource) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 59 with Multipart

use of javax.mail.Multipart in project i2p.i2p-bote by i2p.

the class Email method setContent.

/**
 * Sets the message text and adds attachments.
 * @param text
 * @param attachments Can be <code>null</code>
 * @throws MessagingException
 */
public void setContent(String text, List<Attachment> attachments) throws MessagingException {
    if (attachments == null || attachments.isEmpty())
        setText(text, "UTF-8");
    else {
        Multipart multiPart = new MimeMultipart();
        MimeBodyPart textPart = new MimeBodyPart();
        textPart.setText(text, "UTF-8");
        multiPart.addBodyPart(textPart);
        attach(multiPart, attachments);
        setContent(multiPart);
    }
}
Also used : Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 60 with Multipart

use of javax.mail.Multipart in project perun by CESNET.

the class MessagePreparator method createHtmlMessage.

/**
 * Creates HTML message from freemarker template
 *
 * @return
 * @throws Exception
 */
private BodyPart createHtmlMessage() throws Exception {
    Multipart htmlContent = new MimeMultipart("related");
    BodyPart htmlPage = new MimeBodyPart();
    htmlPage.setDataHandler(createDataHandler(content.getBytes(StandardCharsets.UTF_8), "text/html;charset=utf-8"));
    htmlContent.addBodyPart(htmlPage);
    BodyPart htmlPart = new MimeBodyPart();
    htmlPart.setContent(htmlContent);
    logger.debug("HTML MESSAGE CREATED ; content: " + content);
    return htmlPart;
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Aggregations

Multipart (javax.mail.Multipart)148 MimeMultipart (javax.mail.internet.MimeMultipart)107 MimeBodyPart (javax.mail.internet.MimeBodyPart)92 MimeMessage (javax.mail.internet.MimeMessage)82 BodyPart (javax.mail.BodyPart)62 InternetAddress (javax.mail.internet.InternetAddress)61 MessagingException (javax.mail.MessagingException)58 Session (javax.mail.Session)44 DataHandler (javax.activation.DataHandler)42 IOException (java.io.IOException)37 Properties (java.util.Properties)37 Date (java.util.Date)30 Message (javax.mail.Message)30 FileDataSource (javax.activation.FileDataSource)27 DataSource (javax.activation.DataSource)24 InputStream (java.io.InputStream)23 File (java.io.File)21 Part (javax.mail.Part)21 Test (org.junit.Test)16 UnsupportedEncodingException (java.io.UnsupportedEncodingException)11