Example usage for javax.mail Message setContent

List of usage examples for javax.mail Message setContent

Introduction

In this page you can find the example usage for javax.mail Message setContent.

Prototype

public void setContent(Object obj, String type) throws MessagingException;

Source Link

Document

A convenience method for setting this part's content.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {

    Properties props = new Properties();
    props.put("mail.host", "mail.cloud9.net");

    Session mailConnection = Session.getInstance(props, null);
    Message msg = new MimeMessage(mailConnection);

    Address a = new InternetAddress("a@a.com", "A a");
    Address b = new InternetAddress("fake@java2s.com");

    msg.setContent("Mail contect", "text/plain");
    msg.setFrom(a);/*w ww.  ja va  2s .  co  m*/
    msg.setRecipient(Message.RecipientType.TO, b);
    msg.setSubject("subject");

    Transport.send(msg);
}

From source file:org.bml.util.mail.MailUtils.java

/**
 * Simple mail utility/*from w w  w .  j  ava 2  s.  c  om*/
 * @param sendToAdresses email addresses to send the mail to
 * @param emailSubjectLine the subject of the email.
 * @param emailBody The body of the mail
 * @param smtpHost the smtp host
 * @param sender the mail address that is the sender
 * @param smtpPassword the password for the sender
 * @param smtpPort the port to contact the smtp server on
 * @return boolean true on success and false on error
 */
public static boolean sendMail(final String[] sendToAdresses, final String emailSubjectLine,
        final String emailBody, final String smtpHost, String sender, String smtpPassword, final int smtpPort) {
    if ((sendToAdresses == null) || (sendToAdresses.length == 0)) {
        return false;
    }

    if ((emailSubjectLine == null) || (emailBody == null)) {
        return false;
    }

    try {
        Address[] addresses = new Address[sendToAdresses.length];
        for (int i = 0; i < sendToAdresses.length; i++) {
            addresses[i] = new InternetAddress(sendToAdresses[i]);
        }

        Properties props = System.getProperties();
        props.setProperty("mail.smtp.host", smtpHost);
        props.setProperty("mail.smtp.localhost", smtpHost);
        props.setProperty("mail.smtp.auth", "true");
        props.setProperty("mail.smtp.port", String.valueOf(smtpPort));
        props.put("mail.smtp.starttls.enable", "true");

        Session session = Session.getDefaultInstance(props, null);

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(sender));
        message.setRecipients(Message.RecipientType.TO, addresses);
        message.setSubject(emailSubjectLine);
        message.setContent(emailBody, "text/plain");
        message.saveChanges();

        Transport transport = session.getTransport("smtp");
        transport.connect(smtpHost, sender, smtpPassword);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    } catch (Throwable t) {
        if (LOG.isErrorEnabled()) {
            LOG.error("Error occured while sending mail.", t);
        }
        return false;
    }
    return true;
}

From source file:rapture.mail.Mailer.java

public static void email(String[] recipients, String subject, String message) throws MessagingException {
    final SMTPConfig config = getSMTPConfig();
    Session session = getSession(config);
    Message msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress(config.getFrom()));
    InternetAddress[] address = new InternetAddress[recipients.length];
    for (int i = 0; i < recipients.length; i++)
        address[i] = new InternetAddress(recipients[i]);
    msg.setRecipients(Message.RecipientType.TO, address);
    msg.setSubject(subject);//from   ww  w  . j a  v a  2s.c o  m
    msg.setContent(message, MediaType.PLAIN_TEXT_UTF_8.toString());
    msg.setSentDate(new Date());
    Transport.send(msg);
}

From source file:com.mnt.base.mail.MailHelper.java

public static void sendMail(String mailType, String mailTos, Map<String, Object> infoMap) {

    String[] mailtemplate = loadMailTemplate(mailType);

    if (mailtemplate != null && mailtemplate.length == 2) {
        String from = BaseConfiguration.getProperty("mail_server_email");
        String subject = buildMailContent(mailtemplate[0], infoMap, false);
        String mailContent = buildMailContent(mailtemplate[1], infoMap, true);

        Message msg = new MimeMessage(smtpSession);
        try {/*from  w w w .j  a  v  a  2s . c  o m*/
            msg.setFrom(new InternetAddress(from));
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mailTos, false));
            msg.setSubject(MimeUtility.encodeText(subject, "UTF-8", "B"));
            msg.setContent(mailContent, "text/html;charset=UTF-8");
            msg.setSentDate(new Date());

            Transport.send(msg);
        } catch (Exception e) {
            log.error("fail to send the mail: " + msg, e);
        }
    }
}

From source file:rapture.mail.Mailer.java

public static void email(CallingContext context, String templateName,
        Map<String, ? extends Object> templateValues) {
    final SMTPConfig config = getSMTPConfig();
    Session session = getSession(config);
    EmailTemplate template = getEmailTemplate(context, templateName);
    try {/*from   ww  w  . j  a  v  a  2 s.c  om*/
        // Instantiate a new MimeMessage and fill it with the required information.
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(config.getFrom()));
        String to = renderTemplate(template.getEmailTo(), templateValues);
        if (StringUtils.isBlank(to)) {
            throw RaptureExceptionFactory.create("No emailTo field");
        }
        InternetAddress[] address = { new InternetAddress(to) };
        msg.setRecipients(Message.RecipientType.TO, address);
        msg.setSubject(renderTemplate(template.getSubject(), templateValues));
        msg.setSentDate(new Date());
        msg.setContent(renderTemplate(template.getMsgBody(), templateValues), "text/html; charset=utf-8");
        // Hand the message to the default transport service for delivery.
        Transport.send(msg);
    } catch (MessagingException e) {
        log.error("Failed to send email", e);
    }
}

From source file:com.liferay.util.mail.MailEngine.java

public static void send(InternetAddress from, InternetAddress[] to, InternetAddress[] cc, InternetAddress[] bcc,
        String subject, String body, boolean htmlFormat) throws MailEngineException {

    long start = System.currentTimeMillis();

    try {//from  w  w  w . j  av  a2 s. com
        Session session = getSession();

        Message msg = new MimeMessage(session);

        msg.setFrom(from);
        msg.setRecipients(Message.RecipientType.TO, to);

        if (cc != null) {
            msg.setRecipients(Message.RecipientType.CC, cc);
        }

        if (bcc != null) {
            msg.setRecipients(Message.RecipientType.BCC, bcc);
        }

        msg.setSubject(subject);

        /*BodyPart bodyPart = new MimeBodyPart();
                
        if (htmlFormat) {
           bodyPart.setContent(body, "text/html");
        }
        else {
           bodyPart.setText(body);
        }
                
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(bodyPart);
                
        msg.setContent(multipart);*/

        if (htmlFormat) {
            msg.setContent(body, _TEXT_HTML);
        } else {
            msg.setContent(body, _TEXT_PLAIN);
        }

        _sendMessage(session, msg);
    } catch (SendFailedException sfe) {
        _log.error("From: " + from);
        _log.error("To: " + to);
        _log.error("Subject: " + subject);
        _log.error("Body: " + body);

        Logger.error(MailEngine.class, sfe.getMessage(), sfe);
    } catch (Exception e) {
        throw new MailEngineException(e);
    }

    long end = System.currentTimeMillis();

    _log.debug("Sending mail takes " + (end - start) + " seconds");
}

From source file:yoyo.framework.enterprise.infra.messaging.MailServiceImpl.java

@Override
public void send(final String from, final String to, final String subject, final Object objectBody,
        final String contentType) throws EnterpriseException {
    try {/*w  ww  .j  ava2s .  co  m*/
        final Message message = createMessage(from, to, subject);
        message.setContent(objectBody, contentType);
        send(message);
    } catch (final MessagingException e) {
        throw new EnterpriseException(e.getLocalizedMessage());
    }
}

From source file:yoyo.framework.enterprise.infra.messaging.MailServiceImpl.java

@Override
public void send(final String from, final String to, final String subject, final String textBody)
        throws EnterpriseException {
    try {//from   ww w  .java2s  .  c  o  m
        final Message message = createMessage(from, to, subject);
        message.setContent(textBody, "text/plain");
        send(message);
    } catch (final MessagingException e) {
        throw new EnterpriseException(e.getLocalizedMessage());
    }
}

From source file:com.github.aynu.mosir.core.enterprise.mail.MailServiceImpl.java

/** {@inheritDoc} */
@Override/*  ww w .  ja  va 2s  . c  om*/
public final void send(final InternetAddress originator, final Map<RecipientType, InternetAddress> recipients,
        final String subject, final String body) throws EnterpriseException {
    try {
        final Message message = createMessage(originator, recipients, subject);
        message.setContent(body, "text/plain;charset=UTF-8");
        send(message);
    } catch (final MessagingException e) {
        throw new EnterpriseException(e);
    }
}

From source file:com.github.aynu.mosir.core.enterprise.mail.MailServiceImpl.java

/** {@inheritDoc} */
@Override/*from  www  .ja  va  2s.  c  om*/
public final void send(final InternetAddress originator, final Map<RecipientType, InternetAddress> recipients,
        final String subject, final Object body, final String type) throws EnterpriseException {
    try {
        final Message message = createMessage(originator, recipients, subject);
        message.setContent(body, type);
        send(message);
    } catch (final MessagingException e) {
        throw new EnterpriseException(e);
    }
}