TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Jaspreet Singh
NA
20
3.8k
Unable to Read Emails in Outlook
Nov 8 2019 2:33 AM
Hello Team,
I am unable to read emails in outlook by using the following code:-
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Office.Interop.Outlook;
namespace EmailRead
{
class Program
{
static void Main(string[] args)
{
ReadMailItems();
}
private static void ReadMailItems()
{
Application outlookApplication = null;
NameSpace outlookNamespace = null;
MAPIFolder inboxFolder = null;
const string destinationDirectory = @"C:\TestFileSave";
if (!Directory.Exists(destinationDirectory))
{
Directory.CreateDirectory(destinationDirectory);
}
int emailCounter = 0;
try
{
outlookApplication = new Application();
outlookNamespace = outlookApplication.GetNamespace("MAPI");
inboxFolder = outlookApplication.ActiveExplorer().Session.GetDefaultFolder(OlDefaultFolders.olFolderInbox).Folders["Birthday Emails"];
foreach (MailItem item in inboxFolder.Items)
{
if (item.UnRead)
{
string fromEmailAddress = item.SenderEmailAddress.ToString();
emailCounter++;
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine("From: " + item.SenderEmailAddress.ToString());
stringBuilder.AppendLine("To: " + item.To);
stringBuilder.AppendLine("CC: " + item.CC);
stringBuilder.AppendLine("");
stringBuilder.AppendLine("Subject: " + item.Subject);
stringBuilder.AppendLine(item.Body);
Attachments attachments = item.Attachments;
MailItem newEmail = item as MailItem;
if (newEmail == null) continue;
if (newEmail.Attachments.Count > 0)
{
for (int i = 1; i <= newEmail.Attachments.Count; i++)
{
string fileName = newEmail.Attachments[i].FileName.ToLower();
if (!fileName.Contains(".png") && !fileName.Contains(".jpg") && !fileName.Contains(".jpeg"))
{
string filePath = Path.Combine(destinationDirectory, newEmail.Attachments[i].FileName);
newEmail.Attachments[i].SaveAsFile(filePath);
Console.WriteLine(stringBuilder.ToString());
}
}
}
item.UnRead = false;
}
Marshal.ReleaseComObject(item);
}
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
ReleaseComObject(inboxFolder);
ReleaseComObject(outlookNamespace);
ReleaseComObject(outlookApplication);
}
}
private static void ReleaseComObject(object obj)
{
if (obj != null)
{
Marshal.ReleaseComObject(obj);
obj = null;
}
}
}
}
I am using a Community Edition of Visual Studio and getting the following error message:-
Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'. The system cannot find the file specified.
File name: 'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'
at EmailRead.Program.ReadMailItems()
at EmailRead.Program.Main(String[] args) in C:\Users\jaspreet1.singh\source\repos\EmailTrigger\EmailRead\Program.cs:line 13
Can someone pleae advise on this.
Reply
Answers (
2
)
Is the command to check if the bell is running or stopped ?
Share code for master&details (adding multiple row) EF in m