In this blog, I am explaining how to send mail on any domain using an exchange server. You can implement an exchange server following this step.
Step 1 - Create a Console application.
Step 2 - Add "Microsoft.Exchange" library reference in your console application.
Reference -> Manage Nuget Package--> Add below package
Microsoft.Exchange.WebServices
Description:
The Exchange Web Services (EWS) Managed API provides a .NET Framework interface to EWS in Exchange Online, Exchange Online as part of Office 365, and versions of Exchange starting with Exchange Server 2007 Service Pack 1 (SP1).
You can use this version of the EWS Managed API to evaluate the library for your application needs, to compare it to directly using XML or the auto-generated proxy library, and to create production-ready applications.
or Add via PowerShell commad
PM>Install-Package Microsoft.Exchange.WebServices
Step 3: add "System.Configuration" library in your console app
Thereafter create a method to create mailing concept using exchange server.
using System;
using System.Configuration;
using System.Net;
using Microsoft.Exchange.WebServices.Data;
namespace SendmailByEWS
{
class Program
{
static void Main(string[] args)
{
string MailUser =ConfigurationManager.AppSettings["MailUser"];
string MailPass = ConfigurationManager.AppSettings["mailPass"].ToString();
string MailTo = ConfigurationManager.AppSettings["MailTo"].ToString();
var isSentEmail = SendMail(MailUser, MailPass, MailTo);
}
public static bool SendMail(string MailUser,string MailPass,string MailTo)
{
try
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
service.Credentials = new NetworkCredential(MailUser, MailPass);
service.AutodiscoverUrl(MailUser, (a) =>
{
return true;
});
// service.AutodiscoverUrl(MailUser);
EmailMessage emailMessage = new EmailMessage(service);
emailMessage.Subject ="Profanity Alert! | Profanity Found | Review";
emailMessage.Body = new MessageBody("Hi Admin, profanity found, please review and take further action");
emailMessage.ToRecipients.Add(MailTo);
emailMessage.SendAndSaveCopy();
return true;
}
catch (Exception ex)
{
return false;
}
}
}
}
Dont forget to Configuration values in App.config file
<configuration>
<add key="MailUser" value="girishkumar.s@{Tenant}.onmicrosoft.com"/>
<add key="mailPass" value="********"/>
<add key="MailTo" value="abc@{Tenant}.onmicrosoft.com"/>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
Put Tenant name
Send Email using Outlook Exchange Web Service (EWS) - C# Console Application Example
No comments:
Post a Comment