Example usage for javax.mail Message setRecipients

List of usage examples for javax.mail Message setRecipients

Introduction

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

Prototype

public abstract void setRecipients(RecipientType type, Address[] addresses) throws MessagingException;

Source Link

Document

Set the recipient addresses.

Usage

From source file:info.raack.appliancedetection.common.email.SMTPEmailSender.java

public void sendGeneralEmail(String recipientEmail, String subject, String body) {
    Session session = Session.getDefaultInstance(emailProperties, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }/*from  ww  w . j ava 2 s.  co m*/
    });

    Message message = new MimeMessage(session);

    try {
        message.setFrom(new InternetAddress(from));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail));
        message.setSubject(subject + (!environment.equals("prod") ? " [" + environment + "]" : ""));
        message.setText(body);
    } catch (Exception e) {
        throw new RuntimeException("Could not create message", e);
    }

    emailQueue.add(message);
}

From source file:com.gitlab.anlar.lunatic.server.ServerTest.java

private Message createBaseMessage(String from, String to, String subject, Session session)
        throws MessagingException {
    Message msg = new MimeMessage(session);

    msg.setFrom(new InternetAddress(from));
    msg.setRecipients(Message.RecipientType.TO, new InternetAddress[] { new InternetAddress(to) });
    msg.setSentDate(new Date());
    msg.setSubject(subject);/*from w  ww. j  a v  a  2s. c  o m*/

    return msg;
}

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

public void sendResourceEmail(String resourceName, String sender, String recipient, String subject) {

    try {/*from ww w . ja  va 2 s .c  o  m*/

        InputStream resource = TestMailClient.class.getClassLoader().getResourceAsStream(resourceName);
        if (resource == null) {
            throw new MessagingException("Couldn't find resource at: " + resourceName);
        }

        Message message = new MimeMessage(session, resource);

        // wipe out ALL exisitng recipients
        message.setRecipients(Message.RecipientType.TO, new Address[] {});
        message.setRecipients(Message.RecipientType.CC, new Address[] {});
        message.setRecipients(Message.RecipientType.BCC, new Address[] {});

        // then set the new data
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
        message.setFrom(InternetAddress.parse(sender)[0]);
        message.setSubject(subject);

        Transport.send(message);

        logger.info("Outgoing mail forwarded to " + recipient);

    } catch (MessagingException e) {
        throw new HoldMailException("couldn't send mail: " + e.getMessage(), e);
    }

}

From source file:de.fzi.ALERT.actor.ActionActuator.MailService.java

public void sendEmail(Authenticator auth, String address, String subject, String content) {
    try {/*from  w w  w.j a  v  a2  s.  c  om*/
        Properties props = new Properties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", port);
        props.put("mail.smtp.auth", true);
        props.put("mail.smtp.starttls.enable", "true");

        Session session = Session.getDefaultInstance(props, auth);
        // -- Create a new message --
        Message msg = new MimeMessage(session);

        // -- Set the FROM and TO fields --
        msg.setFrom(new InternetAddress(username));
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(address, false));

        // -- Set the subject and body text --
        msg.setSubject(subject);
        msg.setText(content);

        // -- Set some other header information --
        msg.setHeader("X-Mailer", "LOTONtechEmail");
        msg.setSentDate(new Date());

        // -- Send the message --
        Transport.send(msg);

        System.out.println("An announce Mail has been send to " + address);
    } catch (AddressException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.ayu.filter.RegularService.java

@Async
public void sendSSLMail(String text, String toMail) {

    final String username = "clouddefenceids";
    final String password = "";

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");

    Session session = Session.getInstance(props, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }//from  w ww  . j  a v  a 2s.  com
    });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(username));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toMail));
        message.setSubject("Forgot Password");
        message.setText(text);

        Transport.send(message);

        System.out.println("Done");

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

From source file:ua.aits.sdolyna.controller.MainController.java

@RequestMapping(value = { "/sendmail/", "/sendmail" }, method = RequestMethod.GET)
public @ResponseBody String sendMail(HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    request.setCharacterEncoding("UTF-8");
    String name = request.getParameter("name");
    String email = request.getParameter("email");
    String text = request.getParameter("text");
    final String username = "office@crc.org.ua";
    final String password = "crossroad2000";
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");
    Session session = Session.getInstance(props, new javax.mail.Authenticator() {
        protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
            return new javax.mail.PasswordAuthentication(username, password);
        }/*from w  w  w  .  ja  v  a 2 s.com*/
    });
    try {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(email));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("sonyachna-dolyna@ukr.net"));
        message.setSubject("E-mail ? ?:");
        message.setText("?: " + name + "\nEmail: " + email + "\n\n" + text);
        Transport.send(message);
        return "done";
    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.eclipse.skalli.core.mail.MailComponent.java

private void sendMailInternal(Address[] rcptTo, Address[] rcptCC, Address[] rcptBCC, Address from,
        String subject, String body) {
    try {// w  w w . j  ava2  s  . co m
        String mailHost = "mail.sap.corp"; //$NON-NLS-1$
        Properties props = System.getProperties();
        props.put("mail.smtp.host", mailHost); //$NON-NLS-1$
        Session session = Session.getInstance(props, null);
        Message message = new MimeMessage(session);
        message.setFrom(from);
        if (rcptTo != null && rcptTo.length > 0) {
            message.setRecipients(Message.RecipientType.TO, rcptTo);
        }
        if (rcptCC != null && rcptCC.length > 0) {
            message.setRecipients(Message.RecipientType.CC, rcptCC);
        }
        if (rcptBCC != null && rcptBCC.length > 0) {
            message.setRecipients(Message.RecipientType.BCC, rcptBCC);
        }

        message.setSubject(subject);
        message.setContent(body, "text/plain"); //$NON-NLS-1$
        Transport.send(message);
    } catch (AddressException e) {
        throw new RuntimeException(e);
    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.gazbert.bxbot.core.mail.EmailAlerter.java

public void sendMessage(String subject, String msgContent) {

    if (sendEmailAlertsEnabled) {

        final Session session = Session.getInstance(smtpProps, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(smtpConfig.getAccountUsername(),
                        smtpConfig.getAccountPassword());
            }/*from w  w  w  .ja  v  a  2  s . c o m*/
        });

        try {
            final Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(smtpConfig.getFromAddress()));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(smtpConfig.getToAddress()));
            message.setSubject(subject);
            message.setText(msgContent);

            LOG.info(() -> "About to send following Email Alert with message content: " + msgContent);
            Transport.send(message);

        } catch (MessagingException e) {
            // not much we can do here, especially if the alert was critical - the bot is shutting down; just log it.
            LOG.error("Failed to send Email Alert. Details: " + e.getMessage(), e);
        }
    } else {
        LOG.warn("Email Alerts are disabled. Not sending the following message: Subject: " + subject
                + " Content: " + msgContent);
    }
}

From source file:org.jkcsoft.java.mail.Emailer.java

/**
 * The final funnel point method that actually uses Java Mail (javax.mail.*)
 * API to send the message./*from  www.ja  va2  s.c  o  m*/
 *
 * @throws MessagingException
 */
private void _sendMsg(InternetAddress[] to, InternetAddress[] bccList, InternetAddress from, String subject,
        String msgBody, String strMimeType) throws MessagingException {
    if (Strings.isEmpty(msgBody)) {
        msgBody = "(no email body; see subject)";
    }
    Session session = Session.getDefaultInstance(javaMailProps, null);
    Message msg = new MimeMessage(session);
    msg.setRecipients(Message.RecipientType.TO, to);
    if (bccList != null) {
        msg.setRecipients(Message.RecipientType.BCC, bccList);
    }
    msg.setFrom(from);
    msg.setSubject(subject);
    msg.setSentDate(new Date());
    msg.setContent(msgBody, strMimeType);
    boolean doTrySend = true;
    int numTries = 0;
    while (doTrySend) {
        numTries++;
        try {
            Transport.send(msg);
            log.info("Sent email; subject=" + subject + " to " + to[0].getAddress() + " ");
            doTrySend = false;
        } catch (MessagingException me) {
            log.warn("Try " + numTries + " of " + MAXTRIES + " failed:", me);
            if (numTries == MAXTRIES) {
                log.error("Failed to send email", me);
                throw me;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e1) {
                LogHelper.error(this, "Retry sleep interrupted", e1);
            }
            doTrySend = numTries < MAXTRIES;
        }
    }
}

From source file:net.kamhon.ieagle.function.email.SendMail.java

public void send(String to, String cc, String bcc, String from, String subject, String text, String emailType) {

    // Session session = Session.getInstance(props, null);
    Session session = Session.getInstance(props, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(emailServerSetting.getUsername(),
                    emailServerSetting.getPassword());
        }/*from  w  w  w  . jav  a  2  s. co  m*/
    });

    session.setDebug(emailServerSetting.isDebug());

    try {
        Message msg = new MimeMessage(session);
        if (StringUtils.isNotBlank(from)) {
            msg.setFrom(new InternetAddress(from));
        } else {
            msg.setFrom();
        }

        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
        if (StringUtils.isNotBlank(cc)) {
            msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false));
        }
        if (StringUtils.isNotBlank(bcc)) {
            msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc, false));
        }

        msg.setSubject(subject);
        if (Emailq.TYPE_HTML.equalsIgnoreCase(emailType)) {
            msg.setContent(text, "text/html");
        } else {
            msg.setText(text);
        }
        msg.setSentDate(new java.util.Date());
        Transport.send(msg);
    } catch (MessagingException mex) {
        mex.printStackTrace();
        Exception ex = null;
        if ((ex = mex.getNextException()) != null) {
            ex.printStackTrace();
        }
        throw new SystemErrorException(mex);
    }
}