Search in sources :

Example 21 with MessagingException

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);
            }
        }
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) Transport(javax.mail.Transport) Properties(java.util.Properties) MessagingException(javax.mail.MessagingException)

Example 22 with MessagingException

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) {
            }
        }
    }
}
Also used : Message(javax.mail.Message) MessagingException(javax.mail.MessagingException) ArrayList(java.util.ArrayList) Folder(javax.mail.Folder) HashSet(java.util.HashSet)

Example 23 with MessagingException

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;
}
Also used : BodyPart(javax.mail.BodyPart) InternetHeaders(javax.mail.internet.InternetHeaders) MessagingException(javax.mail.MessagingException) MessagingException(javax.mail.MessagingException)

Example 24 with MessagingException

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;
}
Also used : MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 25 with MessagingException

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;
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) TxDetail(org.nhindirect.common.tx.model.TxDetail)

Aggregations

MessagingException (javax.mail.MessagingException)324 MimeMessage (javax.mail.internet.MimeMessage)126 IOException (java.io.IOException)125 InternetAddress (javax.mail.internet.InternetAddress)64 MimeMultipart (javax.mail.internet.MimeMultipart)60 MimeBodyPart (javax.mail.internet.MimeBodyPart)58 Message (javax.mail.Message)45 Session (javax.mail.Session)43 InputStream (java.io.InputStream)41 Properties (java.util.Properties)38 Date (java.util.Date)35 ArrayList (java.util.ArrayList)33 PackageException (com.axway.ats.action.objects.model.PackageException)32 Address (javax.mail.Address)32 NoSuchMimePackageException (com.axway.ats.action.objects.model.NoSuchMimePackageException)31 PublicAtsApi (com.axway.ats.common.PublicAtsApi)31 ServiceException (com.zimbra.common.service.ServiceException)30 ByteArrayInputStream (java.io.ByteArrayInputStream)25 DataHandler (javax.activation.DataHandler)23 BodyPart (javax.mail.BodyPart)23