Example usage for javax.mail.internet MimeMessage setRecipient

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

Introduction

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

Prototype

public void setRecipient(RecipientType type, Address address) throws MessagingException 

Source Link

Document

Set the recipient address.

Usage

From source file:com.zimbra.cs.service.mail.SendVerificationCode.java

static void sendVerificationCode(String emailAddr, String code, Mailbox mbox)
        throws MessagingException, ServiceException {
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSmtpSession(mbox.getAccount()));
    mm.setRecipient(javax.mail.Message.RecipientType.TO, new JavaMailInternetAddress(emailAddr));
    mm.setText(L10nUtil.getMessage(L10nUtil.MsgKey.deviceSendVerificationCodeText,
            mbox.getAccount().getLocale(), code), MimeConstants.P_CHARSET_UTF8);
    mm.saveChanges();//w w w.j av a 2 s  .  c om
    MailSender mailSender = mbox.getMailSender();
    mailSender.setSaveToSent(false);
    mailSender.sendMimeMessage(null, mbox, mm);
}

From source file:com.muleinaction.GreenMailUtilities.java

public static MimeMessage toMessage(String text, String email) throws MessagingException {
    MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()));
    message.setContent(text, "text/plain");
    message.setRecipient(Message.RecipientType.TO, new InternetAddress(email));
    return message;
}

From source file:com.fiveamsolutions.nci.commons.util.MailUtils.java

/**
 * Send an email./*from w ww.  j  a  va  2 s. c  om*/
 * @param u the recipient of the message
 * @param title the subject of the message
 * @param html the html content of the message
 * @param plainText the plain text content of the message
 * @throws MessagingException on error.
 */
public static void sendEmail(AbstractUser u, String title, String html, String plainText)
        throws MessagingException {
    if (!isMailEnabled()) {
        LOG.info("sending email to " + u.getEmail() + " with title " + title);
        LOG.info("plain text: " + plainText);
        LOG.info("html: " + html);
        return;
    }
    MimeMessage msg = new MimeMessage(getMailSession());
    msg.setFrom(new InternetAddress(getFromAddress()));
    msg.setRecipient(Message.RecipientType.TO, new InternetAddress(u.getEmail()));
    msg.setSubject(title);
    Multipart mp = new MimeMultipart("alternative");
    BodyPart bp = new MimeBodyPart();
    bp.setContent(html, "text/html");
    mp.addBodyPart(bp);

    bp = new MimeBodyPart();
    bp.setContent(plainText, "text/plain");
    mp.addBodyPart(bp);

    msg.setContent(mp);

    Transport.send(msg);
}

From source file:com.bitranger.parknshop.mail.EMailSender.java

public boolean send(String address, String subject, String content) {

    LOG.debug("sending message [" + subject + "] to [" + address + "]");

    MimeMessage msg = sender.createMimeMessage();

    try {/*ww  w.j  ava2s. c om*/
        msg.setRecipient(RecipientType.TO, new InternetAddress(address));
        msg.setSubject(subject);
        msg.setText(content);

        sender.send(msg);

    } catch (Exception e) {
        LOG.error("failed sending email to [" + address + "]" + " subject [" + subject + "] content[" + content
                + "]");
        return false;
    }
    return false;
}

From source file:cl.preguntame.controller.PlataformaController.java

@ResponseBody
@RequestMapping(value = "/email", method = RequestMethod.POST)
public String correo(HttpServletRequest req) {

    try {/*from w  w  w. j a  v  a  2 s .  c  o  m*/
        String host = "smtp.gmail.com";

        Properties prop = System.getProperties();

        prop.put("mail.smtp.starttls.enable", "true");
        prop.put("mail.smtp.host", host);
        prop.put("mail.smtp.user", "hector.riquelme1169@gmail.com");
        prop.put("mail.smtp.password", "rriiqquueellmmee");
        prop.put("mail.smtp.port", 587);
        prop.put("mail.smtp.auth", "true");

        Session sesion = Session.getDefaultInstance(prop, null);
        MimeMessage mensaje = new MimeMessage(sesion);

        mensaje.setFrom(new InternetAddress());
        mensaje.setRecipient(Message.RecipientType.TO, new InternetAddress("hector.riquelme1169@gmail.com"));
        mensaje.setSubject("CONTACTO MIS CONCEPTOS");
        mensaje.setText(req.getParameter("mensaje_contacto"));

        Transport transport = sesion.getTransport("smtp");
        transport.connect(host, "hector.riquelme1169@gmail.com", "rriiqquueellmmee");
        transport.sendMessage(mensaje, mensaje.getAllRecipients());

        transport.close();

    } catch (Exception e) {

    }

    return req.getParameter("mensaje_contacto") + "  -  " + req.getParameter("email_contacto");
}

From source file:ru.codemine.ccms.mail.EmailService.java

public void sendSimpleMessage(String address, String subject, String content) {
    try {//from w w w .j a va2  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.mime.charset", "UTF-8");
        props.put("mail.smtp.ssl.enable", ssl);

        Session session = Session.getInstance(props, new EmailAuthenticator(username, password));

        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(username, ""));
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(address));
        message.setSubject(subject);
        message.setText(content, "utf-8", "html");
        Transport transport = session.getTransport("smtp");
        transport.connect();
        transport.sendMessage(message, message.getAllRecipients());
    } catch (MessagingException | UnsupportedEncodingException ex) {
        log.error(
                "?  ? email,  : "
                        + ex.getLocalizedMessage());
        ex.printStackTrace();
    }

}

From source file:com.otz.plugins.transport.aws.ses.SpringEmailSender.java

public void send(String templateName, Locale locale, String to, Map<String, String> parameters)
        throws MessagingException {

    MimeMessage msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress(emailTemplateRepository.getFrom(templateName, locale)));
    msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
    msg.setSubject(emailTemplateRepository.getSubject(templateName, locale));
    msg.setContent(emailTemplateRepository.getContent(templateName, locale, parameters),
            emailTemplateRepository.getContentType(templateName, locale));

    try {/*from ww  w .jav  a2  s  .c  o  m*/
        // Create a transport.
        transport = session.getTransport();
        transport.connect(emailConfigParameters.getHost(), emailConfigParameters.getAccessKey(),
                emailConfigParameters.getSecretKey());
        transport.sendMessage(msg, msg.getAllRecipients());

    } catch (Exception e) {

        // TODO deal with failure to send
        // send message to hipchat
        e.printStackTrace();
    }

}

From source file:org.lf.yydp.service.film.BuyTicketService.java

/**
 * :?//from  w  ww . j  a v  a  2s  . c om
 * @param receiver
 */
public void sendEmail(String receiver) {
    Properties p = null;
    InputStream inputSteam = null;
    String senter = null;
    String Subject = null;
    String Content = null;
    try {
        p = new Properties();
        inputSteam = BuyTicketService.class.getClassLoader().getResourceAsStream("mail.properties");
        p.load(inputSteam);
        final String uname = p.getProperty("mail.uname");
        final String license = p.getProperty("mail.license");
        senter = p.getProperty("mail.senter");
        Subject = p.getProperty("mail.subject");
        Content = p.getProperty("mail.content");
        Authenticator authenticator = new Authenticator() {
            @Override
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(uname, license);
            }
        };
        Session session = Session.getInstance(p, authenticator);

        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(senter));
        msg.setRecipient(RecipientType.TO, new InternetAddress(receiver));
        msg.setSubject(Subject);
        msg.setContent(Content, "text/html;charset=utf-8");
        Transport.send(msg);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (inputSteam != null) {
                inputSteam.close();
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }

    }
}

From source file:de.blizzy.documentr.mail.SubscriptionMailer.java

private void sendMail(String subject, String text, String senderEmail, String senderName,
        Set<String> subscriberEmails, JavaMailSender sender) {

    for (String subscriberEmail : subscriberEmails) {
        try {/*from w  w  w.  j a  v  a 2  s  . c om*/
            MimeMessage msg = sender.createMimeMessage();
            msg.setFrom(createAddress(senderEmail, senderName));
            msg.setRecipient(RecipientType.TO, createAddress(subscriberEmail, null));
            msg.setSubject(subject, Charsets.UTF_8.name());
            msg.setText(text, Charsets.UTF_8.name());
            sender.send(msg);
        } catch (MessagingException e) {
            log.error("error while sending mail", e); //$NON-NLS-1$
        }
    }
}

From source file:eu.scape_project.planning.application.Feedback.java

/**
 * Method responsible for sending feedback per mail.
 * //from  w w w. j a  v  a2 s .  c om
 * @param userEmail
 *            email address of the user.
 * @param userComments
 *            comments from the user
 * @param location
 *            the location of the application where the error occurred
 * @param applicationName
 *            the name of the application
 * @throws MailException
 *             if the feedback could not be sent
 */
public void sendFeedback(String userEmail, String userComments, String location, String applicationName)
        throws MailException {

    try {
        Properties props = System.getProperties();

        props.put("mail.smtp.host", config.getString("mail.smtp.host"));
        Session session = Session.getDefaultInstance(props, null);

        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(config.getString("mail.from")));
        message.setRecipient(RecipientType.TO, new InternetAddress(config.getString("mail.feedback")));

        message.setSubject("[" + applicationName + "] from " + location);

        StringBuilder builder = new StringBuilder();
        builder.append("Date: ").append(dateFormat.format(new Date())).append("\n\n");

        // User info
        if (user == null) {
            builder.append("No user available.\n\n");
        } else {
            builder.append("User: ").append(user.getUsername()).append("\n");
            if (user.getUserGroup() != null) {
                builder.append("Group: ").append(user.getUserGroup().getName()).append("\n");
            }
        }
        builder.append("UserMail: ").append(userEmail).append("\n\n");

        // Comments
        builder.append("Comments:\n");
        builder.append(SEPARATOR_LINE);
        builder.append(userComments).append("\n");
        builder.append(SEPARATOR_LINE).append("\n");
        message.setText(builder.toString());
        message.saveChanges();

        Transport.send(message);

        log.debug("Feedback mail sent successfully to {}", config.getString("mail.feedback"));
    } catch (MessagingException e) {
        log.error("Error sending feedback mail to {}", config.getString("mail.feedback"), e);
        throw new MailException("Error sending feedback mail to " + config.getString("mail.feedback"), e);
    }
}