Search in sources :

Example 86 with AddressException

use of javax.mail.internet.AddressException in project oozie by apache.

the class SLAEmailEventListener method parseAddress.

private Address[] parseAddress(String str) {
    Address[] addrs = null;
    List<InternetAddress> addrList = new ArrayList<InternetAddress>();
    String[] emails = str.split(ADDRESS_SEPARATOR, -1);
    for (String email : emails) {
        boolean isBlackListed = false;
        AtomicInteger val = blackList.getIfPresent(email);
        if (val != null) {
            isBlackListed = (val.get() >= blacklistFailCount);
        }
        if (!isBlackListed) {
            try {
                // turn on strict syntax check by setting 2nd argument true
                addrList.add(new InternetAddress(email, true));
            } catch (AddressException ae) {
                // simply skip bad address but do not throw exception
                LOG.error("Skipping bad destination address: " + email, ae);
            }
        }
    }
    if (addrList.size() > 0) {
        addrs = (Address[]) addrList.toArray(new InternetAddress[addrList.size()]);
    }
    return addrs;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AddressException(javax.mail.internet.AddressException) ArrayList(java.util.ArrayList)

Example 87 with AddressException

use of javax.mail.internet.AddressException in project acs-aem-commons by Adobe-Consulting-Services.

the class EmailServiceImpl method sendEmail.

@Override
public List<String> sendEmail(String templatePath, Map<String, String> emailParams, Map<String, DataSource> attachments, String... recipients) {
    List<String> failureList = new ArrayList<String>();
    if (recipients == null || recipients.length <= 0) {
        throw new IllegalArgumentException(MSG_INVALID_RECIPIENTS);
    }
    List<InternetAddress> addresses = new ArrayList<InternetAddress>(recipients.length);
    for (String recipient : recipients) {
        try {
            addresses.add(new InternetAddress(recipient));
        } catch (AddressException e) {
            log.warn("Invalid email address {} passed to sendEmail(). Skipping.", recipient);
        }
    }
    InternetAddress[] iAddressRecipients = addresses.toArray(new InternetAddress[addresses.size()]);
    List<InternetAddress> failureInternetAddresses = sendEmail(templatePath, emailParams, attachments, iAddressRecipients);
    for (InternetAddress address : failureInternetAddresses) {
        failureList.add(address.toString());
    }
    return failureList;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) AddressException(javax.mail.internet.AddressException) ArrayList(java.util.ArrayList)

Example 88 with AddressException

use of javax.mail.internet.AddressException in project spring-integration by spring-projects.

the class ImapMailReceiverTests method testIdleWithServerCustomSearch.

@Test
public void testIdleWithServerCustomSearch() throws Exception {
    ImapMailReceiver receiver = new ImapMailReceiver("imap://user:pw@localhost:" + imapIdleServer.getPort() + "/INBOX");
    receiver.setSearchTermStrategy((supportedFlags, folder) -> {
        try {
            FromTerm fromTerm = new FromTerm(new InternetAddress("bar@baz"));
            return new AndTerm(fromTerm, new FlagTerm(new Flags(Flag.SEEN), false));
        } catch (AddressException e) {
            throw new RuntimeException(e);
        }
    });
    testIdleWithServerGuts(receiver, false);
}
Also used : AndTerm(javax.mail.search.AndTerm) InternetAddress(javax.mail.internet.InternetAddress) FlagTerm(javax.mail.search.FlagTerm) AddressException(javax.mail.internet.AddressException) Flags(javax.mail.Flags) FromTerm(javax.mail.search.FromTerm) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 89 with AddressException

use of javax.mail.internet.AddressException in project hudson-2.x by hudson.

the class BaseBuildResultMail method createEmptyMail.

/**
 * Creates empty mail.
 *
 * @param build build.
 * @param listener listener.
 * @return empty mail.
 * @throws MessagingException exception if any.
 */
protected MimeMessage createEmptyMail(AbstractBuild<?, ?> build, BuildListener listener) throws MessagingException {
    MimeMessage msg = new HudsonMimeMessage(Mailer.descriptor().createSession());
    // TODO: I'd like to put the URL to the page in here,
    // but how do I obtain that?
    msg.setContent("", "text/plain");
    msg.setFrom(new InternetAddress(Mailer.descriptor().getAdminAddress()));
    msg.setSentDate(new Date());
    Set<InternetAddress> rcp = new LinkedHashSet<InternetAddress>();
    StringTokenizer tokens = new StringTokenizer(getRecipients());
    while (tokens.hasMoreTokens()) {
        String address = tokens.nextToken();
        if (address.startsWith("upstream-individuals:")) {
            // people who made a change in the upstream
            String projectName = address.substring("upstream-individuals:".length());
            AbstractProject up = Hudson.getInstance().getItemByFullName(projectName, AbstractProject.class);
            if (up == null) {
                listener.getLogger().println("No such project exist: " + projectName);
                continue;
            }
            includeCulpritsOf(up, build, listener, rcp);
        } else {
            // ordinary address
            try {
                rcp.add(new InternetAddress(address));
            } catch (AddressException e) {
                // report bad address, but try to send to other addresses
                e.printStackTrace(listener.error(e.getMessage()));
            }
        }
    }
    if (CollectionUtils.isNotEmpty(upstreamProjects)) {
        for (AbstractProject project : upstreamProjects) {
            includeCulpritsOf(project, build, listener, rcp);
        }
    }
    if (sendToIndividuals) {
        Set<User> culprits = build.getCulprits();
        if (debug)
            listener.getLogger().println("Trying to send e-mails to individuals who broke the build. sizeof(culprits)==" + culprits.size());
        rcp.addAll(buildCulpritList(listener, culprits));
    }
    msg.setRecipients(Message.RecipientType.TO, rcp.toArray(new InternetAddress[rcp.size()]));
    AbstractBuild<?, ?> pb = build.getPreviousBuild();
    if (pb != null) {
        MailMessageIdAction b = pb.getAction(MailMessageIdAction.class);
        if (b != null) {
            msg.setHeader("In-Reply-To", b.messageId);
            msg.setHeader("References", b.messageId);
        }
    }
    return msg;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) InternetAddress(javax.mail.internet.InternetAddress) User(hudson.model.User) HudsonMimeMessage(hudson.tasks.HudsonMimeMessage) Date(java.util.Date) MailMessageIdAction(hudson.tasks.MailMessageIdAction) StringTokenizer(java.util.StringTokenizer) MimeMessage(javax.mail.internet.MimeMessage) HudsonMimeMessage(hudson.tasks.HudsonMimeMessage) AddressException(javax.mail.internet.AddressException) AbstractProject(hudson.model.AbstractProject)

Example 90 with AddressException

use of javax.mail.internet.AddressException in project stanbol by apache.

the class SimpleMailExtractor method extract.

public void extract(URI id, InputStream stream, Charset charset, String mimeType, RDFContainer result) throws ExtractorException {
    try {
        // parse the stream
        MimeMessage message = new MimeMessage(null, stream);
        result.add(RDF.type, NMO.Email);
        // extract the full-text
        StringBuilder buffer = new StringBuilder(10000);
        processMessage(message, buffer, result);
        String text = buffer.toString().trim();
        if (text.length() > 0) {
            result.add(NMO.plainTextMessageContent, text);
            result.add(NIE.plainTextContent, text);
        }
        // extract other metadata
        String title = message.getSubject();
        if (title != null) {
            title = title.trim();
            if (title.length() > 0) {
                result.add(NMO.messageSubject, title);
            }
        }
        try {
            copyAddress(message.getFrom(), NMO.from, result);
        } catch (AddressException e) {
        // ignore
        }
        copyAddress(getRecipients(message, RecipientType.TO), NMO.to, result);
        copyAddress(getRecipients(message, RecipientType.CC), NMO.cc, result);
        copyAddress(getRecipients(message, RecipientType.BCC), NMO.bcc, result);
        MailUtil.getDates(message, result);
    } catch (MessagingException e) {
        throw new ExtractorException(e);
    } catch (IOException e) {
        throw new ExtractorException(e);
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) ExtractorException(org.semanticdesktop.aperture.extractor.ExtractorException) IOException(java.io.IOException)

Aggregations

AddressException (javax.mail.internet.AddressException)130 InternetAddress (javax.mail.internet.InternetAddress)108 MimeMessage (javax.mail.internet.MimeMessage)43 MessagingException (javax.mail.MessagingException)38 ArrayList (java.util.ArrayList)23 IOException (java.io.IOException)21 UnsupportedEncodingException (java.io.UnsupportedEncodingException)21 Address (javax.mail.Address)21 JavaMailInternetAddress (com.zimbra.common.mime.shim.JavaMailInternetAddress)18 Properties (java.util.Properties)18 Session (javax.mail.Session)15 Date (java.util.Date)14 MimeBodyPart (javax.mail.internet.MimeBodyPart)14 MimeMultipart (javax.mail.internet.MimeMultipart)12 File (java.io.File)11 Message (javax.mail.Message)10 Multipart (javax.mail.Multipart)9 DataHandler (javax.activation.DataHandler)8 Test (org.junit.Test)7 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)5