can you show me how you have configured C# code for sending and reading emails.
Of course
ReadMailsFromOutlook:
first of all, I count the number of mails with the next C# code:
Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook._MailItem mailItem = (Microsoft.Office.Interop.Outlook._MailItem)app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
Microsoft.Office.Interop.Outlook.NameSpace nameSpace = app.GetNamespace("MAPI");
Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = nameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
inboxFolder = nameSpace.Folders[account].Folders[bandeja];
//Hay que contar los elementos que hay para poder leerlos con un for
numMails = inboxFolder.Items.Count;
Then, I can go to Outlook and loop all emaill:
Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
error = "1";
Microsoft.Office.Interop.Outlook._MailItem mailItem = (Microsoft.Office.Interop.Outlook._MailItem)app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
error = "2";
Microsoft.Office.Interop.Outlook.NameSpace nameSpace = app.GetNamespace("MAPI");
error = "3";
Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = nameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
error = "4";
inboxFolder = nameSpace.Folders[account].Folders[bandeja];
body = "null";
subject = "null";
sender = "null";
senderAddress = "null";
if ((Microsoft.Office.Interop.Outlook.MailItem)inboxFolder.Items
is Microsoft.Office.Interop.Outlook.MailItem && (Microsoft.Office.Interop.Outlook.MailItem)inboxFolder.Items!= null){
//error = "dentro del if";
body = ((Microsoft.Office.Interop.Outlook.MailItem)inboxFolder.Items).Body;
//error = "pasa body";
subject = ((Microsoft.Office.Interop.Outlook.MailItem)inboxFolder.Items).Subject;
//error = "pasa subject";
sender = ((Microsoft.Office.Interop.Outlook.MailItem)inboxFolder.Items).SenderName;
//error = "pasa sender";
senderAddress = ((Microsoft.Office.Interop.Outlook.MailItem)inboxFolder.Items).SenderEmailAddress;
//error = "pasa emailAddress";
//date = ((Microsoft.Office.Interop.Outlook.MailItem)inboxFolder.Items).CreationTime;
//attachment = ((Microsoft.Office.Interop.Outlook.MailItem)inboxFolder.Items).Attachments;
error = "mail";
}
else {
i = i + 1;
body = "null";
subject = "null";
sender = "null";
senderAddress = "null";
error = "no es igual";
//date = ((Microsoft.Office.Interop.Outlook.MailItem)inboxFolder.Items).CreationTime;
}
//}
This code returns a collection with all mails. I have a rule in my Outlook to move all the mails like meetings and other "extrange" mails.
SendMailsFromOutlook:
Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook._MailItem mailItem = (Microsoft.Office.Interop.Outlook._MailItem)app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
//mailItem.Sender = "";
mailItem.Subject = Subject;
mailItem.To = To;
mailItem.Body = Body;
mailItem.Recipients.Add(Cc);
mailItem.Attachments.Add(Path, OlAttachmentType.olByValue, Type.Missing, Type.Missing);
mailItem.Importance = Microsoft.Office.Interop.Outlook.OlImportance.olImportanceLow;
mailItem.Display(false);
//return mailItem;
mailItem.Send();
I control the Cc and the attachment with the next diagram: