use of javax.mail.MessagingException in project gocd by gocd.
the class GoSmtpMailSender method send.
public ValidationBean send(String subject, String body, String to) {
Transport transport = null;
try {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("Sending email [%s] to [%s]", subject, to));
}
Properties props = mailProperties();
MailSession session = MailSession.getInstance().createWith(props, username, password);
transport = session.getTransport();
transport.connect(host, port, nullIfEmpty(username), nullIfEmpty(password));
MimeMessage msg = session.createMessage(from, to, subject, body);
transport.sendMessage(msg, msg.getRecipients(TO));
return ValidationBean.valid();
} catch (Exception e) {
LOGGER.error(String.format("Sending failed for email [%s] to [%s]", subject, to), e);
return ValidationBean.notValid(ERROR_MESSAGE);
} finally {
if (transport != null) {
try {
transport.close();
} catch (MessagingException e) {
LOGGER.error("Failed to close transport", e);
}
}
}
}
use of javax.mail.MessagingException in project gocd by gocd.
the class Pop3MailClient method matchMessages.
private String matchMessages(MessageMatcher messageContains) throws MessagingException, IOException {
Folder folder = null;
try {
folder = getInboxFolder();
final Set<String> contents = new HashSet<>();
Message[] messagesInBox = folder.getMessages();
List<Message> messages = new ArrayList<>();
for (Message message : messagesInBox) {
if (messageContains.matches(message)) {
messages.add(message);
}
}
if (messages == null || messages.size() == 0) {
throw new RuntimeException("No message found matching :" + messageContains + ", the actual messages are " + contents);
}
if (messages.size() > 1) {
throw new RuntimeException("Multiple message matched content");
}
return (String) messages.get(0).getContent();
} finally {
if (folder != null && folder.isOpen()) {
try {
folder.close(true);
} catch (MessagingException e) {
}
}
}
}
use of javax.mail.MessagingException in project nhin-d by DirectProject.
the class MDNStandard method getNotificationFieldsAsHeaders.
/**
* Parses the notification part fields of the MimeMultipart body of a MDN message. The multipart is expected to conform to the MDN specification
* as described in RFC3798.
* @return The notification part fields as a set of Internet headers.
*/
public static InternetHeaders getNotificationFieldsAsHeaders(MimeMultipart mm) {
InternetHeaders retVal = null;
if (mm == null)
throw new IllegalArgumentException("Multipart can not be null");
try {
if (mm.getCount() < 2)
throw new IllegalArgumentException("Multipart can not be null");
// the second part should be the notification
BodyPart part = mm.getBodyPart(1);
try {
Object contecntObj = part.getContent();
if (dsnClass != null && dsnClass.getCanonicalName().equals(contecntObj.getClass().getCanonicalName())) {
retVal = (InternetHeaders) getHeaders.invoke(contecntObj);
return retVal;
}
} catch (Exception e) {
/* no-op */
}
if (!part.getContentType().equalsIgnoreCase(MDNStandard.MediaType.DispositionNotification))
throw new IllegalArgumentException("Notification part content type is not " + MDNStandard.MediaType.DispositionNotification);
// parse fields
retVal = new InternetHeaders();
String[] fields = getPartContentBodyAsString(part).split("\r\n");
for (String field : fields) {
int idx = field.indexOf(":");
if (idx > -1) {
String name = field.substring(0, idx);
String value = field.substring(idx + 1).trim();
retVal.setHeader(name, value);
}
}
} catch (MessagingException e) {
throw new IllegalArgumentException("Failed to parse notification fields.", e);
}
return retVal;
}
use of javax.mail.MessagingException in project nhin-d by DirectProject.
the class HumanReadableTextAssembler method makeBodyPart.
protected MimeBodyPart makeBodyPart(List<Address> rejectedRecipients, String errorMessage) throws MessagingException {
String assembleHtmlBody;
try {
assembleHtmlBody = assembleHtmlBody(rejectedRecipients, errorMessage);
} catch (IOException e) {
throw new MessagingException("", e);
}
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setContent(assembleHtmlBody, "text/html");
return mimeBodyPart;
}
use of javax.mail.MessagingException in project nhin-d by DirectProject.
the class DefaultTxDetailParser method getMessageDetails.
public Map<String, TxDetail> getMessageDetails(InputStream stream) {
Map<String, TxDetail> retVal = null;
if (stream == null)
throw new IllegalArgumentException("Input stream cannot be null");
try {
// convert into a MimeMessage
final MimeMessage msg = new MimeMessage(null, stream);
retVal = getMessageDetails(msg);
}///CLOVER:OFF
catch (MessagingException e) {
LOGGER.warn("Failed to translate input stream into MimeMessage.", e);
}
return retVal;
}
Aggregations