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:uk.ac.ox.it.ords.api.statistics.services.impl.SendMailTLS.java

private void sendMail() {
    if (sendEmails) {
        if (username == null) {
            log.error("Unable to send emails due to null user");
            return;
        }/*from w  w w  . j av a2  s  . c o  m*/
        Session session = Session.getInstance(props, new javax.mail.Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(props.get("mail.smtp.username").toString(),
                        props.get("mail.smtp.password").toString());
            }
        });

        try {
            Message message = new MimeMessage(session);
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email));
            message.setSubject(subject);
            message.setText(messageText);
            message.setFrom(new InternetAddress("ords@it.ox.ac.uk"));

            Transport.send(message);

            if (log.isDebugEnabled()) {
                log.debug(String.format("Sent email to %s (name %s)", email, username));
                log.debug("with content: " + messageText);
            }

        } catch (MessagingException e) {
            log.error("Unable to send email to " + email + " username " + username, e);
        }
    }
}

From source file:org.nekorp.workflow.desktop.servicio.imp.ServicioAlertaVerificacionEmailImp.java

@Override
public void enviarAlerta(List<AlertaVerificacion> alertas) {
    try {//w  ww.  ja  v  a 2 s  . c  o m
        for (AlertaVerificacion x : alertas) {
            Message message = new MimeMessage(template.buildSession());
            message.setFrom(new InternetAddress(template.getMailSender()));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(template.getMailRecipient()));
            message.setSubject("Alerta de Verificacin");
            ST contenido = new ST(contenidoRaw);
            contenido.add("placas", x.getPlacas());
            contenido.add("periodo", x.getPeriodo());
            message.setText(contenido.render());
            Transport.send(message);
        }
    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.capelin.mvc.mail.SMTPMailSender.java

protected boolean send(String recipient, String body, boolean html) {
    Properties props = System.getProperties();
    props.put("mail.smtp.host", serverName);
    Session session = Session.getInstance(props, null);
    props.put("mail.from", sender);
    Message msg = new MimeMessage(session);
    try {//  w w  w  .  j  a  v a 2 s  . c o  m
        msg.setSubject(subject);
        msg.setFrom();
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient, false));
        if (html) {
            msg.setContent(new String(body.getBytes(), "iso-8859-1"), "text/html; charset=iso-8859-1");
        } else {
            msg.setText(body);
        }
        // Send the message:
        Transport.send(msg);
        log.debug("Email send to: " + sender);
        return true;
    } catch (MessagingException e) {
        log.info("Email Sending failed due to " + e);
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return false;
}

From source file:ips1ap101.ejb.core.mail.MailerBean.java

@Override
public Message sendMessage(String addressList, String subject, String text) throws MessagingException {
    if (EA.isMailingEnabled()) {
    } else {//from ww w . jav  a2 s.  com
        return null;
    }
    if (StringUtils.isBlank(addressList)) {
        throw new InvalidParameterException("addressList");
    }
    if (StringUtils.isBlank(subject)) {
        throw new InvalidParameterException("subject");
    }
    if (StringUtils.isBlank(text)) {
        throw new InvalidParameterException("text");
    }
    Address[] internetAddressList = InternetAddress.parse(addressList, false);
    Date timeStamp = new Date();
    Message message = new MimeMessage(session);
    message.setFrom();
    message.setHeader("X-Mailer", "JavaMail");
    message.setRecipients(Message.RecipientType.TO, internetAddressList);
    message.setSentDate(timeStamp);
    message.setSubject(subject);
    message.setText(text);
    Transport.send(message);
    return message;
}

From source file:org.nekorp.workflow.desktop.servicio.imp.ServicioAlertaEmailImp.java

@Override
public void enviarAlerta(List<AlertaServicio> alertas) {
    try {//from www . j a  v a2 s  . c  o m
        for (AlertaServicio x : alertas) {
            Message message = new MimeMessage(template.buildSession());
            message.setFrom(new InternetAddress(template.getMailSender()));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(template.getMailRecipient()));
            message.setSubject("Alerta");
            ST contenido = new ST(contenidoRaw);
            contenido.add("cliente", x.getNombreCliente());
            contenido.add("tipo", x.getTipoAuto());
            contenido.add("marca", x.getMarcaAuto());
            contenido.add("placas", x.getPlacasAuto());
            contenido.add("kilometraje", x.getKilometrajeAuto());
            contenido.add("servicio", x.getDescripcionServicio());
            contenido.add("kilometrajeServicio", x.getKilometrajeServicio());
            message.setText(contenido.render());
            Transport.send(message);
        }
    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}

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

public boolean sendMailUsingJavaMailAPI(String to, String message1, HttpServletRequest req, String mid,
        String createdby, String pname, String mname) {
    String FinalMessage = new FreeMarkerMailTemplateCreater().createAndReturnTemplateDataMapping(message1,
            getTemplateLocation(req), mid, createdby, pname, mname);
    String from = "analytixdstest@gmail.com";
    final String username = "analytixdstest@gmail.com";
    final String password = "analytix000";
    String host = "smtp.gmail.com";
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.port", "587");
    Session session = Session.getInstance(props, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }//w w  w.j ava  2  s  .c om
    });
    try {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
        message.setContent(FinalMessage, "text/html; charset=utf-8");
        message.saveChanges();
        message.setSubject("Analytix Test Mail");
        Transport.send(message);
        return true;
    } catch (MessagingException e) {
        return false;

    }
}

From source file:mb.MbTermin.java

private void posaljiMail(Student student, Profesor profesorId, Termin t) {

    //ToDo change username and password for google account
    final String username = "*****";
    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() {
        @Override/*from  w w w . ja  va 2 s  .  c o  m*/
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    });

    try {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(username));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(profesorId.getEmail()));
        message.setSubject("Konsultacije");
        message.setText("Potovani " + profesorId.getIme() + " " + profesorId.getPrezime() + ","
                + "\n\n Student " + student.getIme() + " " + student.getPrezime()
                + " je zakazao termin konsultacija" + " za datum " + t.getTerminPK().getVreme() + ".");
        Transport.send(message);

        System.out.println("Message sent");
    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}

From source file:ua.aits.crc.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);
        }//  w  ww.  j a va2s. c  om
    });
    try {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(email));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("office@crc.org.ua"));
        message.setSubject("Mail from site");
        message.setText("Name: " + name + "\nEmail: " + email + "\n\n" + text);
        Transport.send(message);
        return "done";
    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.spartasystems.holdmail.smtp.OutgoingMailSender.java

public void redirectMessage(String recipient, String rawBody) {

    // TODO: this is a crude first pass at bouncing a mail and probably needs to be a little more sophisticated

    try {/*from w ww .j a  va 2  s  .  c  om*/

        Session session = getMailSession();
        Message message = initializeMimeMessage(rawBody, session);

        // 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[] {});

        // and set the new recipient
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));

        InternetAddress[] parsedFrom = InternetAddress.parse(getSenderFrom());
        if (parsedFrom.length > 0) {
            message.setFrom(parsedFrom[0]);
            logger.info("Outgoing mail will have From: " + parsedFrom[0].getAddress());
        }

        sendMessage(message);

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

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

}

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

public void sendEmail(String fromEmail, String toEmail, String subject, String textBody, String htmlBody) {
    try {/* www .ja va  2s  .c o m*/
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(fromEmail));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
        message.setSubject(subject);

        // Set the message
        createMultiMimePart(message, textBody, htmlBody);

        Transport.send(message);
    } catch (MessagingException e) {
        throw new HoldMailException("Failed to send email : " + e.getMessage(), e);
    }
}