Example usage for org.apache.commons.mail HtmlEmail setFrom

List of usage examples for org.apache.commons.mail HtmlEmail setFrom

Introduction

In this page you can find the example usage for org.apache.commons.mail HtmlEmail setFrom.

Prototype

public Email setFrom(final String email, final String name) throws EmailException 

Source Link

Document

Set the FROM field of the email to use the specified address and the specified personal name.

Usage

From source file:org.oscarehr.util.EmailUtils.java

/**
 * This is a convenience method for sending and email to 1 recipient using the configuration file settings.
 * @throws EmailException //from ww  w .  j  a v  a  2s.c  o m
 */
public static void sendEmail(String toEmailAddress, String toName, String fromEmailAddress, String fromName,
        String subject, String textContents, String htmlContents) throws EmailException {
    HtmlEmail htmlEmail = getHtmlEmail();

    htmlEmail.addTo(toEmailAddress, toName);
    htmlEmail.setFrom(fromEmailAddress, fromName);

    htmlEmail.setSubject(subject);
    if (textContents != null)
        htmlEmail.setTextMsg(textContents);
    if (htmlContents != null)
        htmlEmail.setHtmlMsg(htmlContents);

    htmlEmail.send();
}

From source file:org.oscarehr.util.EmailUtils.java

/**
 * This main method is useful when debugging smtp configuration problems.
 *//*w w  w .  j  av a2  s.  c om*/
public static void main(String... argv) throws EmailException {
    // gmail : smtp.gmail.com:465      

    String fromEmailAddress = argv[0];
    String toEmailAddress = argv[1];
    String smtpServer = argv[2];

    String smtpPort = (argv.length > 3 ? argv[3] : null);
    String smtpUser = (argv.length > 4 ? argv[4] : null);
    String smtpPassword = (argv.length > 5 ? argv[4] : null);
    String connectionSecurity = (argv.length > 6 ? argv[5] : null);
    HtmlEmail htmlEmail = EmailUtils.getHtmlEmail(smtpServer, smtpPort, smtpUser, smtpPassword,
            connectionSecurity);

    htmlEmail.addTo(toEmailAddress, toEmailAddress);
    htmlEmail.setFrom(fromEmailAddress, fromEmailAddress);

    htmlEmail.setSubject("test subject");
    htmlEmail.setTextMsg("test contents " + (new java.util.Date()));

    htmlEmail.send();
}

From source file:org.oscarehr.util.EmailUtilsOld.java

/**
 * This main method is useful when debugging smtp configuration problems.
 *//* ww  w.j  a  v a 2s .c o m*/
public static void main(String... argv) throws EmailException {
    // gmail : smtp.gmail.com:465      

    String fromEmailAddress = argv[0];
    String toEmailAddress = argv[1];
    String smtpServer = argv[2];

    String smtpPort = (argv.length > 3 ? argv[3] : null);
    String smtpUser = (argv.length > 4 ? argv[4] : null);
    String smtpPassword = (argv.length > 5 ? argv[4] : null);
    String connectionSecurity = (argv.length > 6 ? argv[5] : null);
    HtmlEmail htmlEmail = EmailUtilsOld.getHtmlEmail(smtpServer, smtpPort, smtpUser, smtpPassword,
            connectionSecurity);

    htmlEmail.addTo(toEmailAddress, toEmailAddress);
    htmlEmail.setFrom(fromEmailAddress, fromEmailAddress);

    htmlEmail.setSubject("test subject");
    htmlEmail.setTextMsg("test contents " + (new java.util.Date()));

    htmlEmail.send();
}

From source file:org.xmatthew.spy2servers.component.util.EMailUtils.java

/**
 * <p>//from w  w w  . j  a v a  2 s .  c  o m
 * send html body mail
 * </p>
 *
 * @param mailBody
 * @throws EmailException email about exception
 */
public static void sendHtmlEmail(MailBody mailBody) throws EmailException {

    HtmlEmail email = new HtmlEmail();
    email.setHostName(mailBody.getLoginServer());
    email.setAuthentication(mailBody.getLoginName(), mailBody.getLoginPassword());

    //receivers
    Map<String, String> receivers = mailBody.getReceivers();
    parseReceivers(email, receivers);
    //cc receivers
    receivers = mailBody.getCcReceivers();
    parseCCRecievers(email, receivers);
    //attatchments
    Map<String, EmailAttachment> attatchments = mailBody.getAttachments();
    parseAttatchments(email, attatchments);

    String aHtml = mailBody.getAHtml();
    if (StringUtils.isNotBlank(aHtml)) {
        email.setHtmlMsg(aHtml);
    }

    email.setFrom(mailBody.getSender(), mailBody.getSender_nick());
    email.setSubject(mailBody.getSubject());
    email.setMsg(mailBody.getBody());
    email.send();
}

From source file:org.yestech.notify.deliver.HtmlEmailDelivery.java

protected void sendMessage(INotification notification, IRecipient recipient) throws EmailException {
    // Create the email message
    HtmlEmail email = new HtmlEmail();
    enableAuthenticator(email);/*from w w  w  . j av a 2s .com*/
    email.setHostName(getEmailHost());
    ISender sender = notification.getSender();
    email.setFrom(sender.getEmailAddress(), sender.getDisplayName());
    if (StringUtils.isNotBlank(sender.getReplyAddress())) {
        email.addReplyTo(sender.getReplyAddress());
    }
    email.setSubject(notification.getMessage().getSubject());
    email.addTo(recipient.getEmailAddress(), recipient.getDisplayName());
    ITemplateLanguage template = notification.getTemplate();
    String appliedMessage = template.apply(notification.getMessage());
    email.setHtmlMsg(appliedMessage);
    email.send();
}

From source file:util.Log.java

public static void relatarExceptionEmail(String className, String exception, String logPath) {
    /*/*from   w w w.jav a 2  s . c  om*/
     * Para compreender melhor acesse esse site:
     * http://www.botecodigital.info/java/enviando-e-mail-em-java-com-api-
     * commons-email-da-apache/
     */
    HtmlEmail email = new HtmlEmail();
    email.setSSLOnConnect(true);
    email.setHostName("smtp.gmail.com");
    email.setSslSmtpPort("465");
    email.setAuthenticator(new DefaultAuthenticator("jjsoftwares10@gmail.com", "jean1420"));
    try {
        email.setFrom("jjsoftwares10@gmail.com", "Software da clinica");
        email.setSubject("Exceo ocorrida no app da clinica");
        StringBuilder msg = new StringBuilder();
        msg.append("<h1 style=\"text-align: center;\">Excecao Ocorrida</h1>");
        msg.append("<p><strong>Na Classe: " + className + " </strong></p>");
        msg.append("<p><strong>Data e Horario do ocorrido: "
                + LocalDateTime.now().format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:s"))
                + "</strong></p>");
        msg.append("<h2 style=\"text-align: center;\"><strong>Excecao</strong></h2>");
        msg.append("<p><span style=\"color: #ff0000;\">" + exception + "</span></p>");
        msg.append("<p><strong>Segue anexo com detalhes</strong></p>");
        /*Enviando o anexo com detalhes da exceo*/
        File arqLog = new File(logPath);
        if (arqLog.exists()) {
            EmailAttachment anexo = new EmailAttachment();
            anexo.setPath(logPath);
            anexo.setDisposition(EmailAttachment.ATTACHMENT);
            anexo.setName(arqLog.getName());
            email.attach(anexo);
        }
        /*enviando*/
        email.setHtmlMsg(msg.toString());
        email.addTo("jeandersonfju@gmail.com");
        email.addTo("jeff-assis@hotmail.com");
        email.send();
    } catch (EmailException e) {
        e.printStackTrace();

    }
}

From source file:velo.tools.EmailSender.java

public void addHtmlEmail(String fromAddress, Collection<String> recipient, String subject, String body)
        throws EmailException {
    HtmlEmail email = new HtmlEmail();
    email.setHostName(getHostName());//from w w w.  ja  v a2s. c  o m
    //email.addTo("jdoe@somewhere.org", "John Doe");
    //email.addTo(recipient,recipient);
    email.setFrom(fromAddress, fromAddress);
    email.setSubject(subject);
    email.setHtmlMsg(body);
    for (String currRec : recipient) {
        email.addTo(currRec);
    }

    email.setCharset("UTF-8");

    log.debug("Adding a new message with subject '" + subject + "', from: '" + fromAddress + "', to: '"
            + recipient + "' to the queue...");
    log.debug("Email server parameters - SMTP host: " + getHostName());
    emails.add(email);
}