Search in sources :

Example 96 with Address

use of javax.mail.Address in project cuba by cuba-platform.

the class EmailerTest method doTestSeveralRecipients.

private void doTestSeveralRecipients(boolean useFs) throws MessagingException {
    emailerConfig.setFileStorageUsed(useFs);
    testMailSender.clearBuffer();
    String body = "Test Email Body";
    // 3 recipients
    String recipients = "misha@example.com,kolya@example.com;tanya@example.com;";
    EmailInfo myInfo = EmailInfoBuilder.create().setAddresses(recipients).setCaption("Test").setBody(body).build();
    List<SendingMessage> messages = emailer.sendEmailAsync(myInfo);
    assertEquals(3, messages.size());
    assertTrue(testMailSender.isEmpty());
    emailer.processQueuedEmails();
    Set<String> recipientSet = new HashSet<>();
    // check
    assertEquals(3, testMailSender.getBufferSize());
    for (int i = 0; i < 3; i++) {
        MimeMessage msg = testMailSender.fetchSentEmail();
        Address[] msgRecipients = msg.getAllRecipients();
        assertEquals(1, msgRecipients.length);
        recipientSet.add(msgRecipients[0].toString());
    }
    assertTrue(recipientSet.contains("misha@example.com"));
    assertTrue(recipientSet.contains("kolya@example.com"));
    assertTrue(recipientSet.contains("tanya@example.com"));
}
Also used : Address(javax.mail.Address) SendingMessage(com.haulmont.cuba.core.entity.SendingMessage) MimeMessage(javax.mail.internet.MimeMessage)

Example 97 with Address

use of javax.mail.Address in project alfresco-repository by Alfresco.

the class AbstractMailActionExecuterTest method testSendingToArrayOfCarbonCopyAndBlindCarbonCopyUsers.

/**
 * ALF-21948
 */
@Test
public void testSendingToArrayOfCarbonCopyAndBlindCarbonCopyUsers() throws MessagingException {
    Map<String, Serializable> params = new HashMap<String, Serializable>();
    String[] ccArray = { "cc_user1@example.com", "cc_user2@example.com" };
    String[] bccArray = { "bcc_user3@example.com", "bcc_user4@example.com", "bcc_user5@example.com" };
    params.put(MailActionExecuter.PARAM_FROM, "sender@email.com");
    params.put(MailActionExecuter.PARAM_TO, "test@email.com");
    params.put(MailActionExecuter.PARAM_CC, ccArray);
    params.put(MailActionExecuter.PARAM_BCC, bccArray);
    params.put(MailActionExecuter.PARAM_TEXT, "Mail body here");
    params.put(MailActionExecuter.PARAM_SUBJECT, "Subject text");
    Action mailAction = ACTION_SERVICE.createAction(MailActionExecuter.NAME, params);
    ACTION_EXECUTER.resetTestSentCount();
    ACTION_SERVICE.executeAction(mailAction, null);
    MimeMessage message = ACTION_EXECUTER.retrieveLastTestMessage();
    Assert.assertNotNull(message);
    Address[] all = message.getAllRecipients();
    Address[] ccs = message.getRecipients(RecipientType.CC);
    Address[] bccs = message.getRecipients(RecipientType.BCC);
    Assert.assertEquals(6, all.length);
    Assert.assertEquals(2, ccs.length);
    Assert.assertEquals(3, bccs.length);
    Assert.assertTrue(ccs[0].toString().contains("cc_user1") && ccs[1].toString().contains("cc_user2"));
    Assert.assertTrue(bccs[0].toString().contains("bcc_user3") && bccs[1].toString().contains("bcc_user4") && bccs[2].toString().contains("bcc_user5"));
}
Also used : Serializable(java.io.Serializable) Action(org.alfresco.service.cmr.action.Action) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) HashMap(java.util.HashMap) MimeMessage(javax.mail.internet.MimeMessage) Test(org.junit.Test)

Example 98 with Address

use of javax.mail.Address in project alfresco-repository by Alfresco.

the class AbstractMailActionExecuterTest method testSendingToCarbonCopy.

/**
 * Test for CC / BCC
 * @throws Exception
 */
@Test
public void testSendingToCarbonCopy() throws IOException, MessagingException {
    // PARAM_TO variant
    Action mailAction = ACTION_SERVICE.createAction(MailActionExecuter.NAME);
    mailAction.setParameterValue(MailActionExecuter.PARAM_FROM, "some.body@example.com");
    mailAction.setParameterValue(MailActionExecuter.PARAM_TO, "some.bodyelse@example.com");
    mailAction.setParameterValue(MailActionExecuter.PARAM_CC, "some.carbon@example.com");
    mailAction.setParameterValue(MailActionExecuter.PARAM_BCC, "some.blindcarbon@example.com");
    mailAction.setParameterValue(MailActionExecuter.PARAM_SUBJECT, "Testing CARBON COPY");
    mailAction.setParameterValue(MailActionExecuter.PARAM_TEMPLATE, "alfresco/templates/mail/test.txt.ftl");
    mailAction.setParameterValue(MailActionExecuter.PARAM_TEMPLATE_MODEL, (Serializable) getModel());
    ACTION_SERVICE.executeAction(mailAction, null);
    MimeMessage message = ACTION_EXECUTER.retrieveLastTestMessage();
    Assert.assertNotNull(message);
    Address[] all = message.getAllRecipients();
    Address[] ccs = message.getRecipients(RecipientType.CC);
    Address[] bccs = message.getRecipients(RecipientType.BCC);
    Assert.assertEquals(3, all.length);
    Assert.assertEquals(1, ccs.length);
    Assert.assertEquals(1, bccs.length);
    Assert.assertTrue(ccs[0].toString().contains("some.carbon"));
    Assert.assertTrue(bccs[0].toString().contains("some.blindcarbon"));
}
Also used : Action(org.alfresco.service.cmr.action.Action) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) Test(org.junit.Test)

Example 99 with Address

use of javax.mail.Address in project alfresco-repository by Alfresco.

the class SubethaEmailMessage method processMimeMessage.

private void processMimeMessage(MimeMessage mimeMessage) {
    if (from == null) {
        Address[] addresses = null;
        try {
            addresses = mimeMessage.getFrom();
        } catch (MessagingException e) {
            throw new EmailMessageException(ERR_EXTRACTING_FROM_ADDRESS, e.getMessage());
        }
        if (addresses == null || addresses.length == 0) {
        // throw new EmailMessageException(ERR_NO_FROM_ADDRESS);
        } else {
            if (addresses[0] instanceof InternetAddress) {
                from = ((InternetAddress) addresses[0]).getAddress();
            } else {
                from = addresses[0].toString();
            }
        }
    }
    if (to == null) {
        Address[] addresses = null;
        try {
            addresses = mimeMessage.getAllRecipients();
        } catch (MessagingException e) {
            throw new EmailMessageException(ERR_EXTRACTING_TO_ADDRESS, e.getMessage());
        }
        if (addresses == null || addresses.length == 0) {
        // throw new EmailMessageException(ERR_NO_TO_ADDRESS);
        } else {
            if (addresses[0] instanceof InternetAddress) {
                to = ((InternetAddress) addresses[0]).getAddress();
            } else {
                to = addresses[0].toString();
            }
        }
    }
    if (cc == null) {
        try {
            ArrayList<String> list = new ArrayList<String>();
            Address[] cca = mimeMessage.getRecipients(RecipientType.CC);
            if (cca != null) {
                for (Address a : cca) {
                    list.add(a.toString());
                }
            }
            cc = list;
        } catch (MessagingException e) {
            // Do nothing - this is not a show-stopper.
            cc = null;
        }
    }
    try {
        subject = mimeMessage.getSubject();
    // subject = encodeSubject(mimeMessage.getSubject());
    } catch (MessagingException e) {
        throw new EmailMessageException(ERR_EXTRACTING_SUBJECT, e.getMessage());
    }
    try {
        sentDate = mimeMessage.getSentDate();
    } catch (MessagingException e) {
        throw new EmailMessageException(ERR_EXTRACTING_SENT_DATE, e.getMessage());
    }
    if (sentDate == null) {
        // Just anti-null stub :)
        sentDate = new Date();
    }
    parseMessagePart(mimeMessage);
    attachments = new EmailMessagePart[attachmentList.size()];
    attachmentList.toArray(attachments);
    attachmentList = null;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MessagingException(javax.mail.MessagingException) ArrayList(java.util.ArrayList) EmailMessageException(org.alfresco.service.cmr.email.EmailMessageException) Date(java.util.Date)

Example 100 with Address

use of javax.mail.Address in project epadd by ePADD.

the class SearchResult method filterForCorrespondents.

/**
 * returns only the docs where the name or email address in the given field matches correspondentsStr in the given field(s).
 * correspondentsStr can be or-delimited and specify multiple strings.
 */
public static SearchResult filterForCorrespondents(SearchResult inputSet, String correspondentsStr, boolean checkToField, boolean checkFromField, boolean checkCcField, boolean checkBccField) {
    Set<Contact> searchedContacts = new LinkedHashSet<>();
    AddressBook ab = inputSet.archive.addressBook;
    Set<String> correspondents = Util.splitFieldForOr(correspondentsStr);
    for (String s : correspondents) {
        // this lookup will normalize, be case-insensitive, etc.
        Collection<Contact> contacts = ab.lookupByEmailOrName(s);
        if (contacts != null)
            searchedContacts.addAll(contacts);
    }
    // keep on removing those documents from allDocs which do not have any contact that matches ANY of searchedContacts
    inputSet.matchedDocs = inputSet.matchedDocs.entrySet().stream().filter(k -> {
        EmailDocument ed = (EmailDocument) k.getKey();
        Collection<Contact> contactsOfInterest = new LinkedHashSet<>();
        if (// add from addresses
        checkFromField && ed.from != null)
            contactsOfInterest.addAll(Arrays.stream(ed.from).map(address -> ab.lookupByAddress(address)).collect(Collectors.toList()));
        if (// add to address
        checkToField && ed.to != null)
            contactsOfInterest.addAll(Arrays.stream(ed.to).map(address -> ab.lookupByAddress(address)).collect(Collectors.toList()));
        if (// add ccd addresses
        checkCcField && ed.cc != null)
            contactsOfInterest.addAll(Arrays.stream(ed.cc).map(address -> ab.lookupByAddress(address)).collect(Collectors.toList()));
        if (// add bcc address
        checkToField & ed.bcc != null)
            contactsOfInterest.addAll(Arrays.stream(ed.bcc).map(address -> ab.lookupByAddress(address)).collect(Collectors.toList()));
        // Collection<Contact> contactsInMessage = EmailUtils.getContactsForMessage(ab, ed);
        return contactsOfInterest.stream().anyMatch(searchedContacts::contains);
    }).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    return inputSet;
}
Also used : Address(javax.mail.Address) Config(edu.stanford.muse.Config) java.util(java.util) Blob(edu.stanford.muse.datacache.Blob) AnnotationManager(edu.stanford.muse.AnnotationManager.AnnotationManager) JSPHelper(edu.stanford.muse.webapp.JSPHelper) Multimap(com.google.common.collect.Multimap) InternetAddress(javax.mail.internet.InternetAddress) LabelManager(edu.stanford.muse.LabelManager.LabelManager) MappedEntity(edu.stanford.muse.ie.variants.MappedEntity) CSVReader(au.com.bytecode.opencsv.CSVReader) EntityBook(edu.stanford.muse.ie.variants.EntityBook) EmailUtils(edu.stanford.muse.util.EmailUtils) LinkedHashMultimap(com.google.common.collect.LinkedHashMultimap) Span(edu.stanford.muse.util.Span) BlobStore(edu.stanford.muse.datacache.BlobStore) AddressBook(edu.stanford.muse.AddressBookManager.AddressBook) MailingList(edu.stanford.muse.AddressBookManager.MailingList) Util(edu.stanford.muse.util.Util) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) Entity(edu.stanford.muse.ie.Entity) Contact(edu.stanford.muse.AddressBookManager.Contact) Pair(edu.stanford.muse.util.Pair) Logger(org.apache.logging.log4j.Logger) FileReader(java.io.FileReader) Pattern(java.util.regex.Pattern) LogManager(org.apache.logging.log4j.LogManager) UnsupportedEncodingException(java.io.UnsupportedEncodingException) AddressBook(edu.stanford.muse.AddressBookManager.AddressBook) Contact(edu.stanford.muse.AddressBookManager.Contact)

Aggregations

Address (javax.mail.Address)203 InternetAddress (javax.mail.internet.InternetAddress)157 MimeMessage (javax.mail.internet.MimeMessage)71 MessagingException (javax.mail.MessagingException)60 ArrayList (java.util.ArrayList)38 Date (java.util.Date)30 Test (org.junit.Test)30 IOException (java.io.IOException)26 AddressException (javax.mail.internet.AddressException)25 Message (javax.mail.Message)23 JavaMailInternetAddress (com.zimbra.common.mime.shim.JavaMailInternetAddress)22 Properties (java.util.Properties)20 MimeBodyPart (javax.mail.internet.MimeBodyPart)19 Session (javax.mail.Session)18 MimeMultipart (javax.mail.internet.MimeMultipart)15 Account (com.zimbra.cs.account.Account)12 HashMap (java.util.HashMap)12 Locale (java.util.Locale)12 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)10 BodyPart (javax.mail.BodyPart)10