Skip to content

Instantly share code, notes, and snippets.

@iyhammad
Created April 11, 2018 08:30
Show Gist options
  • Save iyhammad/a947bf12893cf1670422e166ed79bc03 to your computer and use it in GitHub Desktop.
Save iyhammad/a947bf12893cf1670422e166ed79bc03 to your computer and use it in GitHub Desktop.
CustomMailKitSmtpBuilder --- Compatible with Office 365
using Abp.MailKit;
using Abp.Net.Mail.Smtp;
using MailKit.Net.Smtp;
using MailKit.Security;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace SystemX.Web.MailBauilder
{
//This is to be compatible with Office 365 Mails
public class CustomMailKitSmtpBuilder : DefaultMailKitSmtpBuilder
{
private ISmtpEmailSenderConfiguration _smtpEmailSenderConfiguration;
public CustomMailKitSmtpBuilder(ISmtpEmailSenderConfiguration smtpEmailSenderConfiguration)
: base(smtpEmailSenderConfiguration)
{
this._smtpEmailSenderConfiguration = smtpEmailSenderConfiguration;
}
protected override void ConfigureClient(SmtpClient client)
{
client.Connect(
_smtpEmailSenderConfiguration.Host,
_smtpEmailSenderConfiguration.Port,
SecureSocketOptions.StartTlsWhenAvailable
);
if (_smtpEmailSenderConfiguration.UseDefaultCredentials)
{
return;
}
client.Authenticate(
_smtpEmailSenderConfiguration.UserName,
_smtpEmailSenderConfiguration.Password
);
}
}
}
public class WebModule : AbpModule
{
public override void PreInitialize()
{
//Other configuration goes here
Configuration.ReplaceService<IMailKitSmtpBuilder, CustomMailKitSmtpBuilder>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment