Example usage for javax.mail.internet MimeMessage setContent

List of usage examples for javax.mail.internet MimeMessage setContent

Introduction

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

Prototype

@Override
public void setContent(Object o, String type) throws MessagingException 

Source Link

Document

A convenience method for setting this Message's content.

Usage

From source file:org.mule.transport.email.AbstractGreenMailSupport.java

public MimeMessage getValidMessage(String to) throws Exception {
    MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()));
    message.setContent(MESSAGE, "text/plain");
    message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
    return message;
}

From source file:com.infullmobile.jenkins.plugin.restrictedregister.mail.Mail.java

private MimeMessage createMessage() throws MailException, UnsupportedEncodingException, MessagingException {
    final String content = getContent();
    final String replyToAddress = replyTo;

    final MimeMessageBuilder messageBuilder = new MimeMessageBuilder();
    messageBuilder.setSubject(this.subject);
    messageBuilder.setCharset(META_CHARSET);
    messageBuilder.addRecipients(this.recipients);
    if (EmailValidator.getInstance().isValid(replyToAddress)) {
        Utils.logInfo("Reply to address " + replyToAddress);
        messageBuilder.setReplyTo(replyToAddress);
    }// w w  w  . j av  a 2  s .co m
    final MimeMessage message = messageBuilder.buildMimeMessage();
    message.setContent(content, META_CONTENT_TYPE);
    return message;
}

From source file:com.freemarker.mail.GMail.java

public boolean SendMail(String To, String MessageContent, HttpServletRequest req) throws Exception {
    setMailsender();/*w  w w.  j ava 2s. c  o m*/
    String FinalMessage = new FreeMarkerMailTemplateCreater().createAndReturnTemplateData(MessageContent,
            getTemplateLocation(req));
    String From = "analytixdstest@gmail.com";
    String Subject = "Freemarker Email Template";
    MimeMessage mimeMessage = mailsender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, false, "utf-8");

    mimeMessage.setContent(FinalMessage, "text/html");
    helper.setTo(To);
    helper.setSubject("Subject");
    helper.setFrom(From);
    mailsender.send(mimeMessage);

    return true;

}

From source file:org.trustedanalytics.user.invite.EmailService.java

@Override
public void sendMimeMessage(String email, String subject, String htmlContent) {
    MimeMessage message = mailSender.createMimeMessage();
    try {//from  w w  w.j a v  a  2  s. com
        message.addFrom(senderAddresses);
        message.addRecipients(Message.RecipientType.TO, email);
        message.setSubject(subject);
        message.setContent(htmlContent, "text/html");
    } catch (Exception e) {
        LOGGER.error(e);
    }
    mailSender.send(message);
}

From source file:easyproject.service.Mail.java

public void sendMail() {
    Properties props = new Properties();

    props.put("mail.debug", "true");
    props.put("mail.smtp.auth", true);
    props.put("mail.smtp.starttls.enable", true);
    props.put("mail.smtp.host", servidorSMTP);
    props.put("mail.smtp.port", puerto);

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

    try {//from w w  w  .ja va  2 s .c o  m
        MimeMessage message = new MimeMessage(session);

        message.addRecipient(Message.RecipientType.TO, new InternetAddress(destino));
        message.setSubject(asunto);
        message.setSentDate(new Date());
        message.setContent(mensaje, "text/html; charset=utf-8");

        Transport tr = session.getTransport("smtp");
        tr.connect(servidorSMTP, usuario, password);
        message.saveChanges();
        tr.sendMessage(message, message.getAllRecipients());
        tr.close();

    } catch (MessagingException e) {
        e.printStackTrace();
    }
}

From source file:org.apache.camel.component.google.mail.GmailUsersThreadsIntegrationTest.java

private Message createThreadedTestEmail(String previousThreadId) throws MessagingException, IOException {
    com.google.api.services.gmail.model.Profile profile = requestBody(
            "google-mail://users/getProfile?inBody=userId", CURRENT_USERID);
    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);
    MimeMessage mm = new MimeMessage(session);
    mm.addRecipients(javax.mail.Message.RecipientType.TO, profile.getEmailAddress());
    mm.setSubject("Hello from camel-google-mail");
    mm.setContent("Camel rocks!", "text/plain");
    Message createMessageWithEmail = createMessageWithEmail(mm);
    if (previousThreadId != null) {
        createMessageWithEmail.setThreadId(previousThreadId);
    }/*w  ww. j av a  2s . c  o m*/

    Map<String, Object> headers = new HashMap<String, Object>();
    // parameter type is String
    headers.put("CamelGoogleMail.userId", CURRENT_USERID);
    // parameter type is com.google.api.services.gmail.model.Message
    headers.put("CamelGoogleMail.content", createMessageWithEmail);

    return requestBodyAndHeaders("google-mail://messages/send", null, headers);
}

From source file:csns.util.EmailUtils.java

public boolean sendHtmlMail(Email email) {
    MimeMessage message = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message);

    try {//from w  w  w  . j  a va 2 s . c  om
        message.setContent(getHtml(email), "text/html");

        helper.setSubject(email.getSubject());
        helper.setFrom(email.getAuthor().getPrimaryEmail());
        helper.setCc(email.getAuthor().getPrimaryEmail());
        String addresses[] = getAddresses(email.getRecipients(), email.isUseSecondaryEmail())
                .toArray(new String[0]);
        if (addresses.length > 1) {
            helper.setTo(appEmail);
            helper.setBcc(addresses);
        } else
            helper.setTo(addresses);

        mailSender.send(message);

        logger.info(email.getAuthor().getUsername() + " sent email to "
                + StringUtils.arrayToCommaDelimitedString(addresses));

        return true;
    } catch (MessagingException e) {
        logger.warn("Fail to send MIME message", e);
    }

    return false;
}

From source file:frameworkcontentspeed.Utils.SendEmailAtachament.java

public static void SendEmailAnda(String from, String to1, String subject, String filename)
        throws FileNotFoundException, IOException {
    //Get properties object    
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");
    //get Session   
    Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(from, "anda.cristea");
        }/*from   ww w  .  j  av  a 2  s .  com*/
    });
    //compose message    
    try {
        MimeMessage message = new MimeMessage(session);
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to1));

        message.setSubject(subject);
        // message.setText(msg);

        BodyPart messageBodyPart = new MimeBodyPart();

        messageBodyPart.setText("Raport teste automate");

        Multipart multipart = new MimeMultipart();

        multipart.addBodyPart(messageBodyPart);

        messageBodyPart = new MimeBodyPart();

        DataSource source = new FileDataSource(filename);

        messageBodyPart.setDataHandler(new DataHandler(source));

        messageBodyPart.setFileName(filename);

        multipart.addBodyPart(messageBodyPart);

        //message.setContent(multipart);

        StringWriter writer = new StringWriter();
        IOUtils.copy(new FileInputStream(new File(filename)), writer);

        message.setContent(writer.toString(), "text/html");

        //send message  
        Transport.send(message);

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }

}

From source file:easyproject.utils.SendMail.java

@Override
public void run() {

    Properties props = new Properties();

    props.put("mail.debug", "true");
    props.put("mail.smtp.auth", true);
    props.put("mail.smtp.starttls.enable", true);
    props.put("mail.smtp.host", servidorSMTP);
    props.put("mail.smtp.port", puerto);

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

    try {//from  w  w w.  j av  a2  s . c o m
        MimeMessage message1 = new MimeMessage(session);

        message1.addRecipient(Message.RecipientType.TO, new InternetAddress(destino));
        message1.setSubject(asunto);
        message1.setSentDate(new Date());
        message1.setContent(mensaje, "text/html; charset=utf-8");

        Transport tr = session.getTransport("smtp");
        tr.connect(servidorSMTP, usuario, password);
        message1.saveChanges();
        tr.sendMessage(message1, message1.getAllRecipients());
        tr.close();

    } catch (MessagingException e) {
        e.printStackTrace();
    }

}

From source file:org.rhq.enterprise.server.core.EmailManagerBean.java

/**
 * Send email to the addressses passed in toAddresses with the passed subject and body. Invalid emails will
 * be reported back. This can only catch sender errors up to the first smtp gateway.
 * @param  toAddresses list of email addresses to send to
 * @param  messageSubject subject of the email sent
 * @param  messageBody body of the email to be sent
 *
 * @return list of email receivers for which initial delivery failed.
 */// w w w  .j  a v a2s.  c  om
public Collection<String> sendEmail(Collection<String> toAddresses, String messageSubject, String messageBody) {

    MimeMessage mimeMessage = new MimeMessage(mailSession);
    try {
        mimeMessage.setSubject(messageSubject);
        mimeMessage.setContent(messageBody, "text/plain");
    } catch (MessagingException e) {
        e.printStackTrace(); // TODO: Customise this generated block
        return toAddresses;
    }

    Exception error = null;
    Collection<String> badAdresses = new ArrayList<String>(toAddresses.size());

    // Send to each recipient individually, do not throw exceptions until we try them all
    for (String toAddress : toAddresses) {
        try {
            LOG.debug("Sending email [" + messageSubject + "] to recipient [" + toAddress + "]");
            InternetAddress recipient = new InternetAddress(toAddress);
            Transport.send(mimeMessage, new InternetAddress[] { recipient });
        } catch (Exception e) {
            LOG.error("Failed to send email [" + messageSubject + "] to recipient [" + toAddress + "]: "
                    + e.getMessage());
            badAdresses.add(toAddress);

            // Remember the first error - in case its due to a session initialization problem,
            // we don't want to lose the first error.
            if (error == null) {
                error = e;
            }
        }
    }

    if (error != null) {
        LOG.error("Sending of emails failed for this reason: " + error.getMessage());
    }

    return badAdresses;
}