How to get outlook emails in asp.net | Code2night.com
Code2night
  • Home
  • Blogs
  • Guest Posts
  • Tutorial
  • Post Blog
  • Register
  • Login
  1. Home
  2. Blogpost

How to get outlook emails in asp.net

Date- Mar 02,2024

1897

Free Download Pay & Download
Microsoft Outlook Outlook Appointments

How to get outlook emails in asp.net

Integrating email capabilities into web applications is extremely useful for notifications, marketing, support tickets, and more. Microsoft Outlook is one of the most popular email services, with over 400 million active users.

In this article, we will walk through how to use ASP.NET to securely connect to Outlook accounts and retrieve emails. This allows building web apps that can programmatically read user emails for processing, analysis, backup, and other automations.



Microsoft Outlook

Step-1 Add Microsoft Exchange Nuget package in your application

First of all add a nugget package for Microsoft Exchange Services which will let you connect to Outlook from within your web application. We will use this to get our Outlook appointments and then for saving appointments also.

Step-2 Add Required Namespaces on controller

Add these namespaces to your controller. These are needed for excessing inbuilt methods for accessing or retrieving outlook emails.

using Microsoft.Exchange.WebServices.Data;

Step-3 Add Email Service as a separate class in your project

We will use this to get emails from our Outlook account. You can replace your Outlook username and password and then just run this piece of code. You have to create following class in your asp.net project. This service we will use to get the emails from outlook account.

  using Microsoft.Exchange.WebServices.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace OutlookEmail.Models
{
    public class EmailService
    {
        private readonly string _exchangeServiceUrl;
        private readonly string _username;
        private readonly string _password;

        public EmailService(string exchangeServiceUrl, string username, string password)
        {
            _exchangeServiceUrl = exchangeServiceUrl;
            _username = username;
            _password = password;
        }

        public void GetEmails()
        {
            // Create the ExchangeService object
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013);

            // Set credentials
            service.Credentials = new WebCredentials(_username, _password);

            // Set the URL of the Exchange server
            service.Url = new Uri(_exchangeServiceUrl);

            try
            {
                // Define the properties to retrieve (e.g., subject, sender, body)
              
                // Define the search filter
                SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));

                // Retrieve emails using FindItems method
                FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, searchFilter, new ItemView(10));

                // Iterate through the results
                foreach (Item item in findResults)
                {
                    if (item is EmailMessage)
                    {
                        EmailMessage message = item as EmailMessage;
                        // Access email properties
                        string subject = message.Subject;
                        string sender = message.Sender.Name;
                        string body = message.Body.Text;
                        // Do something with the email
                    }
                }
            }
            catch (Exception ex)
            {
                // Handle exceptions
                Console.WriteLine("Error: " + ex.Message);
            }
        }
    }
}

Step-4 After creating the service now we will add following code on the controller to use that email service for getting data from outlook

 using OutlookEmail.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OutlookEmail.Controllers
{
    public class HomeController : Controller
    {
        private readonly EmailService _emailService;

        public HomeController()
        {
            // Initialize EmailService with appropriate credentials
            _emailService = new EmailService("https://outlook.office365.com/EWS/Exchange.asmx", ConfigurationManager.AppSettings["OutlookEmail"].ToString(), ConfigurationManager.AppSettings["OutlookPassword"].ToString());
        }

        public ActionResult Index()
        {
            // Call GetEmails method to retrieve emails
            _emailService.GetEmails();
            return View();
        }
       
    }
}

You have to add following keys into your webconfig for getting the credentials for outlook account.

<add key="OutlookEmail" value="YourUser@outlook.com" />
<add key="OutlookPassword" value="Yourpassword" />

Conclusion

So this is how we can get emails from outlook in asp.net using Microsoft exchange services.

Comments

Tags

LinkedinLogin
LinkedinProfile
GetLinkedinProfile
C#
Aspnet
MVC
Linkedin
ITextSharp
Export to Pdf
AspNet Core
AspNet
View to Pdf in Aspnet
Model Validation In ASPNET Core MVC 60
Model Validation
Model Validation In ASPNET Core MVC
Model Validation In ASPNET
Image Compression in AspNet
Compress Image in c#
AspNet MVC
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1210
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook

Welcome To Code2night, A common place for sharing your programming knowledge,Blogs and Videos

  • Panipat
  • info@Code2night.com

Links

  • Home
  • Blogs
  • Tutorial
  • Post Blog

Popular Tags

Copyright © 2025 by Code2night. All Rights Reserved

  • Home
  • Blog
  • Login
  • SignUp
  • Contact
  • Terms & Conditions
  • Refund Policy
  • About Us
  • Privacy Policy
  • Json Beautifier
  • Guest Posts