Example usage for javax.mail Message setText

List of usage examples for javax.mail Message setText

Introduction

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

Prototype

public void setText(String text) throws MessagingException;

Source Link

Document

A convenience method that sets the given String as this part's content with a MIME type of "text/plain".

Usage

From source file:com.app.mail.DefaultMailSender.java

private static Message _populateContactMessage(String emailAddress, String messageBody, Session session)
        throws Exception {

    Message message = new MimeMessage(session);

    message.setFrom(new InternetAddress(emailAddress));

    message.addRecipient(Message.RecipientType.TO,
            new InternetAddress(PropertiesValues.OUTBOUND_EMAIL_ADDRESS));

    message.setSubject("You Have A New Message From " + emailAddress);
    message.setText(messageBody);

    return message;
}

From source file:NotificationMessage.java

static void sendMail(String toUser, Severity s) {
    // Recipient's email ID needs to be mentioned.
    //String to = "abhiyank@gmail.com";//change accordingly

    // Sender's email ID needs to be mentioned
    String from = "mdtprojectteam16@gmail.com";//change accordingly
    final String username = "mdtprojectteam16";//change accordingly
    final String password = "mdtProject16";//change accordingly

    // Assuming you are sending email through relay.jangosmtp.net
    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");

    // Get the Session object.
    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 {
        // Create a default MimeMessage object.
        javax.mail.Message message1 = new MimeMessage(session);

        // Set From: header field of the header.
        message1.setFrom(new InternetAddress(from));

        // Set To: header field of the header.
        message1.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toUser));

        // Set Subject: header field
        message1.setSubject("Alert Message From Patient");

        // Now set the actual message
        message1.setText(messageMap.get(s));

        // Send message
        Transport.send(message1);

        System.out.println("Sent message successfully....");

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

From source file:org.vulpe.commons.util.VulpeEmailUtil.java

/**
 * Send mail to recipients by Web Service.
 *
 * @param recipients/*from www.ja v a2s . c  om*/
 *
 * @param subject
 *
 * @param body
 *
 * @param mailerService
 *
 * @throws VulpeSystemException
 *             exception
 */
public static void sendMailByService(final String[] recipients, final String subject, final String body,
        final String mailerService) {
    try {
        final ResourceBundle bundle = ResourceBundle.getBundle("mail");
        String mailFrom = "";
        if (bundle.containsKey("mail.from")) {
            mailFrom = bundle.getString("mail.from");
        }
        final InitialContext initialContext = new InitialContext();
        final Session session = (Session) initialContext.lookup(mailerService);
        final Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(mailFrom));
        for (String recipient : recipients) {
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
        }
        // msg.setRecipient(Message.RecipientType.TO, new
        // InternetAddress(to));
        message.setSubject(subject);
        message.setText(body);
        Transport.send(message);
    } catch (Exception e) {
        LOG.error(e.getMessage());
    }

}

From source file:com.mycompany.craftdemo.utility.java

public static void send(long phno, double price, double profit, String domain, String company) {
    HashMap<String, String> domainMap = new HashMap<>();
    domainMap.put("TMobile", "tmomail.net ");
    domainMap.put("ATT", "txt.att.net");
    domainMap.put("Sprint", "messaging.sprintpcs.com");
    domainMap.put("Verizon", "vtext.com");
    String to = phno + "@" + domainMap.get(domain); //change accordingly

    // Sender's email ID needs to be mentioned
    String from = "uni5prince@gmail.com"; //change accordingly
    final String username = "uni5prince"; //change accordingly
    final String password = "savageph8893"; //change accordingly

    // Assuming you are sending email through relay.jangosmtp.net
    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");

    // Get the Session object.
    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.c  om
    });

    try {
        // Create a default MimeMessage object.
        Message message = new MimeMessage(session);

        // Set From: header field of the header.
        message.setFrom(new InternetAddress(from));

        // Set To: header field of the header.
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));

        // Set Subject: header field
        message.setSubject("Prices have gone up!!");

        // Now set the actual message
        message.setText("Hello Jane, Stock prices for " + company + " has reached to $" + price
                + " with profit of $" + profit);

        // Send message
        Transport.send(message);

        System.out.println("Sent message successfully....");

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

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

private void sendSinglePartEmail(String from, String to, String subject, String body)
        throws MessagingException {
    Properties props = createEmailProps(serverPort);
    Session session = Session.getInstance(props);

    Message msg = createBaseMessage(from, to, subject, session);
    msg.setText(body);

    Transport.send(msg);/* w w w . j a  v a2  s. c o m*/
}

From source file:org.apache.oodt.cas.crawl.action.EmailNotification.java

@Override
public boolean performAction(File product, Metadata metadata) throws CrawlerActionException {
    try {//from   w  w  w  . j a v a  2 s .com
        Properties props = new Properties();
        props.put("mail.host", this.mailHost);
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.from", this.sender);

        Session session = Session.getDefaultInstance(props);
        Message msg = new MimeMessage(session);
        msg.setSubject(PathUtils.doDynamicReplacement(subject, metadata));
        msg.setText(new String(PathUtils.doDynamicReplacement(message, metadata).getBytes()));
        for (String recipient : recipients) {
            try {
                msg.addRecipient(Message.RecipientType.TO, new InternetAddress(
                        PathUtils.replaceEnvVariables(recipient.trim(), metadata), ignoreInvalidAddresses));
                LOG.fine("Recipient: " + PathUtils.replaceEnvVariables(recipient.trim(), metadata));
            } catch (AddressException ae) {
                LOG.fine("Recipient: " + PathUtils.replaceEnvVariables(recipient.trim(), metadata));
                LOG.warning(ae.getMessage());
            }
        }
        LOG.fine("Subject: " + msg.getSubject());
        LOG.fine("Message: " + new String(PathUtils.doDynamicReplacement(message, metadata).getBytes()));
        Transport.send(msg);
        return true;
    } catch (Exception e) {
        LOG.severe(e.getMessage());
        return false;
    }
}

From source file:uk.ac.ox.it.ords.api.project.services.impl.SendMailTLS.java

protected void sendMail(String subject, String messageText) throws Exception {

    ////from ww w.  j  ava 2 s.c  om
    // Validate Mail server settings
    //
    if (!props.containsKey("mail.smtp.username") || !props.containsKey("mail.smtp.password")
            || !props.containsKey("mail.smtp.host")) {
        log.error("Unable to send emails as email server configuration is missing");
        throw new Exception("Unable to send emails as email server configuration is missing");
    }

    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);

        Transport.send(message);

        if (log.isDebugEnabled()) {
            log.debug(String.format("Sent email to %s", email));
            log.debug("with content: " + messageText);
        }
    } catch (MessagingException e) {
        log.error("Unable to send email to " + email, e);
        throw new Exception("Unable to send email to " + email, e);
    }
}

From source file:uk.ac.ox.it.ords.api.database.services.impl.SendMailTLS.java

protected void sendMail(String subject, String messageText) throws Exception {

    ////  w  w w  .  j a  va 2 s  .c o  m
    // Validate Mail server settings
    //
    if (!props.containsKey("mail.smtp.username") || !props.containsKey("mail.smtp.password")
            || !props.containsKey("mail.smtp.host")) {
        log.error("Unable to send emails as email server configuration is missing");
        //throw new Exception("Unable to send emails as email server configuration is missing");
        return; // 
    }

    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);

        Transport.send(message);

        if (log.isDebugEnabled()) {
            log.debug(String.format("Sent email to %s", email));
            log.debug("with content: " + messageText);
        }
    } catch (MessagingException e) {
        log.error("Unable to send email to " + email, e);
        throw new Exception("Unable to send email to " + email, e);
    }
}

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;
        }/* www  .  j  a  va2 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:pmp.springmail.TestDrive.java

void sendEmailViaPlainMail(String text) {
    Authenticator authenticator = new Authenticator() {

        @Override//from   w w  w  .ja  va2  s . c o  m
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(USER, PASS);
        }
    };

    Session session = Session.getInstance(props, authenticator);
    Message message = new MimeMessage(session);

    try {
        message.setFrom(new InternetAddress(FROM));
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(TO));
        message.setSubject("Plain JavaMail Test");
        message.setText(DATE_FORMAT.format(new Date()) + " " + text);

        Transport.send(message);

    } catch (Exception e) {
        System.err.println(e.getClass().getSimpleName() + " " + e.getMessage());
    }
}