Example usage for javax.mail.internet MimeBodyPart setText

List of usage examples for javax.mail.internet MimeBodyPart setText

Introduction

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

Prototype

@Override
public void setText(String text, String charset) throws MessagingException 

Source Link

Document

Convenience method that sets the given String as this part's content, with a MIME type of "text/plain" and the specified charset.

Usage

From source file:org.nuxeo.ecm.automation.client.jaxrs.impl.MultipartRequestEntity.java

public void setRequest(String content) throws MessagingException {
    MimeBodyPart part = new MimeBodyPart();
    part.setText(content, "UTF-8");
    part.setContentID("request");
    part.setHeader("Content-Type", Constants.CTYPE_REQUEST);
    part.setHeader("Content-Transfer-Encoding", "8bit");
    part.setHeader("Content-Length", Integer.toString(content.length()));
    mp.addBodyPart(part);//from ww w .jav  a2 s . c  om
}

From source file:nl.surfnet.coin.teams.util.ControllerUtilImpl.java

@Override
public MimeMultipart getMimeMultipartMessageBody(String plainText, String html) throws MessagingException {
    MimeMultipart multiPart = new MimeMultipart("alternative");
    MimeBodyPart textPart = new MimeBodyPart();
    textPart.setText(plainText, "utf-8");

    MimeBodyPart htmlPart = new MimeBodyPart();
    htmlPart.setContent(html, "text/html; charset=utf-8");

    multiPart.addBodyPart(textPart); // least important
    multiPart.addBodyPart(htmlPart); // most important
    return multiPart;
}

From source file:com.emc.plants.service.impl.MailerBean.java

/** 
 * Create a mail message and send it./*from w  ww  .  j a  va  2  s .  c  o  m*/
 *
 * @param customerInfo  Customer information.
 * @param orderKey
 * @throws MailerAppException
 */
public void createAndSendMail(CustomerInfo customerInfo, long orderKey) throws MailerAppException {
    try {
        EMailMessage eMessage = new EMailMessage(createSubjectLine(orderKey), createMessage(orderKey),
                customerInfo.getCustomerID());
        Util.debug("Sending message" + "\nTo: " + eMessage.getEmailReceiver() + "\nSubject: "
                + eMessage.getSubject() + "\nContents: " + eMessage.getHtmlContents());

        //Session mailSession = (Session) Util.getInitialContext().lookup(MAIL_SESSION);

        MimeMessage msg = new MimeMessage(mailSession);
        msg.setFrom();

        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(eMessage.getEmailReceiver(), false));

        msg.setSubject(eMessage.getSubject());
        MimeBodyPart mbp = new MimeBodyPart();
        mbp.setText(eMessage.getHtmlContents(), "us-ascii");
        msg.setHeader("X-Mailer", "JavaMailer");
        Multipart mp = new MimeMultipart();
        mp.addBodyPart(mbp);
        msg.setContent(mp);
        msg.setSentDate(new Date());

        Transport.send(msg);

        Util.debug("\nMail sent successfully.");
    } catch (Exception e) {
        Util.debug("Error sending mail. Have mail resources been configured correctly?");
        Util.debug("createAndSendMail exception : " + e);
        e.printStackTrace();
        throw new MailerAppException("Failure while sending mail");
    }
}

From source file:com.formkiq.core.service.notification.ExternalMailSender.java

/**
 * Send Reset Email./*  w  w w . j a  v a 2s  . c o  m*/
 * @param to {@link String}
 * @param email {@link String}
 * @param subject {@link String}
 * @param text {@link String}
 * @param html {@link String}
 * @throws MessagingException MessagingException
 */
private void sendResetEmail(final String to, final String email, final String subject, final String text,
        final String html) throws MessagingException {

    String hostname = this.systemProperties.getSystemHostname();
    String resetToken = this.userservice.generateResetToken(to);

    StringSubstitutor s = new StringSubstitutor(
            ImmutableMap.of("hostname", hostname, "to", to, "email", email, "resettoken", resetToken));

    MimeBodyPart textPart = new MimeBodyPart();
    textPart.setText(s.replace(text), "UTF-8");

    s.replace(html);
    MimeBodyPart htmlPart = new MimeBodyPart();
    htmlPart.setContent(s.replace(html), "text/html;charset=UTF-8");

    final Multipart mp = new MimeMultipart("alternative");
    mp.addBodyPart(textPart);
    mp.addBodyPart(htmlPart);

    MimeMessage msg = this.mail.createMimeMessage();
    msg.setContent(mp);

    msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

    msg.setSubject(subject);

    this.mail.send(msg);
}

From source file:com.enjoyxstudy.selenium.autoexec.mail.MailSender.java

/**
 * @param runner//from   www. j  a v  a  2 s.  c om
 * @param resultDir
 * @throws MessagingException
 * @throws IOException
 */
public void send(MultiHTMLSuiteRunner runner, File resultDir) throws MessagingException, IOException {

    MimeMessage mimeMessage = new MimeMessage(session);

    mimeMessage.setHeader("Content-Transfer-Encoding", "7bit");

    // To
    mimeMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(config.getTo()));

    // From
    mimeMessage.setFrom(new InternetAddress(config.getFrom()));

    HashMap<String, Object> context = new HashMap<String, Object>();
    context.put("result", runner.getResult() ? "passed" : "failed");
    context.put("passedCount", new Integer(runner.getPassedCount()));
    context.put("failedCount", new Integer(runner.getFailedCount()));
    context.put("totalCount", new Integer(runner.getHtmlSuiteList().size()));
    context.put("startTime", new Date(runner.getStartTime()));
    context.put("endTime", new Date(runner.getEndTime()));
    context.put("htmlSuites", runner.getHtmlSuiteList());

    // subject
    mimeMessage.setSubject(TemplateUtils.merge(config.getSubject(), context), config.getCharset());

    // multipart message
    MimeMultipart content = new MimeMultipart();

    MimeBodyPart body = new MimeBodyPart();
    body.setText(TemplateUtils.merge(config.getBody(), context), config.getCharset());
    content.addBodyPart(body);

    File resultArchive = createResultArchive(resultDir);

    MimeBodyPart attachmentFile = new MimeBodyPart();
    attachmentFile.setDataHandler(new DataHandler(new FileDataSource(resultArchive)));
    attachmentFile.setFileName(RESULT_ARCHIVE_FILE);
    content.addBodyPart(attachmentFile);

    mimeMessage.setContent(content);

    // send mail
    _send(mimeMessage);
}

From source file:com.aurel.track.util.emailHandling.MailBuilder.java

/**
 * Prepares a plain MimeMessage: the MimeMessage.RecipientType.TO is not yet set
 * @return//from  w w w . j a  va 2 s. c  o m
 * @throws Exception
 */
private MimeMessage preparePlainMimeMessage(InternetAddress internetAddressFrom, String subject,
        String plainBody, List<LabelValueBean> attachments) throws Exception {
    MimeMessage msg = new MimeMessage(session);
    msg.setFrom(internetAddressFrom);
    msg.setHeader(XMAILER, xmailer);
    msg.setSubject(subject.trim(), mailEncoding);
    msg.setSentDate(new Date());
    if (attachments == null || attachments.isEmpty()) {
        msg.setText(plainBody, mailEncoding);
    } else {
        MimeMultipart mimeMultipart = new MimeMultipart();

        MimeBodyPart textBodyPart = new MimeBodyPart();
        textBodyPart.setText(plainBody, mailEncoding);
        mimeMultipart.addBodyPart(textBodyPart);

        if (attachments != null) {
            includeAttachments(mimeMultipart, attachments);
        }

        msg.setContent(mimeMultipart);
    }
    return msg;
}

From source file:it.cnr.icar.eric.common.RepositoryItemImpl.java

/**
 * Packages the RepositoryItem as a MimeMultiPart and returns it
 *
 * @deprecated sigElement/multipart attachment is not used anymore.
 *///from w  w  w.ja  v  a 2s .com
public MimeMultipart getMimeMultipart() throws RegistryException {
    MimeMultipart mp = null;
    try {
        //Create a multipart with two bodyparts
        //First bodypart is an XMLDSIG and second is the attached file
        mp = new MimeMultipart();

        //The signature part
        ByteArrayOutputStream os = new ByteArrayOutputStream();

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer trans = tf.newTransformer();
        trans.transform(new DOMSource(sigElement), new StreamResult(os));

        MimeBodyPart bp1 = new MimeBodyPart();
        bp1.addHeader("Content-ID", "payload1");
        bp1.setText(os.toString(), "utf-8");
        mp.addBodyPart(bp1);

        //The payload part
        MimeBodyPart bp2 = new MimeBodyPart();
        bp2.setDataHandler(handler);
        bp2.addHeader("Content-Type", handler.getContentType());
        bp2.addHeader("Content-ID", "payload2");
        mp.addBodyPart(bp2);
    } catch (MessagingException e) {
        throw new RegistryException(e);
    } catch (TransformerException e) {
        throw new RegistryException(e);
    }
    return mp;
}

From source file:egovframework.oe1.cms.cmm.notify.email.service.impl.EgovOe1SSLMailServiceImpl.java

protected void send(String subject, String content, String contentType) throws Exception {
    Properties props = new Properties();

    props.put("mail.transport.protocol", "smtps");
    props.put("mail.smtps.host", getHost());
    props.put("mail.smtps.auth", "true");

    Session mailSession = Session.getDefaultInstance(props);
    mailSession.setDebug(false);/*  w ww  .  j av a 2  s.  co  m*/
    Transport transport = mailSession.getTransport();

    MimeMessage message = new MimeMessage(mailSession);
    message.setFrom(new InternetAddress("www.egovframe.org", "webmaster", "euc-kr"));
    message.setSubject(subject);

    MimeBodyPart mbp1 = new MimeBodyPart();
    mbp1.setText(content, "utf-8");

    Multipart mp = new MimeMultipart();
    mp.addBodyPart(mbp1);

    List<String> fileNames = getAtchFileIds();

    for (Iterator<String> it = fileNames.iterator(); it.hasNext();) {
        MimeBodyPart mbp2 = new MimeBodyPart();

        // attach the file to the message
        FileDataSource fds = new FileDataSource(it.next());
        mbp2.setDataHandler(new DataHandler(fds));

        mbp2.setFileName(MimeUtility.encodeText(fds.getName(), "euc-kr", "B")); // Q : ascii, B : 

        mp.addBodyPart(mbp2);
    }

    // add the Multipart to the message
    message.setContent(mp);

    for (Iterator<String> it = getReceivers().iterator(); it.hasNext();)
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(it.next()));

    transport.connect(getHost(), getPort(), getUsername(), getPassword());
    transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
    transport.close();
}

From source file:de.betterform.connector.serializer.MultipartRelatedSerializer.java

public void serialize(Submission submission, Node node, SerializerRequestWrapper wrapper,
        String defaultEncoding) throws Exception {
    Map cache = new HashMap();
    MimeMultipart multipart = new MimeMultipart();

    MimeBodyPart part = new MimeBodyPart();
    multipart.addBodyPart(part);/*w w w . j  a v  a  2  s.  com*/

    String encoding = defaultEncoding;
    if (submission.getEncoding() != null) {
        encoding = submission.getEncoding();
    }

    if (node.getNodeType() == Node.ELEMENT_NODE || node.getNodeType() == Node.DOCUMENT_NODE) {
        part.setText(new String(serializeXML(multipart, cache, submission, node, encoding), encoding),
                encoding);
    } else {
        part.setText(node.getTextContent());
    }
    part.setContentID("<instance.xml@start>");
    part.addHeader("Content-Type", "application/xml");
    part.addHeader("Content-Transfer-Encoding", "base64");
    part.setDisposition("attachment");
    part.setFileName("instance.xml");

    multipart.setSubType("related; type=\"" + submission.getMediatype() + "\"; start=\"instance.xml@start\"");
    // FIXME: Is this a global http header or a local mime header?
    wrapper.getBodyStream()
            .write(("Content-Type: " + multipart.getContentType() + "\n\nThis is a MIME message.\n")
                    .getBytes(encoding));
    multipart.writeTo(wrapper.getBodyStream());
}

From source file:com.szmslab.quickjavamail.send.MailSender.java

/**
 * MimeBodyPart????text/plain/*from  w  w w .j a  v  a 2  s  . co  m*/
 *
 * @return MimeBodyParttext/plain?
 * @throws MessagingException
 */
private MimeBodyPart createTextPart() throws MessagingException {
    MimeBodyPart textPart = new MimeBodyPart();
    textPart.setText(text, charset);
    setHeaderToPart(textPart);
    return textPart;
}