Example usage for javax.mail.internet MimeMessage setRecipients

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

Introduction

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

Prototype

public void setRecipients(Message.RecipientType type, String addresses) throws MessagingException 

Source Link

Document

Set the specified recipient type to the given addresses.

Usage

From source file:de.elomagic.mag.AbstractTest.java

protected MimeMessage createMimeMessage(String filename) throws Exception {

    MimeBodyPart textPart = new MimeBodyPart();
    textPart.setContent("This is some text to be displayed inline", "text/plain");
    textPart.setDisposition(Part.INLINE);

    MimeBodyPart binaryPart = new MimeBodyPart();
    InputStream in = getClass().getResourceAsStream("/TestFile.pdf");
    binaryPart.setContent(getOriginalMailAttachment(), "application/pdf");
    binaryPart.setDisposition(Part.ATTACHMENT);
    binaryPart.setFileName(filename);//from  w  w  w  . ja  va2  s  .  co m

    Multipart mp = new MimeMultipart();
    mp.addBodyPart(textPart);
    mp.addBodyPart(binaryPart);

    MimeMessage message = new MimeMessage(greenMail.getImap().createSession());
    message.setRecipients(Message.RecipientType.TO,
            new InternetAddress[] { new InternetAddress("mailuser@localhost.com") });
    message.setSubject("Another test mail subject");
    message.setContent(mp);

    //
    return message; // GreenMailUtil.createTextEmail("to@localhost.com", "from@localhost.com", "subject", "body", greenMail.getImap().getServerSetup());
}

From source file:jease.cms.service.Mails.java

/**
 * Sends an email synchronously.//from  w  w w  .j a v  a 2s .  c o  m
 */
public void send(String sender, String recipients, String subject, String text) throws MessagingException {
    if (properties != null) {
        Session session = Session.getInstance(properties.asProperties(), new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(properties.getUser(), properties.getPassword());
            }
        });
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(sender));
        message.setReplyTo(new InternetAddress[] { new InternetAddress(sender) });
        message.setRecipients(Message.RecipientType.TO, recipients);
        message.setSubject(subject, "utf-8");
        message.setSentDate(new Date());
        message.setHeader("Content-Type", "text/plain; charset=\"utf-8\"");
        message.setHeader("Content-Transfer-Encoding", "quoted-printable");
        message.setText(text, "utf-8");
        Transport.send(message);
    }
}

From source file:com.threepillar.labs.meeting.email.EmailInviteImpl.java

@Override
public void sendInvite(final String subject, final String description, final Participant from,
        final List<Participant> attendees, final Date startDate, final Date endDate, final String location)
        throws Exception {

    this.properties.put("mail.smtp.socketFactory.port", properties.get("mail.smtp.port"));
    this.properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    this.properties.put("mail.smtp.socketFactory.fallback", "false");

    validate();//www . ja  va 2 s .  c o  m

    LOG.info("Sending meeting invite");
    LOG.debug("Mail Properties :: " + this.properties);

    Session session;
    if (password != null) {
        session = Session.getInstance(this.properties, new javax.mail.Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });
    } else {
        session = Session.getInstance(this.properties);
    }

    ICal cal = new ICal(subject, description, from, attendees, startDate, endDate, location);
    cal.init();

    StringBuffer sb = new StringBuffer();
    sb.append(from.getEmail());
    for (Participant bean : attendees) {
        if (sb.length() > 0) {
            sb.append(",");
        }
        sb.append(bean.getEmail());
    }
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from.getEmail()));
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(sb.toString()));
    message.setSubject(subject);
    Multipart multipart = new MimeMultipart();
    MimeBodyPart iCal = new MimeBodyPart();
    iCal.setDataHandler(new DataHandler(new ByteArrayDataSource(new ByteArrayInputStream(cal.toByteArray()),
            "text/calendar;method=REQUEST;charset=\"UTF-8\"")));

    LOG.debug("Calender Request :: \n" + cal.toString());

    multipart.addBodyPart(iCal);
    message.setContent(multipart);
    Transport.send(message);
}

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

/**
 * Makes a HTML message with one recipient
 * @return/*w w  w  .ja  v  a 2 s  .  com*/
 * @throws Exception
 */
public MimeMessage makeHTMLMessage(InternetAddress internetAddressFrom, InternetAddress[] internetAddressesTo,
        String subject, String htmlBody, List<LabelValueBean> attachments) throws Exception {

    LOGGER.debug("Constructing the HTML MimeMessage with one recipient");
    MimeMessage msg = prepareHTMLMimeMessage(internetAddressFrom, subject, htmlBody, attachments);

    msg.setRecipients(RecipientType.TO, internetAddressesTo);
    msg.saveChanges();
    return msg;
}

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

/**
 * Makes a plain message for one recipient
 * @param subject/*from   w  w w.ja v a 2s . c om*/
 * @return
 * @throws Exception
 */
public MimeMessage makePlainMessage(InternetAddress internetAddressFrom, InternetAddress[] internetAddressesTo,
        String subject, String plainBody, List<LabelValueBean> attachments) throws Exception {

    LOGGER.debug("Constructing the plain MimeMessage with one recipient");
    MimeMessage msg = preparePlainMimeMessage(internetAddressFrom, subject, plainBody, attachments);
    msg.setRecipients(RecipientType.TO, internetAddressesTo);
    msg.saveChanges();
    return msg;
}

From source file:de.tuttas.servlets.MailSender.java

private void transmitMail(MailObject mo) throws MessagingException {

    // creates a new session with an authenticator
    Authenticator auth = new Authenticator() {
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(Config.getInstance().user, Config.getInstance().pass);
        }/*from  www.  j  a  v a2  s.  co  m*/
    };
    Session session = Session.getInstance(properties, auth);
    // creates a new e-mail message
    MimeMessage msg = new MimeMessage(session);

    msg.setFrom(new InternetAddress(mo.getFrom()));
    InternetAddress[] toAddresses = mo.getRecipient();
    msg.setRecipients(Message.RecipientType.TO, toAddresses);
    InternetAddress[] bccAdresses = mo.getBcc();
    InternetAddress[] ccAdresses = mo.getCC();

    if (bccAdresses[0] != null)
        msg.setRecipients(Message.RecipientType.BCC, bccAdresses);
    if (ccAdresses[0] != null)
        msg.setRecipients(Message.RecipientType.CC, ccAdresses);
    msg.setSubject(mo.getSubject(), "UTF-8");

    msg.setSentDate(new Date());
    msg.setContent(mo.getContent(), "text/plain; charset=UTF-8");
    // sends the e-mail
    // TODO Kommentar entfernen
    Transport.send(msg);
}

From source file:org.eurekastreams.server.service.actions.strategies.EmailerFactory.java

/**
 * Sets the primary ('to') recipients.// w  w  w.ja  v a  2s  . c o m
 *
 * @param message
 *            Email message being built.
 * @param emailToString
 *            Comma delimited list of email addresses Email is being sent to.
 * @throws MessagingException
 *             Thrown if there are problems creating the message.
 */
public void setTo(final MimeMessage message, final String emailToString) throws MessagingException {
    message.setRecipients(RecipientType.TO, emailToString);
}

From source file:org.eurekastreams.server.service.actions.strategies.EmailerFactory.java

/**
 * Sets the CC recipients./*from w  ww .  j  a v  a2 s  .  c  om*/
 *
 * @param message
 *            Email message being built.
 * @param emailToString
 *            Comma delimited list of email addresses Email is being sent to.
 * @throws MessagingException
 *             Thrown if there are problems creating the message.
 */
public void setCc(final MimeMessage message, final String emailToString) throws MessagingException {
    message.setRecipients(RecipientType.CC, emailToString);
}

From source file:org.eurekastreams.server.service.actions.strategies.EmailerFactory.java

/**
 * Sets the BCC recipients./*  w  w w.j ava 2s . com*/
 *
 * @param message
 *            Email message being built.
 * @param emailToString
 *            Comma delimited list of email addresses Email is being sent to.
 * @throws MessagingException
 *             Thrown if there are problems creating the message.
 */
public void setBcc(final MimeMessage message, final String emailToString) throws MessagingException {
    message.setRecipients(RecipientType.BCC, emailToString);
}

From source file:net.sourceforge.subsonic.backend.service.EmailSession.java

private MimeMessage createMessage(String from, List<String> to, List<String> cc, List<String> bcc,
        List<String> replyTo, String subject) throws MessagingException {
    MimeMessage message = new MimeMessage(session);

    message.setFrom(new InternetAddress(from));
    message.setReplyTo(new Address[] { new InternetAddress(from) });
    message.setRecipients(Message.RecipientType.TO, convertAddress(to));
    message.setRecipients(Message.RecipientType.CC, convertAddress(cc));
    message.setRecipients(Message.RecipientType.BCC, convertAddress(bcc));
    message.setReplyTo(convertAddress(replyTo));
    message.setSubject(subject);/*from w  ww . j a v  a  2 s  .  c o  m*/
    return message;
}