How to get outlook emails in asp.net
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.
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.