Following code can be used to authenticate with outlook and fetch email content and attachments
1. Authenticate outlook.
public void AuthenticateOutlook()
Pop3Client pop3Client;
pop3Client = new Pop3Client();
pop3Client.Connect("pod51016.outlook.com", 995, true);
pop3Client.Authenticate("emailID", "Password");
int count = pop3Client.GetMessageCount();
if (count > 0)
FetchOutlookData(pop3Client, count);
private void FetchOutlookData(Pop3Client pop3Client, int count)
{
DataTable dtMessages = new DataTable();
dtMessages.Columns.Add("EmailFrom");
dtMessages.Columns.Add("EmailSubject");
dtMessages.Columns.Add("EmailDateSent");
dtMessages.Columns.Add("EmailContent");
dtMessages.Columns.Add("EmailMessageId");
//dtMessages.Columns.Add("ResumeFilename");
//dtMessages.Columns.Add("ResumeChangedName");
dtMessages.Columns.Add("EmailCounter");
DataTable dataTableAttachment = new DataTable();
dataTableAttachment.Columns.Add("EmailCounter");
dataTableAttachment.Columns.Add("AttachmentName");
dataTableAttachment.Columns.Add("ActualName");
for (int i = GetMailCounter() + 1; i <= 30; i++)
{
Message message = pop3Client.GetMessage(i);
MessagePart messagePart = message.MessagePart;
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
string mailDate = message.Headers.DateSent.ToShortDateString();
string currentDate = DateTime.Now.ToShortDateString();
dtMessages.Rows.Add();
dtMessages.Rows[dtMessages.Rows.Count - 1]["EmailFrom"] = message.Headers.From;
dtMessages.Rows[dtMessages.Rows.Count - 1]["EmailSubject"] = message.Headers.Subject;
dtMessages.Rows[dtMessages.Rows.Count - 1]["EmailDateSent"] = message.Headers.DateSent;
if (messagePart.Body != null)
{
dtMessages.Rows[dtMessages.Rows.Count - 1]["EmailContent"] = messagePart.BodyEncoding.GetString(messagePart.Body);
}
else
{
dtMessages.Rows[dtMessages.Rows.Count - 1]["EmailContent"] = "";
}
dtMessages.Rows[dtMessages.Rows.Count - 1]["EmailMessageId"] = message.Headers.MessageId;
dtMessages.Rows[dtMessages.Rows.Count - 1]["EmailCounter"] = i;
DLAttachment(message, i, dataTableAttachment);
// pop3Client.DeleteMessage(i);
}
}
1. Authenticate outlook.
public void AuthenticateOutlook()
Pop3Client pop3Client;
pop3Client = new Pop3Client();
pop3Client.Connect("pod51016.outlook.com", 995, true);
pop3Client.Authenticate("emailID", "Password");
int count = pop3Client.GetMessageCount();
if (count > 0)
FetchOutlookData(pop3Client, count);
private void FetchOutlookData(Pop3Client pop3Client, int count)
{
DataTable dtMessages = new DataTable();
dtMessages.Columns.Add("EmailFrom");
dtMessages.Columns.Add("EmailSubject");
dtMessages.Columns.Add("EmailDateSent");
dtMessages.Columns.Add("EmailContent");
dtMessages.Columns.Add("EmailMessageId");
//dtMessages.Columns.Add("ResumeFilename");
//dtMessages.Columns.Add("ResumeChangedName");
dtMessages.Columns.Add("EmailCounter");
DataTable dataTableAttachment = new DataTable();
dataTableAttachment.Columns.Add("EmailCounter");
dataTableAttachment.Columns.Add("AttachmentName");
dataTableAttachment.Columns.Add("ActualName");
for (int i = GetMailCounter() + 1; i <= 30; i++)
{
Message message = pop3Client.GetMessage(i);
MessagePart messagePart = message.MessagePart;
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
string mailDate = message.Headers.DateSent.ToShortDateString();
string currentDate = DateTime.Now.ToShortDateString();
dtMessages.Rows.Add();
dtMessages.Rows[dtMessages.Rows.Count - 1]["EmailFrom"] = message.Headers.From;
dtMessages.Rows[dtMessages.Rows.Count - 1]["EmailSubject"] = message.Headers.Subject;
dtMessages.Rows[dtMessages.Rows.Count - 1]["EmailDateSent"] = message.Headers.DateSent;
if (messagePart.Body != null)
{
dtMessages.Rows[dtMessages.Rows.Count - 1]["EmailContent"] = messagePart.BodyEncoding.GetString(messagePart.Body);
}
else
{
dtMessages.Rows[dtMessages.Rows.Count - 1]["EmailContent"] = "";
}
dtMessages.Rows[dtMessages.Rows.Count - 1]["EmailMessageId"] = message.Headers.MessageId;
dtMessages.Rows[dtMessages.Rows.Count - 1]["EmailCounter"] = i;
DLAttachment(message, i, dataTableAttachment);
// pop3Client.DeleteMessage(i);
}
}
No comments:
Post a Comment