Passing Email data to database using EWS

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Passing Email data to database using EWS



I'm currently working on a windows service that collects emails from a mailbox using the Exchange Web Service API, and passes the data from the emails to a stored procedure. The stored procedure then adds this data to a table in SSMS. I've tested the stored procedure manually and it functions fine. I've also got a method writing to a text file and all aspects of the emails (recipients, date, subject etc) are being collected.



My problem is that only the latest email in the mailbox is being passed to the database. Below is my code:


int offset = 0;
int pageSize = 50;
bool moreEmails = true;
ItemView view = new ItemView(pageSize, offset, OffsetBasePoint.Beginning);

view.PropertySet = PropertySet.IdOnly;
FindItemsResults<Item> findResults;
List<EmailMessage> emails = new List<EmailMessage>();
findResults = service.FindItems(WellKnownFolderName.Inbox, view);

while (moreEmails)

foreach (var item in findResults.Items)

emails.Add((EmailMessage)item);


moreEmails = findResults.MoreAvailable;

if (moreEmails)

view.Offset += pageSize;



PropertySet properties = (BasePropertySet.FirstClassProperties);
service.LoadPropertiesForItems(emails, properties);

foreach (var EmailParam in emails)

FromEmail = EmailParam.From.ToString();
EmailDate = EmailParam.DateTimeReceived.ToString("yyyy-MM-dd hh:mm:ss");
EmailSubject = EmailParam.Subject;
EmailBody = EmailParam.Body;
int SourceID = 1;

foreach (var recipient in EmailParam.ToRecipients)

ToEmail += recipient.Address.ToString();


foreach (var cc in EmailParam.CcRecipients)

Emailcc += cc.Address.ToString();


using (SqlConnection con = new SqlConnection(conStr))

con.Open();
SqlDataAdapter da = new SqlDataAdapter("mtsp_CreateTicket", con);
da.SelectCommand.CommandType = System.Data.CommandType.StoredProcedure;

da.SelectCommand.Parameters.AddWithValue("@Date", EmailDate);
da.SelectCommand.Parameters.AddWithValue("@FromEmail", FromEmail);
da.SelectCommand.Parameters.AddWithValue("@ToEmail", ToEmail);
da.SelectCommand.Parameters.AddWithValue("@EmailSubject", EmailSubject);
da.SelectCommand.Parameters.AddWithValue("@EmailBody", EmailBody);
da.SelectCommand.Parameters.AddWithValue("@Emailcc", Emailcc);
da.SelectCommand.Parameters.AddWithValue("@SourceID", SourceID);
da.SelectCommand.ExecuteNonQuery();




It's my first time using a windows service and I'm sort of new to this so any help is much appreciated. Thanks!





If there are multiple entries in the emails collection, perhaps you are getting an exception and the code is aborting the loop? Btw, you need to reset your ToEmail and Emailcc variables after closing the SqlConnection.
– stevie_c
Aug 10 at 14:12






can you put more piece of code? i just see the part when you read the parameters of 1 email, where is the service? where is the for or while to read all emails?
– polzka90
Aug 10 at 14:48





@stevie_c There doesn't appear to be any exceptions being thrown. The text file I'm writing to shows all the data is being collected. This code is also in a try-catch and no exception there. Moreover, I assumed setting the ToEmail and Emailcc in the foreach loop would reset for every iteration?
– Jura
Aug 10 at 14:50






@polzka90 I've added the code showing how I'm collecting the inbox emails.
– Jura
Aug 10 at 14:52






Since you're using the += operator on ToEmail and Emailcc those variables will continue to grow on each iteration around the "emails" variable.
– stevie_c
Aug 10 at 15:46









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard