Example usage for javax.mail.internet MimeMultipart MimeMultipart

List of usage examples for javax.mail.internet MimeMultipart MimeMultipart

Introduction

In this page you can find the example usage for javax.mail.internet MimeMultipart MimeMultipart.

Prototype

public MimeMultipart() 

Source Link

Document

Default constructor.

Usage

From source file:org.unitime.commons.JavaMailWrapper.java

public JavaMailWrapper() {
    Properties p = ApplicationProperties.getProperties();
    if (p.getProperty("mail.smtp.host") == null && p.getProperty("tmtbl.smtp.host") != null)
        p.setProperty("mail.smtp.host", p.getProperty("tmtbl.smtp.host"));

    final String user = ApplicationProperty.EmailSmtpUser.value();
    final String password = ApplicationProperty.EmailSmtpPassword.value();

    Authenticator a = null;/* w w  w  .j  ava2s  . c  o  m*/
    if (user != null && password != null) {
        p.setProperty("mail.smtp.user", user);
        p.setProperty("mail.smtp.auth", "true");
        a = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(user, password);
            }
        };
    }

    iMailSession = javax.mail.Session.getDefaultInstance(p, a);
    iMail = new MimeMessage(iMailSession);
    iBody = new MimeMultipart();
}

From source file:com.trivago.mail.pigeon.mail.MailFacade.java

public void sendMail(MailTransport mailTransport) {
    log.debug("Mail delivery started");
    File propertyfile = ((PropertiesConfiguration) Settings.create().getConfiguration()).getFile();
    Properties config = new Properties();

    try {/* w w w .  j  av  a 2  s  .c  o m*/
        config.load(new FileReader(propertyfile));
    } catch (IOException e) {
        log.error(e);
    }

    Session session = Session.getDefaultInstance(config);

    log.debug("Received session");

    MimeMessage message = new MimeMessage(session);

    String to = mailTransport.getTo();
    String from = mailTransport.getFrom();
    String replyTo = mailTransport.getReplyTo();
    String subject = mailTransport.getSubject();
    String html = mailTransport.getHtml();
    String text = mailTransport.getText();

    try {
        Address fromAdr = new InternetAddress(from);
        Address toAdr = new InternetAddress(to);
        Address rplyAdr = new InternetAddress(replyTo);

        message.setSubject(subject);
        message.setFrom(fromAdr);
        message.setRecipient(Message.RecipientType.TO, toAdr);
        message.setReplyTo(new Address[] { rplyAdr });
        message.setSender(fromAdr);
        message.addHeader("Return-path", replyTo);
        message.addHeader("X-TRV-MID", mailTransport.getmId());
        message.addHeader("X-TRV-UID", mailTransport.getuId());

        // Content
        MimeBodyPart messageTextPart = new MimeBodyPart();
        messageTextPart.setText(text);
        messageTextPart.setContent(html, "text/html");

        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageTextPart);

        // Put parts in message
        message.setContent(multipart);

        log.debug("Dispatching message");
        Transport.send(message);
        log.debug("Mail delivery ended");
    } catch (MessagingException e) {
        log.error(e);
    }

}

From source file:com.spartasystems.holdmail.util.TestMailClient.java

private void createMultiMimePart(Message message, String textBody, String htmlBody) throws MessagingException {
    Multipart mp = new MimeMultipart();

    if (StringUtils.isNotBlank(textBody)) {
        mp.addBodyPart(createTextBodyPart(textBody));
    }/*from  w w w  . j a  va2s  .co m*/
    if (StringUtils.isNotBlank(htmlBody)) {
        mp.addBodyPart(createHtmlBodyPart(htmlBody));
    }

    message.setContent(mp);
}

From source file:com.synyx.greetingcard.mail.OpenCmsMailService.java

public void sendMultipartMail(MessageConfig config, DataSource ds, String filename) throws MessagingException {
    log.debug("Sending multipart message " + config);

    Session session = getSession();//from   w  w w.  ja v  a  2 s  .c  o m
    MimeMultipart multipart = new MimeMultipart();
    MimeBodyPart html = new MimeBodyPart();
    html.setContent(config.getContent(), config.getContentType());
    html.setHeader("MIME-Version", "1.0");
    html.setHeader("Content-Type", html.getContentType());
    multipart.addBodyPart(html);

    BodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setDataHandler(new DataHandler(ds));
    messageBodyPart.setFileName(filename);
    multipart.addBodyPart(messageBodyPart);

    final MimeMessage message = new MimeMessage(session);
    message.setContent(multipart);
    try {
        message.setFrom(new InternetAddress(config.getFrom(), config.getFromName()));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(config.getTo(), config.getToName()));
    } catch (UnsupportedEncodingException ex) {
        throw new MessagingException("Setting from or to failed", ex);
    }

    message.setSubject(config.getSubject());

    // we don't send in a new Thread so that we get the Exception
    Transport.send(message);
}

From source file:org.masukomi.aspirin.core.Bouncer.java

/**
 * This generates a response to the Return-Path address, or the address of
 * the message's sender if the Return-Path is not available. Note that this
 * is different than a mail-client's reply, which would use the Reply-To or
 * From header./*from  w ww . j a v a2  s . co m*/
 * 
 * @param mail
 *            DOCUMENT ME!
 * @param message
 *            DOCUMENT ME!
 * @param bouncer
 *            DOCUMENT ME!
 * 
 * @throws MessagingException
 *             DOCUMENT ME!
 */
static public void bounce(MailQue que, Mail mail, String message, MailAddress bouncer)
        throws MessagingException {
    if (bouncer != null) {
        if (log.isDebugEnabled()) {
            log.debug("bouncing message to postmaster");
        }
        MimeMessage orig = mail.getMessage();
        //Create the reply message
        MimeMessage reply = (MimeMessage) orig.reply(false);
        //If there is a Return-Path header,
        if (orig.getHeader(RFC2822Headers.RETURN_PATH) != null) {
            //Return the message to that address, not to the Reply-To
            // address
            reply.setRecipient(MimeMessage.RecipientType.TO,
                    new InternetAddress(orig.getHeader(RFC2822Headers.RETURN_PATH)[0]));
        }
        //Create the list of recipients in our MailAddress format
        Collection recipients = new HashSet();
        Address[] addresses = reply.getAllRecipients();
        for (int i = 0; i < addresses.length; i++) {
            recipients.add(new MailAddress((InternetAddress) addresses[i]));
        }
        //Change the sender...
        reply.setFrom(bouncer.toInternetAddress());
        try {
            //Create the message body
            MimeMultipart multipart = new MimeMultipart();
            //Add message as the first mime body part
            MimeBodyPart part = new MimeBodyPart();
            part.setContent(message, "text/plain");
            part.setHeader(RFC2822Headers.CONTENT_TYPE, "text/plain");
            multipart.addBodyPart(part);
            //Add the original message as the second mime body part
            part = new MimeBodyPart();
            part.setContent(orig.getContent(), orig.getContentType());
            part.setHeader(RFC2822Headers.CONTENT_TYPE, orig.getContentType());
            multipart.addBodyPart(part);
            reply.setHeader(RFC2822Headers.DATE, rfc822DateFormat.format(new Date()));
            reply.setContent(multipart);
            reply.setHeader(RFC2822Headers.CONTENT_TYPE, multipart.getContentType());
        } catch (IOException ioe) {
            throw new MessagingException("Unable to create multipart body", ioe);
        }
        //Send it off...
        //sendMail( bouncer, recipients, reply );
        que.queMail(reply);
    }
}

From source file:org.roda.core.util.EmailUtility.java

/**
 * @param from/*ww  w  .  j a va 2  s.c om*/
 * @param recipients
 * @param subject
 * @param message
 * @throws MessagingException
 */
public void sendMail(String from, String recipients[], String subject, String message)
        throws MessagingException {
    boolean debug = false;

    // Set the host smtp address
    Properties props = new Properties();
    props.put("mail.smtp.host", this.smtpHost);

    // create some properties and get the default Session
    Session session = Session.getDefaultInstance(props, null);
    session.setDebug(debug);

    // create a message
    Message msg = new MimeMessage(session);

    // set the from and to address
    InternetAddress addressFrom = new InternetAddress(from);
    msg.setFrom(addressFrom);

    InternetAddress[] addressTo = new InternetAddress[recipients.length];
    for (int i = 0; i < recipients.length; i++) {
        addressTo[i] = new InternetAddress(recipients[i]);
    }
    msg.setRecipients(Message.RecipientType.TO, addressTo);

    // Optional : You can also set your custom headers in the Email if you
    // want
    // msg.addHeader("MyHeaderName", "myHeaderValue");

    String htmlMessage = String.format(
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<html><body><pre>%s</pre></body></html>",
            StringEscapeUtils.escapeHtml4(message));

    MimeMultipart mimeMultipart = new MimeMultipart();
    MimeBodyPart mimeBodyPart = new MimeBodyPart();
    mimeBodyPart.setContent(htmlMessage, "text/html;charset=UTF-8");
    mimeMultipart.addBodyPart(mimeBodyPart);

    // Setting the Subject and Content Type
    msg.setSubject(subject);
    msg.setContent(mimeMultipart);
    // msg.setContent(message, "text/plain;charset=UTF-8");
    Transport.send(msg);
}

From source file:com.eviware.soapui.impl.wsdl.submit.filters.WsdlPackagingRequestFilter.java

protected String initWsdlRequest(WsdlRequest wsdlRequest, ExtendedPostMethod postMethod, String requestContent)
        throws Exception {
    MimeMultipart mp = null;/*  w  ww.j a v a  2  s  .c  o m*/

    StringToStringMap contentIds = new StringToStringMap();
    boolean isXOP = wsdlRequest.isMtomEnabled() && wsdlRequest.isForceMtom();

    // preprocess only if neccessary
    if (wsdlRequest.isMtomEnabled() || wsdlRequest.isInlineFilesEnabled()
            || wsdlRequest.getAttachmentCount() > 0) {
        try {
            mp = new MimeMultipart();

            MessageXmlObject requestXmlObject = new MessageXmlObject(wsdlRequest.getOperation(), requestContent,
                    true);
            MessageXmlPart[] requestParts = requestXmlObject.getMessageParts();
            for (MessageXmlPart requestPart : requestParts) {
                if (AttachmentUtils.prepareMessagePart(wsdlRequest, mp, requestPart, contentIds))
                    isXOP = true;
            }
            requestContent = requestXmlObject.getMessageContent();
        } catch (Throwable e) {
            SoapUI.log.warn("Failed to process inline/MTOM attachments; " + e);
        }
    }

    // non-multipart request?
    if (!isXOP && (mp == null || mp.getCount() == 0) && hasContentAttachmentsOnly(wsdlRequest)) {
        String encoding = System.getProperty("soapui.request.encoding",
                StringUtils.unquote(wsdlRequest.getEncoding()));
        byte[] content = StringUtils.isNullOrEmpty(encoding) ? requestContent.getBytes()
                : requestContent.getBytes(encoding);
        postMethod.setRequestEntity(new ByteArrayRequestEntity(content));
    } else {
        // make sure..
        if (mp == null)
            mp = new MimeMultipart();

        // init root part
        initRootPart(wsdlRequest, requestContent, mp, isXOP);

        // init mimeparts
        AttachmentUtils.addMimeParts(wsdlRequest, Arrays.asList(wsdlRequest.getAttachments()), mp, contentIds);

        // create request message
        MimeMessage message = new MimeMessage(AttachmentUtils.JAVAMAIL_SESSION);
        message.setContent(mp);
        message.saveChanges();
        WsdlRequestMimeMessageRequestEntity mimeMessageRequestEntity = new WsdlRequestMimeMessageRequestEntity(
                message, isXOP, wsdlRequest);
        postMethod.setRequestEntity(mimeMessageRequestEntity);
        postMethod.setRequestHeader("Content-Type", mimeMessageRequestEntity.getContentType());
        postMethod.setRequestHeader("MIME-Version", "1.0");
    }

    return requestContent;
}

From source file:bo.com.offercruzmail.imp.FormadorMensajes.java

public static Multipart enviarErrorInesperado() throws MessagingException {
    Multipart cuerpo = new MimeMultipart();
    cuerpo.addBodyPart(FormadorMensajes/*  w ww .  j  a va  2s .com*/
            .getBodyPartEnvuelto("Ha ocurrido un error inesperado, por favor intentelo nuevamente"));
    return cuerpo;
}

From source file:com.assetmanager.service.mail.MailService.java

/**
 * Sends the activation e-mail to the given user.
 *
 * @param user the user/*from   w w w.jav  a2  s.  co m*/
 * @param locale the locale
 * @throws MessagingException messaging exception
 */
public final void sendActivationEmail(final UserAccount user, final String locale) throws MessagingException {
    final Properties props = new Properties();
    final Session session = Session.getDefaultInstance(props, null);
    final Message message = new MimeMessage(session);
    final Multipart multipart = new MimeMultipart();
    final MimeBodyPart htmlPart = new MimeBodyPart();
    final MimeBodyPart textPart = new MimeBodyPart();

    message.setFrom(new InternetAddress(getFromAddress()));
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail()));

    message.setSubject(messageSource.getMessage("mail.subject", null, new Locale(locale)));

    textPart.setContent(messageSource.getMessage("mail.body.txt",
            new Object[] { getHostname(), user.getActivationKey() }, new Locale(locale)), "text/plain");

    htmlPart.setContent(messageSource.getMessage("mail.body.html",
            new Object[] { getHostname(), user.getActivationKey() }, new Locale(locale)), "text/html");

    multipart.addBodyPart(textPart);
    multipart.addBodyPart(htmlPart);
    message.setContent(multipart);

    Transport.send(message);
}

From source file:org.unitime.commons.Email.java

public Email() {
    Properties p = ApplicationProperties.getProperties();
    if (p.getProperty("mail.smtp.host") == null && p.getProperty("tmtbl.smtp.host") != null)
        p.setProperty("mail.smtp.host", p.getProperty("tmtbl.smtp.host"));

    final String user = ApplicationProperties.getProperty("mail.smtp.user", ApplicationProperties
            .getProperty("unitime.email.user", ApplicationProperties.getProperty("tmtbl.mail.user")));
    final String password = ApplicationProperties.getProperty("mail.smtp.password", ApplicationProperties
            .getProperty("unitime.email.password", ApplicationProperties.getProperty("tmtbl.mail.pwd")));

    Authenticator a = null;/*from  www .  java  2s. c om*/
    if (user != null && password != null) {
        p.setProperty("mail.smtp.user", user);
        p.setProperty("mail.smtp.auth", "true");
        a = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(user, password);
            }
        };
    }

    iMailSession = javax.mail.Session.getDefaultInstance(p, a);
    iMail = new MimeMessage(iMailSession);
    iBody = new MimeMultipart();
}