Search in sources :

Example 31 with AddressException

use of javax.mail.internet.AddressException in project ORCID-Source by ORCID.

the class BaseController method validateEmailAddress.

/**
     * Validates if the provided string matches an email address pattern.
     * 
     * @param email
     *            The string to evaluate
     * @return true if the provided string matches an email address pattern,
     *         false otherwise.
     */
protected boolean validateEmailAddress(String email) {
    if (StringUtils.isNotBlank(email)) {
        try {
            InternetAddress addr = new InternetAddress(email);
            addr.validate();
            return true;
        } catch (AddressException ex) {
        }
    }
    return false;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) AddressException(javax.mail.internet.AddressException)

Example 32 with AddressException

use of javax.mail.internet.AddressException in project ORCID-Source by ORCID.

the class CustomEmailController method validateSender.

@RequestMapping(value = "/validate-sender.json", method = RequestMethod.POST)
@ResponseBody
public CustomEmailForm validateSender(@RequestBody CustomEmailForm customEmailForm) {
    customEmailForm.getSender().setErrors(new ArrayList<String>());
    if (!PojoUtil.isEmpty(customEmailForm.getSender())) {
        try {
            String sender = customEmailForm.getSender().getValue();
            InternetAddress addr = new InternetAddress(sender);
            addr.validate();
        } catch (AddressException ex) {
            customEmailForm.getSender().getErrors().add(getMessage("custom_email.sender.invalid"));
        }
    }
    return customEmailForm;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) AddressException(javax.mail.internet.AddressException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 33 with AddressException

use of javax.mail.internet.AddressException in project opennms by OpenNMS.

the class JavaMailer method buildMessage.

/**
     * Build a complete message ready for sending.
     *
     * @return completed message, ready to be passed to Transport.sendMessage
     * @throws org.opennms.javamail.JavaMailerException if any of the underlying operations fail
     */
public Message buildMessage() throws JavaMailerException {
    try {
        checkEnvelopeAndContents();
        MimeMessage message = initializeMessage();
        // The next line has been commented, because it prevents the usage of internationalized characters and makes the email unreadable.
        // String encodedText = MimeUtility.encodeText(getMessageText(), m_charSet, m_encoding);
        String encodedText = getMessageText();
        if ((getFileName() == null) && (getInputStream() == null)) {
            message.setContent(encodedText, m_contentType + "; charset=" + m_charSet);
        } else if (getFileName() == null) {
            BodyPart streamBodyPart = new MimeBodyPart();
            streamBodyPart.setDataHandler(new DataHandler(new InputStreamDataSource(m_inputStreamName, m_inputStreamContentType, m_inputStream)));
            streamBodyPart.setFileName(m_inputStreamName);
            streamBodyPart.setHeader("Content-Transfer-Encoding", "base64");
            streamBodyPart.setDisposition(Part.ATTACHMENT);
            MimeMultipart mp = new MimeMultipart();
            mp.addBodyPart(streamBodyPart);
            message.setContent(mp);
        } else {
            BodyPart bp = new MimeBodyPart();
            bp.setContent(encodedText, m_contentType + "; charset=" + m_charSet);
            MimeMultipart mp = new MimeMultipart();
            mp.addBodyPart(bp);
            mp.addBodyPart(createFileAttachment(new File(getFileName())));
            message.setContent(mp);
        }
        message.setHeader("X-Mailer", getMailer());
        message.setSentDate(new Date());
        message.saveChanges();
        return message;
    } catch (AddressException e) {
        LOG.error("Java Mailer Addressing exception: ", e);
        throw new JavaMailerException("Java Mailer Addressing exception: ", e);
    } catch (MessagingException e) {
        LOG.error("Java Mailer messaging exception: ", e);
        throw new JavaMailerException("Java Mailer messaging exception: ", e);
    //        } catch (UnsupportedEncodingException e) {
    //            log().error("Java Mailer messaging exception: ", e);
    //            throw new JavaMailerException("Java Mailer encoding exception: ", e);
    }
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) DataHandler(javax.activation.DataHandler) MimeBodyPart(javax.mail.internet.MimeBodyPart) File(java.io.File) Date(java.util.Date)

Example 34 with AddressException

use of javax.mail.internet.AddressException in project nhin-d by DirectProject.

the class TrustChainValidator method resolveIssuers.

protected void resolveIssuers(X509Certificate certificate, /*in-out*/
Collection<X509Certificate> issuers, int chainLength, Collection<X509Certificate> anchors) {
    X500Principal issuerPrin = certificate.getIssuerX500Principal();
    if (issuerPrin.equals(certificate.getSubjectX500Principal())) {
        // no intermediate between me, myself, and I
        return;
    }
    // look in the issuer list and see if the certificate issuer already exists in the list
    for (X509Certificate issuer : issuers) {
        if (issuerPrin.equals(issuer.getSubjectX500Principal()))
            // already found the certificate issuer... done
            return;
    }
    if (chainLength >= maxIssuerChainLength) {
        // bail out with what we have now
        return;
    }
    // first check to see there is an AIA extension with one ore more caIssuer entries and attempt to resolve the
    // intermediate via the URL
    final Collection<X509Certificate> issuerCerts = getIntermediateCertsByAIA(certificate);
    // of using resolvers
    if (issuerCerts.isEmpty()) {
        final String address = this.getIssuerAddress(certificate);
        if (address == null || address.isEmpty())
            // not much we can do about this... the resolver interface only knows how to work with addresses
            return;
        // multiple resolvers
        for (CertificateResolver publicResolver : certResolvers) {
            Collection<X509Certificate> holdCerts = null;
            try {
                holdCerts = publicResolver.getCertificates(new InternetAddress(address));
            } catch (AddressException e) {
                continue;
            } catch (Exception e) {
            /* no-op*/
            }
            if (holdCerts != null && holdCerts.size() > 0)
                issuerCerts.addAll(holdCerts);
        }
    }
    if (issuerCerts.size() == 0)
        // no intermediates.. just return
        return;
    boolean issuerFoundInAnchors = false;
    Collection<X509Certificate> searchForParentIssuers = new ArrayList<X509Certificate>();
    for (X509Certificate issuerCert : issuerCerts) {
        if (issuerCert.getSubjectX500Principal().equals(issuerPrin) && !isIssuerInCollection(issuers, issuerCert) && !isIssuerInAnchors(anchors, issuerCert)) /* if we hit an anchor then stop */
        {
            searchForParentIssuers.add(issuerCert);
        } else if (isIssuerInAnchors(anchors, issuerCert)) {
            issuerFoundInAnchors = true;
            break;
        }
    }
    // the go up the next level in the chain
    if (!issuerFoundInAnchors) {
        for (X509Certificate issuerCert : searchForParentIssuers) {
            issuers.add(issuerCert);
            // see if this issuer also has intermediate certs
            resolveIssuers(issuerCert, issuers, chainLength + 1, anchors);
        }
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) AddressException(javax.mail.internet.AddressException) ArrayList(java.util.ArrayList) X500Principal(javax.security.auth.x500.X500Principal) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) CertificateResolver(org.nhindirect.stagent.cert.CertificateResolver) X509Certificate(java.security.cert.X509Certificate) CertificateParsingException(java.security.cert.CertificateParsingException) AddressException(javax.mail.internet.AddressException) PolicyProcessException(org.nhindirect.policy.PolicyProcessException) NHINDException(org.nhindirect.stagent.NHINDException)

Example 35 with AddressException

use of javax.mail.internet.AddressException in project nhin-d by DirectProject.

the class MessageServiceImplService method sendMessage.

@Override
public /**
     * Converts an incoming WS request into an email message and sends it to the configured 
     * email server
     */
SendResponseType sendMessage(EmailType body) {
    if (log.isDebugEnabled())
        log.debug("Enter");
    SendResponseType response = new SendResponseType();
    checkAuth(response);
    if (response.getError() == null) {
        log.info("Auth success");
        Multipart mailBody;
        MimeBodyPart mainBody;
        MimeBodyPart mimeAttach;
        String fromaddress = body.getHead().getFrom().getAddress();
        log.info("Got FROM address");
        try {
            InternetAddress addressFrom;
            addressFrom = new InternetAddress(fromaddress.toString());
            if (log.isDebugEnabled())
                log.debug("Sender: " + addressFrom);
            InternetAddress[] addressTo = new InternetAddress[1];
            int i = 0;
            for (AddressType recipient : body.getHead().getTo()) {
                addressTo[i] = new InternetAddress(recipient.getAddress());
                if (log.isDebugEnabled())
                    log.debug("Recipient: " + addressTo[i]);
                i++;
            }
            Session session = Session.getInstance(smtpProps, new SMTPAuthenticator());
            // Build message object
            MimeMessage mimeMsg = new MimeMessage(session);
            mimeMsg.setFrom(addressFrom);
            mimeMsg.setRecipients(Message.RecipientType.TO, addressTo);
            if (body.getHead().getSubject() != null) {
                mimeMsg.setSubject(body.getHead().getSubject());
            } else {
                mimeMsg.setSubject("Direct message");
            }
            mailBody = new MimeMultipart();
            mainBody = new MimeBodyPart();
            if (body.getBody().getText() != null) {
                mainBody.setText(body.getBody().getText());
            } else {
                mainBody.setText("");
            }
            mailBody.addBodyPart(mainBody);
            copyAttachments(body, mailBody);
            mimeMsg.setContent(mailBody);
            DirectMimeMessage dMsg = new DirectMimeMessage(mimeMsg, getSenderHost());
            dMsg.updateMessageID();
            Transport transport;
            if (getUseTLSforSMTP().equals("SOCKET")) {
                transport = session.getTransport("smtps");
            } else {
                transport = session.getTransport("smtp");
            }
            transport.connect();
            try {
                transport.sendMessage(dMsg, addressTo);
                // Transport.send(dMsg);
                response.setMessageID(dMsg.getMessageID());
                transport.close();
            } finally {
                transport.close();
            }
        } catch (AddressException e) {
            ErrorType et = new ErrorType();
            et.setCode(ErrorCodeType.ADDRESSING);
            et.setMessage(e.getMessage());
            response.setError(et);
            log.error(e);
        } catch (MessagingException e) {
            ErrorType et = new ErrorType();
            et.setCode(ErrorCodeType.MESSAGING);
            et.setMessage(e.getMessage());
            response.setError(et);
            log.error(e);
        } catch (Exception e) {
            ErrorType et = new ErrorType();
            et.setCode(ErrorCodeType.SYSTEM);
            et.setMessage(e.getMessage());
            response.setError(et);
            log.error(e);
            e.printStackTrace();
        }
    }
    return response;
}
Also used : Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) InternetAddress(javax.mail.internet.InternetAddress) MessagingException(javax.mail.MessagingException) SendResponseType(org.nhindirect.schema.edge.ws.SendResponseType) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) InvalidPropertyException(org.springframework.beans.InvalidPropertyException) IOException(java.io.IOException) ErrorType(org.nhindirect.schema.edge.ws.ErrorType) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) AddressException(javax.mail.internet.AddressException) MimeBodyPart(javax.mail.internet.MimeBodyPart) AddressType(org.nhindirect.schema.edge.ws.AddressType) Transport(javax.mail.Transport) Session(javax.mail.Session)

Aggregations

AddressException (javax.mail.internet.AddressException)46 InternetAddress (javax.mail.internet.InternetAddress)37 MimeMessage (javax.mail.internet.MimeMessage)18 JavaMailInternetAddress (com.zimbra.common.mime.shim.JavaMailInternetAddress)17 MessagingException (javax.mail.MessagingException)15 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)9 Date (java.util.Date)7 Address (javax.mail.Address)7 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 MimeMultipart (javax.mail.internet.MimeMultipart)5 Properties (java.util.Properties)4 Session (javax.mail.Session)4 MimeBodyPart (javax.mail.internet.MimeBodyPart)4 ServiceException (com.zimbra.common.service.ServiceException)3 Account (com.zimbra.cs.account.Account)3 Invite (com.zimbra.cs.mailbox.calendar.Invite)3 ZAttendee (com.zimbra.cs.mailbox.calendar.ZAttendee)3 Locale (java.util.Locale)3