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;
}
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;
}
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);
}
}
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);
}
}
}
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;
}
Aggregations