Example usage for javax.mail.internet MimeMessage setText

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

Introduction

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

Prototype

@Override
public void setText(String text) throws MessagingException 

Source Link

Document

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

Usage

From source file:MailExample.java

public static void main(String args[]) throws Exception {
    if (args.length != 3) {
        System.err.println("Usage: java MailExample host from to");
        System.exit(-1);//from   www .jav a 2  s  . co m
    }

    String host = args[0];
    String from = args[1];
    String to = args[2];

    // Get system properties
    Properties props = System.getProperties();

    // Setup mail server
    props.put("mail.smtp.host", host);

    // Get session
    Session session = Session.getDefaultInstance(props, null);

    // Define message
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
    message.setSubject("The Subject");
    message.setText("The Message");

    // Send message
    Transport.send(message);
}

From source file:msgsendsample.java

public static void main(String[] args) {
    if (args.length != 4) {
        usage();/*from w  ww  .  j a  va  2  s.  c o m*/
        System.exit(1);
    }

    System.out.println();

    String to = args[0];
    String from = args[1];
    String host = args[2];
    boolean debug = Boolean.valueOf(args[3]).booleanValue();

    // create some properties and get the default Session
    Properties props = new Properties();
    props.put("mail.smtp.host", host);
    if (debug)
        props.put("mail.debug", args[3]);

    Session session = Session.getInstance(props, null);
    session.setDebug(debug);

    try {
        // create a message
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(from));
        InternetAddress[] address = { new InternetAddress(to) };
        msg.setRecipients(Message.RecipientType.TO, address);
        msg.setSubject("JavaMail APIs Test");
        msg.setSentDate(new Date());
        // If the desired charset is known, you can use
        // setText(text, charset)
        msg.setText(msgText);

        Transport.send(msg);
    } catch (MessagingException mex) {
        System.out.println("\n--Exception handling in msgsendsample.java");

        mex.printStackTrace();
        System.out.println();
        Exception ex = mex;
        do {
            if (ex instanceof SendFailedException) {
                SendFailedException sfex = (SendFailedException) ex;
                Address[] invalid = sfex.getInvalidAddresses();
                if (invalid != null) {
                    System.out.println("    ** Invalid Addresses");
                    for (int i = 0; i < invalid.length; i++)
                        System.out.println("         " + invalid[i]);
                }
                Address[] validUnsent = sfex.getValidUnsentAddresses();
                if (validUnsent != null) {
                    System.out.println("    ** ValidUnsent Addresses");
                    for (int i = 0; i < validUnsent.length; i++)
                        System.out.println("         " + validUnsent[i]);
                }
                Address[] validSent = sfex.getValidSentAddresses();
                if (validSent != null) {
                    System.out.println("    ** ValidSent Addresses");
                    for (int i = 0; i < validSent.length; i++)
                        System.out.println("         " + validSent[i]);
                }
            }
            System.out.println();
            if (ex instanceof MessagingException)
                ex = ((MessagingException) ex).getNextException();
            else
                ex = null;
        } while (ex != null);
    }
}

From source file:Main.java

/**
 * Create a MimeMessage using the parameters provided.
 *
 * @param to Email address of the receiver.
 * @param from Email address of the sender, the mailbox account.
 * @param subject Subject of the email.// ww w  .  j a  v  a2 s .  c  om
 * @param bodyText Body text of the email.
 * @return MimeMessage to be used to send email.
 * @throws MessagingException
 */
public static MimeMessage createEmail(String to, String from, String subject, String bodyText)
        throws MessagingException {
    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);

    MimeMessage email = new MimeMessage(session);

    email.setFrom(new InternetAddress(from));
    email.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(to));
    email.setSubject(subject);
    email.setText(bodyText);
    return email;
}

From source file:gov.nih.nci.caarray.util.EmailUtil.java

/**
 * Sends mail based upon input parameters.
 *
 * @param mailRecipients List of strings that are the recipient email addresses
 * @param from the from of the email//ww w .ja v  a  2 s.co  m
 * @param mailSubject the subject of the email
 * @param mailBody the body of the email
 * @throws MessagingException thrown if there is a problem sending the message
 */
public static void sendMail(List<String> mailRecipients, String from, String mailSubject, String mailBody)
        throws MessagingException {
    MimeMessage message = constructMessage(mailRecipients, from, mailSubject);

    if (StringUtils.isEmpty(mailBody)) {
        LOG.info("No email body specified");
    }
    message.setText(mailBody);

    LOG.debug("sending email");
    Transport.send(message);
    LOG.debug("email successfully sent");
}

From source file:com.zimbra.qa.unittest.TestStoreManager.java

public static ParsedMessage getMessage() throws Exception {
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession());
    mm.setHeader("From", " Jimi <jimi@example.com>");
    mm.setHeader("To", " Janis <janis@example.com>");
    mm.setHeader("Subject", "Hello");
    mm.setHeader("Message-ID", "<sakfuslkdhflskjch@oiwm.example.com>");
    mm.setText("nothing to see here" + RandomStringUtils.random(1024));
    return new ParsedMessage(mm, false);
}

From source file:gt.dakaik.common.Common.java

public static MimeMessage createEmail(String to, String subject, String bodyText) throws MessagingException {
    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);

    MimeMessage email = new MimeMessage(session);

    email.setFrom(new InternetAddress(sendMailFrom));
    email.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(to));
    email.setSubject(subject);//from w  ww  .  ja  v  a  2 s .  c  o m
    email.setText(bodyText);
    return email;
}

From source file:org.xmlactions.email.EMailSend.java

public static void sendEMail(String fromAddress, String toAddress, String host, String userName,
        String password, String subject, String msg) throws AddressException, MessagingException {

    log.debug(String.format(//w ww . j  av  a  2 s.  c om
            "sendEMail(from:%s, to:%s, host:%s, userName:%s, password:%s)\nsubject:{" + subject
                    + "\n}\nmessage:{" + msg + "\n}",
            fromAddress, toAddress, host, userName, toPassword(password), subject, msg));
    Properties props = System.getProperties();
    props.put("mail.smtp.host", host);
    Session session;
    if (!StringUtils.isEmpty(password)) {
        props.put("mail.smtp.auth", "true");
        //EMailAuthenticator auth = new EMailAuthenticator(userName + "+" + host, password);
        EMailAuthenticator auth = new EMailAuthenticator(userName, password);
        session = Session.getInstance(props, auth);
    } else {
        session = Session.getInstance(props);
    }

    // Define message
    MimeMessage message = new MimeMessage(session);
    // message.setFrom(new InternetAddress("email_addresses@riostl.com"));
    message.setFrom(new InternetAddress(fromAddress));
    message.addRecipient(RecipientType.TO, new InternetAddress(toAddress));
    message.setSubject(subject);
    message.setText(msg);

    // Send message
    if (StringUtils.isEmpty(password)) {
        Transport.send(message);
    } else {
        Provider provider = session.getProvider("smtp");

        Transport transport = session.getTransport(provider);
        // Send message
        transport.connect();
        transport.sendMessage(message, new Address[] { new InternetAddress(toAddress) });
        transport.close();
    }

}

From source file:org.tsm.concharto.web.util.ConfirmationEmail.java

public static MimeMessage makeMessage(MimeMessage message, User user, String subject, String messageText) {
    InternetAddress from = new InternetAddress();
    from.setAddress(FROM_ADDRESS);//from w w  w  . j  ava 2s.  com
    InternetAddress to = new InternetAddress();
    to.setAddress(user.getEmail());
    try {
        from.setPersonal(FROM_NAME);
        message.addRecipient(Message.RecipientType.TO, to);
        message.setSubject(subject);
        message.setText(messageText);
        message.setFrom(from);
    } catch (UnsupportedEncodingException e) {
        log.error(e);
    } catch (MessagingException e) {
        log.error(e);
    }
    return message;
}

From source file:com.zimbra.cs.mailbox.MailboxTestUtil.java

public static ParsedMessage generateMessage(String subject) throws Exception {
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession());
    mm.setHeader("From", "Bob Evans <bob@example.com>");
    mm.setHeader("To", "Jimmy Dean <jdean@example.com>");
    mm.setHeader("Subject", subject);
    mm.setText("nothing to see here");
    return new ParsedMessage(mm, false);
}

From source file:com.email.SendEmailNotification.java

/**
 * This sends a basic notification email that is just pure TEXT. Message is
 * sent from the section gathered/*from   www.  ja  v a2 s  . c o  m*/
 *
 * @param eml DocketNotificationModel
 */
public static void sendNotificationEmail(DocketNotificationModel eml) {
    //Get Account
    SystemEmailModel account = null;
    for (SystemEmailModel acc : Global.getSystemEmailParams()) {
        if (acc.getSection().equals(eml.getSection())) {
            account = acc;
            break;
        }
    }
    if (account != null) {
        String FROMaddress = account.getEmailAddress();
        String[] TOAddressess = ((eml.getSendTo() == null) ? "".split(";") : eml.getSendTo().split(";"));
        String subject = eml.getMessageSubject();
        String body = eml.getMessageBody();

        Authenticator auth = EmailAuthenticator.setEmailAuthenticator(account);
        Properties properties = EmailProperties.setEmailOutProperties(account);

        Session session = Session.getInstance(properties, auth);
        MimeMessage email = new MimeMessage(session);

        try {
            for (String To : TOAddressess) {
                if (EmailValidator.getInstance().isValid(To)) {
                    email.addRecipient(Message.RecipientType.TO, new InternetAddress(To));
                }
            }

            email.setFrom(new InternetAddress(FROMaddress));
            email.setSubject(subject);
            email.setText(body);
            if (Global.isOkToSendEmail()) {
                Transport.send(email);
            } else {
                Audit.addAuditEntry("Notification Not Actually Sent: " + eml.getId() + " - " + subject);
            }

            DocketNotification.deleteEmailEntry(eml.getId());
        } catch (AddressException ex) {
            ExceptionHandler.Handle(ex);
        } catch (MessagingException ex) {
            ExceptionHandler.Handle(ex);
        }
    }
}