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.cloudbees.demo.beesshop.service.MailService.java

@Required
public void setFromAddress(String fromAddress) throws AddressException {
    this.fromAddress = new InternetAddress(fromAddress);
}

From source file:org.openmhealth.dsu.service.EndUserServiceImpl.java

@Override
@Transactional(readOnly = true)//  w  ww.  j  ava2  s . c o m
public boolean doesEmailExist(String emailAddress) {
    // Iterate through the existing users and check for duplicate e-mail addresses
    Iterator usrItr = endUserRepository.findAll().iterator();
    boolean res = false;
    InternetAddress address = null;

    try {
        address = new InternetAddress(emailAddress);
    } catch (AddressException e) {
        res = true;
    }

    while (usrItr.hasNext() && !res) {
        EndUser user = (EndUser) usrItr.next();
        res = address.equals(user.getEmailAddress());
    }

    return res;
}

From source file:com.spartasystems.holdmail.util.TestMailClient.java

public void sendEmail(String fromEmail, String toEmail, String subject, String textBody, String htmlBody) {
    try {/*  www  . j ava2s . com*/
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(fromEmail));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
        message.setSubject(subject);

        // Set the message
        createMultiMimePart(message, textBody, htmlBody);

        Transport.send(message);
    } catch (MessagingException e) {
        throw new HoldMailException("Failed to send email : " + e.getMessage(), e);
    }
}

From source file:com.email.SendEmailCalInvite.java

/**
 * Sends email based off of the section it comes from. This creates a
 * calendar invite object that is interactive by Outlook.
 *
 * @param eml EmailOutInviteModel//w  ww .  j  a  v a2  s.com
 */
public static void sendCalendarInvite(EmailOutInvitesModel eml) {
    SystemEmailModel account = null;

    //Get Account
    for (SystemEmailModel acc : Global.getSystemEmailParams()) {
        if (acc.getSection().equals(eml.getSection())) {
            account = acc;
            break;
        }
    }
    if (account != null) {
        //Get parts
        String FromAddress = account.getEmailAddress();
        String[] TOAddressess = ((eml.getToAddress() == null) ? "".split(";") : eml.getToAddress().split(";"));
        String[] CCAddressess = ((eml.getCcAddress() == null) ? "".split(";") : eml.getCcAddress().split(";"));
        String emailSubject = "";
        BodyPart emailBody = body(eml);
        BodyPart inviteBody = null;

        if (eml.getHearingRoomAbv() == null) {
            emailSubject = eml.getEmailSubject() == null
                    ? (eml.getEmailBody() == null ? eml.getCaseNumber() : eml.getEmailBody())
                    : eml.getEmailSubject();
            inviteBody = responseDueCalObject(eml, account);
        } else {
            emailSubject = eml.getEmailSubject() == null ? Subject(eml) : eml.getEmailSubject();
            inviteBody = inviteCalObject(eml, account, emailSubject);
        }

        //Set Email Parts
        Authenticator auth = EmailAuthenticator.setEmailAuthenticator(account);
        Properties properties = EmailProperties.setEmailOutProperties(account);
        Session session = Session.getInstance(properties, auth);
        MimeMessage smessage = new MimeMessage(session);
        Multipart multipart = new MimeMultipart("alternative");
        try {
            smessage.addFrom(new InternetAddress[] { new InternetAddress(FromAddress) });
            for (String To : TOAddressess) {
                if (EmailValidator.getInstance().isValid(To)) {
                    smessage.addRecipient(Message.RecipientType.TO, new InternetAddress(To));
                }
            }
            for (String Cc : CCAddressess) {
                if (EmailValidator.getInstance().isValid(Cc)) {
                    smessage.addRecipient(Message.RecipientType.CC, new InternetAddress(Cc));
                }
            }
            smessage.setSubject(emailSubject);
            multipart.addBodyPart(emailBody);
            multipart.addBodyPart(inviteBody);
            smessage.setContent(multipart);
            if (Global.isOkToSendEmail()) {
                Transport.send(smessage);
            } else {
                Audit.addAuditEntry("Cal Invite Not Actually Sent: " + eml.getId() + " - " + emailSubject);
            }
            EmailOutInvites.deleteEmailEntry(eml.getId());
        } catch (AddressException ex) {
            ExceptionHandler.Handle(ex);
        } catch (MessagingException ex) {
            ExceptionHandler.Handle(ex);
        }
    }
}

From source file:com.silverwrist.dynamo.util.IDUtils.java

public static boolean isValidEmailAddress(String addr) {
    try { // take advantage of the JavaMail address parser
        InternetAddress tmp = new InternetAddress(addr);

    } // end try//ww  w .  j a v  a 2s  .co  m
    catch (AddressException e) { // if we get an AddressException, it's not valid, is it?
        return false;

    } // end catch

    return true; // if get here, it's valid

}

From source file:com.mycompany.login.mb.EmailBean.java

public void envia() throws AddressException, MessagingException {
    Session session = Session.getInstance(this.propriedades, this.authentication);
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress("anderson.freitas@lifemed.com.br"));
    message.setRecipients(Message.RecipientType.TO, "anderson.freitas@lifemed.com.br");
    message.setSentDate(new Date());
    message.setSubject("Teste envio jsf");
    message.setContent("Sua solicitao foi aprovada: OS n" + this.os, "text/plain");

    Transport.send(message);/*from ww w.j a v  a2s .co m*/

}

From source file:mail.MailService.java

/**
 * Erstellt eine MIME-Mail./*ww  w  .  java2 s.c om*/
 * @param email
 * @throws MessagingException
 * @throws IOException
 */
public String createMail1(Mail email, Config config) throws MessagingException, IOException {

    Properties props = new Properties();
    props.put("mail.smtp.host", "mail.java-tutor.com");
    Session session = Session.getDefaultInstance(props);

    Message msg = new MimeMessage(session);
    //      msg.setHeader("MIME-Version" , "1.0"); 
    //      msg.setHeader("Content-Type" , "text/plain"); 

    // Absender
    InternetAddress addressFrom = new InternetAddress(email.getAbsender());
    msg.setFrom(addressFrom);

    // Empfnger
    InternetAddress addressTo = new InternetAddress(email.getEmpfaenger());
    msg.setRecipient(Message.RecipientType.TO, addressTo);

    msg.setSubject(email.getBetreff());
    msg.setSentDate(email.getAbsendeDatum());
    String txt = Utils.toString(email.getText());

    msg.setText(txt);
    msg.saveChanges();

    // Mail in Ausgabestrom schreiben
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    try {
        msg.writeTo(bOut);
    } catch (IOException e) {
        if (config.isTest())
            System.out.println("Fehler beim Schreiben der Mail in Schritt 1");
        throw e;
    }
    return removeMessageId(bOut.toString(Charset.defaultCharset().name()));
}

From source file:org.trpr.platform.integration.impl.email.SpringMailSender.java

/**
 * Interface method implementation. Sends an email using the specified values and the configured mail sender.
 * @see org.trpr.platform.integration.spi.email.MailSender#sendMail(java.lang.String, java.lang.String[], java.lang.String, java.net.URL)
 *///from w w  w . ja v  a  2  s.c o  m
public void sendMail(final String senderAddress, final String subject, final String[] recipients,
        final String message, final URL attachmentURL) {
    MimeMessagePreparator preparator = new MimeMessagePreparator() {
        public void prepare(MimeMessage mimeMessage) throws Exception {
            InternetAddress[] recipientAddresses = new InternetAddress[recipients.length];
            for (int i = 0; i < recipientAddresses.length; i++) {
                recipientAddresses[i] = new InternetAddress(recipients[i]);
            }
            mimeMessage.setRecipients(Message.RecipientType.TO, recipientAddresses);
            mimeMessage.setFrom(new InternetAddress(senderAddress));
            mimeMessage.setSubject(subject);
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); // multi-part flag is set to true for accommodating attachments
            if (attachmentURL != null) {
                helper.addAttachment(attachmentURL.getFile(), new FileSystemResource(attachmentURL.toString()));
            }
            helper.setText(message);
        }
    };
    this.mailSender.send(preparator);
}

From source file:com.trivago.mail.pigeon.mail.MailFacade.java

public void sendMail(MailTransport mailTransport) {
    log.debug("Mail delivery started");
    File propertyfile = ((PropertiesConfiguration) Settings.create().getConfiguration()).getFile();
    Properties config = new Properties();

    try {/*from w  w  w.  jav a2  s.  c o  m*/
        config.load(new FileReader(propertyfile));
    } catch (IOException e) {
        log.error(e);
    }

    Session session = Session.getDefaultInstance(config);

    log.debug("Received session");

    MimeMessage message = new MimeMessage(session);

    String to = mailTransport.getTo();
    String from = mailTransport.getFrom();
    String replyTo = mailTransport.getReplyTo();
    String subject = mailTransport.getSubject();
    String html = mailTransport.getHtml();
    String text = mailTransport.getText();

    try {
        Address fromAdr = new InternetAddress(from);
        Address toAdr = new InternetAddress(to);
        Address rplyAdr = new InternetAddress(replyTo);

        message.setSubject(subject);
        message.setFrom(fromAdr);
        message.setRecipient(Message.RecipientType.TO, toAdr);
        message.setReplyTo(new Address[] { rplyAdr });
        message.setSender(fromAdr);
        message.addHeader("Return-path", replyTo);
        message.addHeader("X-TRV-MID", mailTransport.getmId());
        message.addHeader("X-TRV-UID", mailTransport.getuId());

        // Content
        MimeBodyPart messageTextPart = new MimeBodyPart();
        messageTextPart.setText(text);
        messageTextPart.setContent(html, "text/html");

        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageTextPart);

        // Put parts in message
        message.setContent(multipart);

        log.debug("Dispatching message");
        Transport.send(message);
        log.debug("Mail delivery ended");
    } catch (MessagingException e) {
        log.error(e);
    }

}

From source file:org.ktunaxa.referral.server.command.test.TestEmailCommand.java

@Override
public void execute(TestEmailRequest request, TestEmailResponse response) throws Exception {
    final String from = "rms@ktunaxa.org";
    final String to = request.getTo();
    if (null == to) {
        throw new GeomajasException(ExceptionCode.PARAMETER_MISSING, "to");
    }/*from w w  w . jav a2  s .  c o  m*/
    MimeMessagePreparator preparator = new MimeMessagePreparator() {

        public void prepare(MimeMessage mimeMessage) throws Exception {

            mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            mimeMessage.setFrom(new InternetAddress(from));
            mimeMessage.setSubject("Test");
            mimeMessage.setText("Testing RMS");
        }
    };
    mailSender.send(preparator);
}