Example usage for javax.mail.internet InternetAddress InternetAddress

List of usage examples for javax.mail.internet InternetAddress InternetAddress

Introduction

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

Prototype

public InternetAddress(String address) throws AddressException 

Source Link

Document

Constructor.

Usage

From source file:com.hygenics.parser.Send.java

public void run() {

    try {/*from  w w  w  .  j  av a2  s.c  om*/
        log.info("Creating Message");
        MimeMessage message = mailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(message, true);

        InternetAddress addr = new InternetAddress(fromEmails);

        helper.setFrom(addr);

        for (String email : emails) {
            helper.addTo(new InternetAddress(email));
        }

        helper.setSubject(subject);
        helper.setText(body);

        if (fpath != null) {
            log.info("Attaching File");
            File f = new File(fpath);

            if (f.exists()) {
                helper.addAttachment(fpath, f);
            }
        }

        log.info("Sending Email");
        mailSender.send(message);

    } catch (AddressException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:org.jasig.schedassist.impl.reminder.DefaultEmailAddressValidatorImpl.java

@Override
public boolean canSendToEmailAddress(ICalendarAccount account) {
    if (account == null) {
        return false;
    }/* w ww . j a v  a  2s.  co  m*/

    final String address = account.getEmailAddress();
    if (StringUtils.isBlank(address)) {
        return false;
    }

    try {
        InternetAddress emailAddr = new InternetAddress(address);
        emailAddr.validate();
        String[] parts = address.split("@");
        if (parts.length == 2) {
            if (!restrictedDomains.contains(parts[1])) {
                return true;
            }
        }
    } catch (AddressException ex) {
        log.info("email address for " + account + " failed to valdidate", ex);
        return false;
    }

    return false;
}

From source file:de.tuttas.servlets.MailSender.java

private void transmitMail(MailObject mo) throws MessagingException {

    // creates a new session with an authenticator
    Authenticator auth = new Authenticator() {
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(Config.getInstance().user, Config.getInstance().pass);
        }/*from   www.  ja  v  a  2s.  c  o  m*/
    };
    Session session = Session.getInstance(properties, auth);
    // creates a new e-mail message
    MimeMessage msg = new MimeMessage(session);

    msg.setFrom(new InternetAddress(mo.getFrom()));
    InternetAddress[] toAddresses = mo.getRecipient();
    msg.setRecipients(Message.RecipientType.TO, toAddresses);
    InternetAddress[] bccAdresses = mo.getBcc();
    InternetAddress[] ccAdresses = mo.getCC();

    if (bccAdresses[0] != null)
        msg.setRecipients(Message.RecipientType.BCC, bccAdresses);
    if (ccAdresses[0] != null)
        msg.setRecipients(Message.RecipientType.CC, ccAdresses);
    msg.setSubject(mo.getSubject(), "UTF-8");

    msg.setSentDate(new Date());
    msg.setContent(mo.getContent(), "text/plain; charset=UTF-8");
    // sends the e-mail
    // TODO Kommentar entfernen
    Transport.send(msg);
}

From source file:mailhost.StartApp.java

public void sendEmail(String to, String subject, String body)
        throws UnsupportedEncodingException, MessagingException {

    System.out.println(String.format("Sending notification email recipients " + "to  " + to + " subject  "
            + subject + "host " + mailhost));

    if (StringUtils.isBlank(to))
        throw new IllegalArgumentException("The email request should have at least one recipient");

    Properties properties = new Properties();
    properties.setProperty("mail.smtp.host", mailhost);

    Session session = Session.getDefaultInstance(properties);
    Address[] a = new InternetAddress[1];
    a[0] = new InternetAddress("test@matson.com");

    MimeMessage message = new MimeMessage(session);

    message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
    message.addFrom(a);// w w w  . j a  va  2 s  .c om

    message.setSubject(subject);
    message.setContent(body, "text/html; charset=utf-8");
    //message.setText(body);

    // Send message
    Transport.send(message);
    System.out.println("Email sent.");
}

From source file:com.cloudbees.demo.beesshop.service.MailService.java

public void sendProductEmail(Product product, String recipient, String cocktailPageUrl)
        throws MessagingException {

    Message msg = new MimeMessage(mailSession);

    msg.setFrom(fromAddress);/* ww w  .j a v a  2s. co  m*/
    msg.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));

    msg.setSubject("[BeesShop] Check this product: " + product.getName());
    String message = product.getName() + "\n" //
            + "--------------------\n" //
            + "\n" //
            + Strings.nullToEmpty(product.getDescription()) + "\n" //
            + "\n" //
            + cocktailPageUrl;
    msg.setContent(message, "text/plain");

    mailSession.getTransport().send(msg);
    auditLogger.info("Sent to {} product '{}'", recipient, product.getName());
    sentEmailCounter.incrementAndGet();
}

From source file:EmailBean.java

public void sendMessage() throws Exception {

    Properties properties = System.getProperties();

    //populate the 'Properties' object with the mail
    //server address, so that the default 'Session'
    //instance can use it.
    properties.put("mail.smtp.host", smtpHost);

    Session session = Session.getDefaultInstance(properties);

    Message mailMsg = new MimeMessage(session);//a new email message

    InternetAddress[] addresses = null;/*from  w  w w . j  a v  a 2  s.  co m*/

    try {

        if (to != null) {

            //throws 'AddressException' if the 'to' email address
            //violates RFC822 syntax
            addresses = InternetAddress.parse(to, false);

            mailMsg.setRecipients(Message.RecipientType.TO, addresses);

        } else {

            throw new MessagingException("The mail message requires a 'To' address.");

        }

        if (from != null) {

            mailMsg.setFrom(new InternetAddress(from));

        } else {

            throw new MessagingException("The mail message requires a valid 'From' address.");

        }

        if (subject != null)
            mailMsg.setSubject(subject);

        if (content != null)
            mailMsg.setText(content);

        //Finally, send the mail message; throws a 'SendFailedException'
        //if any of the message's recipients have an invalid address
        Transport.send(mailMsg);

    } catch (Exception exc) {

        throw exc;

    }

}

From source file:ru.org.linux.user.EditRegisterRequestValidator.java

@Override
public void validate(Object o, Errors errors) {
    EditRegisterRequest form = (EditRegisterRequest) o;
    if (!Strings.isNullOrEmpty(form.getTown())) {
        if (StringUtil.escapeHtml(form.getTown()).length() > TOWN_LENGTH) {
            errors.rejectValue("town", null,
                    "    (? " + TOWN_LENGTH
                            + " ?)");
        }/* w w w.j a va  2s. c o  m*/
    }

    if (!Strings.isNullOrEmpty(form.getUrl()) && !URLUtil.isUrl(form.getUrl())) {
        errors.rejectValue("url", null, "? URL");
    }

    if (form.getPassword2() != null && form.getPassword() != null
            && !form.getPassword().equals(form.getPassword2())) {
        errors.reject(null, "   ?");
    }

    if (!Strings.isNullOrEmpty(form.getPassword()) && form.getPassword().length() < MIN_PASSWORD_LEN) {
        errors.reject(null, "?  , ? : "
                + MIN_PASSWORD_LEN);
    }

    if (Strings.isNullOrEmpty(form.getEmail())) {
        errors.rejectValue("email", null, "?  e-mail");
    } else {
        try {
            InternetAddress mail = new InternetAddress(form.getEmail());
            checkEmail(mail, errors);
        } catch (AddressException e) {
            errors.rejectValue("email", null, "? e-mail: " + e.getMessage());
        }
    }

}

From source file:com.wso2telco.workflow.notification.EmailService.java

public void sendEmail(final String emailAddress, final String subject, final String content) {

    new Thread() {
        @Override/*from   ww  w.j  a v  a2 s .c om*/
        public void run() {
            Map<String, String> workflowProperties = WorkflowProperties.loadWorkflowPropertiesFromXML();
            String emailHost = workflowProperties.get(Constants.KEY_WORKFLOW_EMAIL_NOTIFICATION_HOST);
            String fromEmailAddress = workflowProperties
                    .get(Constants.KEY_WORKFLOW_EMAIL_NOTIFICATION_FROM_ADDRESS);
            String fromEmailPassword = workflowProperties
                    .get(Constants.KEY_WORKFLOW_EMAIL_NOTIFICATION_FROM_PASSWORD);

            Properties props = System.getProperties();
            props.put("mail.smtp.host", emailHost);
            props.put("mail.smtp.user", fromEmailAddress);
            props.put("mail.smtp.password", fromEmailPassword);
            props.put("mail.smtp.port", "587");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.auth", "true");

            try {
                Session session = Session.getDefaultInstance(props, null);
                InternetAddress toAddress = new InternetAddress(emailAddress);

                MimeMessage message = new MimeMessage(session);
                message.setFrom(new InternetAddress(fromEmailAddress));
                message.addRecipient(Message.RecipientType.TO, toAddress);
                message.setSubject(subject);
                message.setContent(content, "text/html; charset=UTF-8");

                Transport transport = session.getTransport("smtp");
                transport.connect(emailHost, fromEmailAddress, fromEmailPassword);
                transport.sendMessage(message, message.getAllRecipients());
                transport.close();

            } catch (Exception e) {
                log.error("Email sending failed. ", e);
            }
        }
    }.start();
}

From source file:gov.nih.nci.cabig.caaers.web.admin.DiagnosticsController.java

private boolean testSmtp(DiagnosticsCommand diagnosticsCommand) {
    boolean testResult = false;
    try {/*  w  w  w . j a  v  a2  s. c om*/
        MimeMessage message = caaersJavaMailSender.createMimeMessage();
        message.setSubject("Test mail from caAERS Diagnostics");
        message.setFrom(new InternetAddress("caaers.app@gmail.com"));

        // use the true flag to indicate you need a multipart message
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        String fromEmail = configuration.get(Configuration.SYSTEM_FROM_EMAIL);
        if (fromEmail == null)
            fromEmail = "admin@semanticbits.com";
        helper.setTo(fromEmail);
        caaersJavaMailSender.send(message);
        testResult = true;
    } catch (Exception e) {
        //log.error(" Error in sending email , please check the confiuration " + e);
        diagnosticsCommand.setSmtpException(e);
        testResult = false;
    }
    return testResult;
}

From source file:de.fzi.ALERT.actor.ActionActuator.MailService.java

public void sendEmail(Authenticator auth, String address, String subject, String content) {
    try {//  w w w .  j ava 2  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.smtp.starttls.enable", "true");

        Session session = Session.getDefaultInstance(props, auth);
        // -- Create a new message --
        Message msg = new MimeMessage(session);

        // -- Set the FROM and TO fields --
        msg.setFrom(new InternetAddress(username));
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(address, false));

        // -- Set the subject and body text --
        msg.setSubject(subject);
        msg.setText(content);

        // -- Set some other header information --
        msg.setHeader("X-Mailer", "LOTONtechEmail");
        msg.setSentDate(new Date());

        // -- Send the message --
        Transport.send(msg);

        System.out.println("An announce Mail has been send to " + address);
    } catch (AddressException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}