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

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

Introduction

In this page you can find the example usage for org.apache.commons.mail Email 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.structr.common.MailHelper.java

private static void setup(final Email mail, final String to, final String toName, final String from,
        final String fromName, final String cc, final String bcc, final String bounce, final String subject)
        throws EmailException {

    // FIXME: this might be slow if the config file is read each time
    final Properties config = Services.getInstance().getCurrentConfig();
    final String smtpHost = config.getProperty(Services.SMTP_HOST, "localhost");
    final String smtpPort = config.getProperty(Services.SMTP_PORT, "25");
    final String smtpUser = config.getProperty(Services.SMTP_USER);
    final String smtpPassword = config.getProperty(Services.SMTP_PASSWORD);
    final String smtpUseTLS = config.getProperty(Services.SMTP_USE_TLS, "true");
    final String smtpRequireTLS = config.getProperty(Services.SMTP_REQUIRE_TLS, "false");

    mail.setCharset(charset);//w w w .  j a v a  2  s  . c om
    mail.setHostName(smtpHost);
    mail.setSmtpPort(Integer.parseInt(smtpPort));
    mail.setStartTLSEnabled(Boolean.parseBoolean(smtpUseTLS));
    mail.setStartTLSRequired(Boolean.parseBoolean(smtpRequireTLS));
    mail.setCharset(charset);
    mail.setHostName(smtpHost);
    mail.setSmtpPort(Integer.parseInt(smtpPort));

    if (StringUtils.isNotBlank(smtpUser) && StringUtils.isNotBlank(smtpPassword)) {
        mail.setAuthentication(smtpUser, smtpPassword);
    }

    mail.addTo(to, toName);
    mail.setFrom(from, fromName);

    if (StringUtils.isNotBlank(cc)) {
        mail.addCc(cc);
    }

    if (StringUtils.isNotBlank(bcc)) {
        mail.addBcc(bcc);
    }

    if (StringUtils.isNotBlank(bounce)) {
        mail.setBounceAddress(bounce);
    }

    mail.setSubject(subject);

}

From source file:org.xerela.server.birt.ReportJob.java

private void setupEmail(JobExecutionContext executionContext, String emailTo, String emailCc, Email email)
        throws AddressException, EmailException {
    InternetAddress[] toAddrs = InternetAddress.parse(emailTo);
    email.setTo(Arrays.asList(toAddrs));

    if (emailCc != null && emailCc.trim().length() > 0) {
        InternetAddress[] ccAddrs = InternetAddress.parse(emailCc);
        email.setCc(Arrays.asList(ccAddrs));
    }// w ww.ja v a 2s . co  m

    email.setCharset("utf-8"); //$NON-NLS-1$
    email.setHostName(System.getProperty(MAIL_HOST_PROP, "mail")); //$NON-NLS-1$
    String authUser = System.getProperty(MAIL_AUTH_USER_PROP);
    if (authUser != null) {
        email.setAuthentication(authUser, System.getProperty(MAIL_AUTH_PASSWORD_PROP));
    }
    email.setFrom(System.getProperty(MAIL_FROM_PROP), System.getProperty(MAIL_FROM_NAME_PROP));
    email.setSubject(
            Messages.bind(Messages.ReportJob_emailSubject, executionContext.getJobDetail().getFullName()));
    email.addHeader("X-Mailer", "Xerela Mailer"); //$NON-NLS-1$ //$NON-NLS-2$
    email.setDebug(Boolean.getBoolean("org.xerela.mail.debug")); //$NON-NLS-1$
}

From source file:ru.codeinside.adm.CheckExecutionDates.java

@TransactionAttribute(REQUIRES_NEW)
public Email checkDates(ProcessEngine processEngine) throws EmailException {
    String emailTo = get(API.EMAIL_TO);
    String receiverName = get(API.RECEIVER_NAME);
    String hostName = get(API.HOST);
    String port = get(API.PORT);/* w  w w.j  a  v a 2s .  co m*/
    String senderLogin = get(API.SENDER_LOGIN);
    String password = get(API.PASSWORD);
    String emailFrom = get(API.EMAIL_FROM);
    String senderName = get(API.SENDER_NAME);

    if (emailTo.isEmpty() || receiverName.isEmpty() || hostName.isEmpty() || port.isEmpty()
            || senderLogin.isEmpty() || password.isEmpty() || emailFrom.isEmpty() || senderName.isEmpty()) {
        return null;
    }

    List<TaskDates> overdueTasks = new ArrayList<TaskDates>();
    List<Bid> overdueBids = new ArrayList<Bid>();
    List<TaskDates> endingTasks = new ArrayList<TaskDates>();
    List<Bid> endingBids = new ArrayList<Bid>();
    List<TaskDates> inactionTasks = new ArrayList<TaskDates>();

    Date currentDate = check(overdueTasks, overdueBids, endingTasks, endingBids, inactionTasks);

    Email email = new SimpleEmail();
    email.setSubject("[siu.oep-penza.ru]  ?? ?  ?  "
            + new SimpleDateFormat("yyyy.MM.dd HH:mm").format(currentDate));

    StringBuilder msg = new StringBuilder();
    msg.append(" !\n"
            + "  ?? ?  ? ?? ? [siu.oep-penza.ru]  ")
            .append(new SimpleDateFormat("yyyy.MM.dd").format(currentDate))
            .append(", ??  :\n\n");

    int n = 1;
    n = addTaskList(
            ",    ? ? ??",
            n, processEngine, overdueTasks, msg);
    n = addBidList(
            "?,    ? ? ??",
            n, overdueBids, msg);
    n = addTaskList(
            ", ? ??  ??  ?",
            n, processEngine, endingTasks, msg);
    n = addBidList(
            "?, ? ??  ??  ?",
            n, endingBids, msg);
    n = addTaskList(",  ???  ?:", n,
            processEngine, inactionTasks, msg);

    msg.append(
            "  ,  ? ? ?    !\n"
                    + " ?  ?  ? "
                    + "      . 8(8412)23-11-25 (. 45, 46, 47)");

    if (n == 1) {
        return null;
    }

    email.setMsg(msg.toString());
    email.addTo(emailTo, receiverName);
    email.setHostName(hostName);
    email.setSmtpPort(Integer.parseInt(port));
    email.setTLS(AdminServiceProvider.getBoolProperty(API.TLS));
    email.setAuthentication(senderLogin, password);
    email.setFrom(emailFrom, senderName);
    email.setCharset("utf-8");

    return email;
}

From source file:ru.codeinside.adm.ui.AdminApp.java

private Panel createEmailDatesPanel() {
    VerticalLayout emailDates = new VerticalLayout();
    emailDates.setSpacing(true);/*from   w ww .  jav  a  2  s. co m*/
    emailDates.setMargin(true);
    emailDates.setSizeFull();
    Panel panel2 = new Panel(" ? ??", emailDates);
    panel2.setSizeFull();

    final TextField emailToField = new TextField("e-mail ?:");
    emailToField.setValue(get(API.EMAIL_TO));
    emailToField.setRequired(true);
    emailToField.setReadOnly(true);
    emailToField.addValidator(new EmailValidator("  e-mail ?"));

    final TextField receiverNameField = new TextField("? ?:");
    receiverNameField.setValue(get(API.RECEIVER_NAME));
    receiverNameField.setRequired(true);
    receiverNameField.setReadOnly(true);

    final TextField emailFromField = new TextField("e-mail ?:");
    emailFromField.setValue(get(API.EMAIL_FROM));
    emailFromField.setRequired(true);
    emailFromField.setReadOnly(true);
    emailFromField.addValidator(new EmailValidator("  e-mail ?"));

    final TextField senderLoginField = new TextField(" ?:");
    senderLoginField.setValue(get(API.SENDER_LOGIN));
    senderLoginField.setRequired(true);
    senderLoginField.setReadOnly(true);

    final TextField senderNameField = new TextField("? ?:");
    senderNameField.setValue(get(API.SENDER_NAME));
    senderNameField.setRequired(true);
    senderNameField.setReadOnly(true);

    final PasswordField passwordField = new PasswordField(":");
    passwordField.setValue(API.PASSWORD);
    passwordField.setRequired(true);
    passwordField.setReadOnly(true);

    final TextField hostField = new TextField("SMTP ?:");
    String host = get(API.HOST);
    hostField.setValue(host == null ? "" : host);
    hostField.setRequired(true);
    hostField.setReadOnly(true);

    final TextField portField = new TextField(":");
    String port = get(API.PORT);
    portField.setValue(port == null ? "" : port);
    portField.setRequired(true);
    portField.setReadOnly(true);
    portField.addValidator(new IntegerValidator(" "));

    final CheckBox tls = new CheckBox("? TLS",
            AdminServiceProvider.getBoolProperty(API.TLS));
    tls.setReadOnly(true);

    final Button save = new Button("");
    save.setVisible(false);
    final Button cancel = new Button("");
    cancel.setVisible(false);
    final Button change = new Button("");
    final Button check = new Button("");
    check.addListener(new Button.ClickListener() {
        @Override
        public void buttonClick(Button.ClickEvent event) {
            String emailTo = get(API.EMAIL_TO);
            String receiverName = get(API.RECEIVER_NAME);
            String hostName = get(API.HOST);
            String port = get(API.PORT);
            String senderLogin = get(API.SENDER_LOGIN);
            String password = get(API.PASSWORD);
            String emailFrom = get(API.EMAIL_FROM);
            String senderName = get(API.SENDER_NAME);
            if (emailTo.isEmpty() || receiverName.isEmpty() || hostName.isEmpty() || port.isEmpty()
                    || senderLogin.isEmpty() || password.isEmpty() || emailFrom.isEmpty()
                    || senderName.isEmpty()) {
                check.getWindow().showNotification("? ? ");
                return;
            }
            Email email = new SimpleEmail();
            try {
                email.setSubject("? ?");
                email.setMsg("? ?");
                email.addTo(emailTo, receiverName);
                email.setHostName(hostName);
                email.setSmtpPort(Integer.parseInt(port));
                email.setTLS(AdminServiceProvider.getBoolProperty(API.TLS));
                email.setAuthentication(senderLogin, password);
                email.setFrom(emailFrom, senderName);
                email.setCharset("utf-8");
                email.send();
            } catch (EmailException e) {
                check.getWindow().showNotification(e.getMessage());
                return;
            }
            check.getWindow().showNotification("? ? ");
        }
    });
    change.addListener(new Button.ClickListener() {
        @Override
        public void buttonClick(Button.ClickEvent event) {
            emailToField.setReadOnly(false);
            receiverNameField.setReadOnly(false);
            emailFromField.setReadOnly(false);
            senderLoginField.setReadOnly(false);
            senderNameField.setReadOnly(false);
            passwordField.setReadOnly(false);
            hostField.setReadOnly(false);
            portField.setReadOnly(false);
            tls.setReadOnly(false);

            change.setVisible(false);
            check.setVisible(false);
            save.setVisible(true);
            cancel.setVisible(true);
        }
    });
    save.addListener(new Button.ClickListener() {
        @Override
        public void buttonClick(Button.ClickEvent event) {
            if (StringUtils.isEmpty((String) emailToField.getValue())
                    || StringUtils.isEmpty((String) receiverNameField.getValue())
                    || StringUtils.isEmpty((String) emailFromField.getValue())
                    || StringUtils.isEmpty((String) senderNameField.getValue())
                    || StringUtils.isEmpty((String) senderLoginField.getValue())
                    || StringUtils.isEmpty((String) passwordField.getValue())
                    || StringUtils.isEmpty((String) hostField.getValue()) || portField.getValue() == null) {
                emailToField.getWindow().showNotification(" ?",
                        Window.Notification.TYPE_HUMANIZED_MESSAGE);
                return;
            }
            boolean errors = false;
            try {
                emailToField.validate();
            } catch (Validator.InvalidValueException ignore) {
                errors = true;
            }
            try {
                emailFromField.validate();
            } catch (Validator.InvalidValueException ignore) {
                errors = true;
            }
            try {
                portField.validate();
            } catch (Validator.InvalidValueException ignore) {
                errors = true;
            }
            if (errors) {
                return;
            }
            set(API.EMAIL_TO, emailToField.getValue());
            set(API.RECEIVER_NAME, receiverNameField.getValue());
            set(API.EMAIL_FROM, emailFromField.getValue());
            set(API.SENDER_LOGIN, senderLoginField.getValue());
            set(API.SENDER_NAME, senderNameField.getValue());
            set(API.PASSWORD, passwordField.getValue());
            set(API.HOST, hostField.getValue());
            set(API.PORT, portField.getValue());
            set(API.TLS, tls.getValue());

            emailToField.setReadOnly(true);
            receiverNameField.setReadOnly(true);
            emailFromField.setReadOnly(true);
            senderLoginField.setReadOnly(true);
            senderNameField.setReadOnly(true);
            passwordField.setReadOnly(true);
            hostField.setReadOnly(true);
            portField.setReadOnly(true);
            tls.setReadOnly(true);

            save.setVisible(false);
            cancel.setVisible(false);
            change.setVisible(true);
            check.setVisible(true);
            emailToField.getWindow().showNotification("?? ?",
                    Window.Notification.TYPE_HUMANIZED_MESSAGE);
        }
    });
    cancel.addListener(new Button.ClickListener() {
        @Override
        public void buttonClick(Button.ClickEvent event) {
            emailToField.setValue(get(API.EMAIL_TO));
            receiverNameField.setValue(get(API.RECEIVER_NAME));
            emailFromField.setValue(get(API.EMAIL_FROM));
            senderLoginField.setValue(get(API.SENDER_LOGIN));
            senderNameField.setValue(get(API.SENDER_NAME));
            passwordField.setValue(get(API.PASSWORD));
            hostField.setValue(get(API.HOST));
            portField.setValue(get(API.PORT));
            tls.setValue(AdminServiceProvider.getBoolProperty(API.TLS));

            emailToField.setReadOnly(true);
            receiverNameField.setReadOnly(true);
            emailFromField.setReadOnly(true);
            senderLoginField.setReadOnly(true);
            senderNameField.setReadOnly(true);
            passwordField.setReadOnly(true);
            hostField.setReadOnly(true);
            portField.setReadOnly(true);
            tls.setReadOnly(true);

            save.setVisible(false);
            cancel.setVisible(false);
            change.setVisible(true);
            check.setVisible(true);
        }
    });

    FormLayout fields1 = new FormLayout();
    fields1.setSizeFull();
    fields1.addComponent(senderLoginField);
    fields1.addComponent(passwordField);
    fields1.addComponent(hostField);
    fields1.addComponent(portField);
    fields1.addComponent(tls);

    FormLayout fields2 = new FormLayout();
    fields2.setSizeFull();
    fields2.addComponent(emailToField);
    fields2.addComponent(receiverNameField);
    fields2.addComponent(emailFromField);
    fields2.addComponent(senderNameField);

    HorizontalLayout fields = new HorizontalLayout();
    fields.setSpacing(true);
    fields.setSizeFull();
    fields.addComponent(fields1);
    fields.addComponent(fields2);

    HorizontalLayout buttons = new HorizontalLayout();
    buttons.setSpacing(true);
    buttons.addComponent(change);
    buttons.addComponent(save);
    buttons.addComponent(cancel);
    buttons.addComponent(check);

    Label label = new Label("?? ");
    label.addStyleName(Reindeer.LABEL_H2);
    emailDates.addComponent(label);
    emailDates.addComponent(fields);
    emailDates.addComponent(buttons);
    emailDates.setExpandRatio(fields, 1f);
    return panel2;
}