Below code shows how to send email via outlook host using SMTP client in C#
public interface IOutlookMailClient
{
void SendMail(string recipient, string subject, string message);
}
{
void SendMail(string recipient, string subject, string message);
}
public class OutlookMailClient : IOutlookMailClient
{
{
private readonly string sender;
private readonly string password;
private readonly string host;
private readonly int port;
private readonly OutlookMailServiceOptions emailOptions;
private readonly ILogger<OutlookMailClient> logger;
private readonly string password;
private readonly string host;
private readonly int port;
private readonly OutlookMailServiceOptions emailOptions;
private readonly ILogger<OutlookMailClient> logger;
public OutlookMailClient(IOptions<OutlookMailServiceOptions> mailOptions,ILogger<OutlookMailClient> logger)
{
this.emailOptions = mailOptions.Value; ;
this.password = emailOptions.Password;
this.host = emailOptions.Host;
this.port = emailOptions.Port;
this.sender = emailOptions.Sender;
this.logger = logger;
}
{
this.emailOptions = mailOptions.Value; ;
this.password = emailOptions.Password;
this.host = emailOptions.Host;
this.port = emailOptions.Port;
this.sender = emailOptions.Sender;
this.logger = logger;
}
public void SendMail(string recipient, string subject, string message)
{
logger.LogInformation("Enter : OutlookMailClient.SendMail");
logger.LogInformation("Mail recipient :" +recipient +" Subject : "+subject);
{
logger.LogInformation("Enter : OutlookMailClient.SendMail");
logger.LogInformation("Mail recipient :" +recipient +" Subject : "+subject);
SmtpClient client = new SmtpClient(host);//new SmtpClient("outlook.office365.com");
client.Port = port;
//client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
System.Net.NetworkCredential credentials =
new System.Net.NetworkCredential(sender, password);
client.EnableSsl = true;
client.Credentials = credentials;
client.Port = port;
//client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
System.Net.NetworkCredential credentials =
new System.Net.NetworkCredential(sender, password);
client.EnableSsl = true;
client.Credentials = credentials;
try
{
var mail = new MailMessage(sender.Trim(), recipient.Trim());
mail.Subject = subject;
mail.Body = message;
client.Send(mail);
logger.LogInformation("Exit : OutlookMailClient.SendMail");
}
catch (Exception ex)
{
logger.LogError(ex.Message);
throw ex;
}
}
{
var mail = new MailMessage(sender.Trim(), recipient.Trim());
mail.Subject = subject;
mail.Body = message;
client.Send(mail);
logger.LogInformation("Exit : OutlookMailClient.SendMail");
}
catch (Exception ex)
{
logger.LogError(ex.Message);
throw ex;
}
}
}
}
}
IOptions<OutlookMailServiceOptions> is a asp.net core feature to read data from appsetting.json using a model. here OutlookMailServiceOptions is a POCO class used to keep mail settings from Json config file. you can use any alternative if you are not using asp.net core.
public class OutlookMailServiceOptions
{
public string Sender { get; set; }
public string Host { get; set; }
public int Port { get; set; }
public string Password { get; set; }
}
{
public string Sender { get; set; }
public string Host { get; set; }
public int Port { get; set; }
public string Password { get; set; }
}
put this configuration in appsetting.json
"OutlookMailServiceOptions": {
"Sender": "sender mail id",
"Host": "outlook.office365.com",
"Port": 587,
"Password": "your password"
}
"Host": "outlook.office365.com",
"Port": 587,
"Password": "your password"
}
Now you have to call this api in your code, to call first you need to inject this api via dependency injection in ConfigureService method of Startup.cs like :
services.Configure<OutlookMailServiceOptions>(Configuration.GetSection(nameof(OutlookMailServiceOptions))).AddSingleton(x => x.GetRequiredService<IOptions<OutlookMailServiceOptions>>().Value);
services.AddScoped<IOutlookMailClient, OutlookMailClient>();
Now, you can inject this API in your Controller via constructor :
private readonly IOutlookMailClient emailClient
DefaultController (IOutlookMailClient emailClient) //Constructor
{
this.emailClient=emailClient;
}
//mail api injected , now simply call sendMail() method from any method like:
emailClient.SendMail("To mail address", "Subject", "Body");
All the other required details (Sender, Host, Port, etc.) to send mail, API will read from AppSetting.Json via IOption mechanism.