amor - muammar
amor - muammar

Reputation: 61

Get id from just sended email

I try to get the ID of email that I just send it through Java EWS API. My goal is when I got that ID I would store it to Database.

This what I'm trying:

    try {
        String isiEmail     = generateIsiEmail(nmBank, jenis, tglAw, tglAk, produk);
        EmailMessage mail   = new EmailMessage(service);
        mail.setSubject(jdlEmail);
        mail.setBody(new MessageBody(isiEmail));

        //set to cc 
        mail.getToRecipients().add(new EmailAddress(from.replaceAll("\\s", "")));                   
        String[] too = to.split("\\;");
        for (int i = 0; i <too.length; i++) { 
            mail.getToRecipients().add(new EmailAddress(too[i].replaceAll("\\s", "")));                 
        }           
        String[] ccc = cc.split("\\;");
        for (int i = 0; i <ccc.length; i++) {
            mail.getCcRecipients().add(new EmailAddress(ccc[i].replaceAll("\\s", "")));
        }           
        mail.sendAndSaveCopy();
    } catch (ServiceLocalException e) {
        e.printStackTrace();
    } catch (Exception e) { 
        e.printStackTrace();
    }

Thanks for help.

Upvotes: 1

Views: 1816

Answers (4)

Pradip Rupareliya
Pradip Rupareliya

Reputation: 574

I am using C# EWS Api, But this logic will work for you. First you have to Save the email in Draft and then after you can get Email id.

EmailMessage emailMessage = new EmailMessage(service);
emailMessage.From = email.From;
emailMessage.Subject = email.Subject;
emailMessage.Body = new MessageBody(BodyType.HTML, email.Body);

foreach (var toAddress in email.To)
{
  emailMessage.ToRecipients.Add(toAddress);
}

// Send message and save copy by default to sentItems folder
emailMessage.Save();
emailMessage.Load();

// Get Email Conversation Id.
string ConversationId = emailMessage.ConversationId.UniqueId;
string EmailMessageId;

emailMessage.SendAndSaveCopy();


// Get Email Message Id by InternetMessageId.
List<SearchFilter> searchFilterCollection = new List<SearchFilter>();
            searchFilterCollection.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.InternetMessageId, InternetMessageId));

// Create the search filter.
SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, searchFilterCollection.ToArray());

ItemView view = new ItemView(50);
view.PropertySet = new PropertySet(BasePropertySet.IdOnly, EmailMessageSchema.InternetMessageId);
            FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.SentItems, searchFilter, view);

            
if (results.Items.Count > 0)
{
 Item item = results.Items[0];
 EmailMessage message = EmailMessage.Bind(service, item.Id);

 EmailMessageId = message.Id.UniqueId;
}

Upvotes: 1

amor - muammar
amor - muammar

Reputation: 61

I solved by my self. this step what I'm done with. I use ExtendedPropertyDefinition refer from this tutorial https://learn.microsoft.com/en-us/previous-versions/office/developer/exchange-server-2010/dd633654(v%3dexchg.80) , but I modified from C# into java programing language,

  1. set ExtendedPropertyDefinition then save the uuidToStr to database

        UUID uuid = UUID.randomUUID();
        ExtendedPropertyDefinition epd = new ExtendedPropertyDefinition(uuid, "NamaId", MapiPropertyType.String);
        String uuidToStr = uuid.toString();
    
        String isiEmail     = generateIsiEmail(nmBank, jenis, tglAw, tglAk, produk);
        EmailMessage mail   = new EmailMessage(service);
        mail.setSubject(jdlEmail);
        mail.setBody(new MessageBody(isiEmail)); 
    
        //set to cc 
        mail.getToRecipients().add(new EmailAddress(from.replaceAll("\\s", "")));                   
        String[] too = to.split("\\;");
        for (int i = 0; i <too.length; i++){
            mail.getToRecipients().add(new EmailAddress(too[i].replaceAll("\\s", "")));                 
        }           
        String[] ccc = cc.split("\\;");
        for (int i = 0; i <ccc.length; i++){
            mail.getCcRecipients().add(new EmailAddress(ccc[i].replaceAll("\\s", "")));
        }           
        mail.setExtendedProperty(epd, "isiId"); 
        mail.sendAndSaveCopy();     
    
  2. get the email based on ExtendedPropertyDefinition uuidToStr from database

        UUID uuid = UUID.fromString("cc59cdbf-aad4-4cd1-a4f0-e7819c56b884");
        ExtendedPropertyDefinition epd = new ExtendedPropertyDefinition(uuid, "NamaId", MapiPropertyType.String);
    
        ItemView view2 = new ItemView(3);
        SearchFilter sf = new SearchFilter.IsEqualTo(epd,"isiId");
        FindItemsResults<Item> fir = service.findItems(WellKnownFolderName.SentItems, sf, view2);
        for (Item itm : fir.getItems()){
            System.out.println(itm.getId());
            System.out.println(itm.getSubject());  
        }
    

DONE;

Upvotes: 3

MadDev
MadDev

Reputation: 1150

You should make use of the InternetMessageId property.

Call the FindItems method to search for messages in the sent items folder. Then instantiate an EmailMessage object so you can access the InternetMessageId property:

ItemView view = new ItemView(100); // You can change this to your needs.
view.PropertySet = new PropertySet(BasePropertySet.IdOnly, EmailMessageSchema.InternetMessageId);
FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.SentItems, view);
foreach (Item item in results)
{
    if (item is EmailMessage)
    {
        EmailMessage emailMsg = item as EmailMessage;
        Console.WriteLine(emailMsg.InternetMessageId);
    }
}

Upvotes: 1

plucins
plucins

Reputation: 153

I believe the solution is this:

EmailMessage em = EmailMessage.bind( service, item.getId(),
                   new PropertySet( EmailMessageSchema.InternetMessageId) );

Explanation :

We have to bind the item to an email message, but instead of grabbing all the info, we only ask for the ID and any additional properties we want through the PropertySet parameter.

found -> Exchange Web Services get Message Message-ID

Upvotes: -1

Related Questions