You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Ombi/src/Ombi.Notifications/GenericEmailProvider.cs

117 lines
4.5 KiB

using System;
using System.Threading.Tasks;
using MailKit.Net.Smtp;
using MimeKit;
7 years ago
using Ombi.Core.Settings;
using Ombi.Notifications.Models;
using Ombi.Notifications.Templates;
7 years ago
using Ombi.Settings.Settings.Models;
using Ombi.Settings.Settings.Models.Notifications;
namespace Ombi.Notifications
{
public class GenericEmailProvider : IEmailProvider
{
7 years ago
public GenericEmailProvider(ISettingsService<CustomizationSettings> s)
{
CustomizationSettings = s;
}
private ISettingsService<CustomizationSettings> CustomizationSettings { get; }
/// <summary>
/// This will load up the Email template and generate the HTML
/// </summary>
/// <param name="model"></param>
/// <param name="settings"></param>
/// <returns></returns>
public async Task SendAdHoc(NotificationMessage model, EmailNotificationSettings settings)
{
try
{
var email = new EmailBasicTemplate();
7 years ago
var customization = await CustomizationSettings.GetSettingsAsync();
var html = email.LoadTemplate(model.Subject, model.Message, null, customization.Logo);
var body = new BodyBuilder
{
HtmlBody = html,
//TextBody = model.Other["PlainTextBody"]
};
var message = new MimeMessage
{
Body = body.ToMessageBody(),
Subject = model.Subject
};
message.From.Add(new MailboxAddress(settings.SenderAddress, settings.SenderAddress));
message.To.Add(new MailboxAddress(model.To, model.To));
using (var client = new SmtpClient())
{
client.Connect(settings.Host, settings.Port); // Let MailKit figure out the correct SecureSocketOptions.
// Note: since we don't have an OAuth2 token, disable
// the XOAUTH2 authentication mechanism.
client.AuthenticationMechanisms.Remove("XOAUTH2");
if (settings.Authentication)
{
client.Authenticate(settings.Username, settings.Password);
}
//Log.Info("sending message to {0} \r\n from: {1}\r\n Are we authenticated: {2}", message.To, message.From, client.IsAuthenticated);
await client.SendAsync(message);
await client.DisconnectAsync(true);
}
}
catch (Exception e)
{
//Log.Error(e);
throw new InvalidOperationException(e.Message);
}
}
public async Task Send(NotificationMessage model, EmailNotificationSettings settings)
{
try
{
var body = new BodyBuilder
{
HtmlBody = model.Message,
//TextBody = model.Other["PlainTextBody"]
};
var message = new MimeMessage
{
Body = body.ToMessageBody(),
Subject = model.Subject
};
message.From.Add(new MailboxAddress(string.IsNullOrEmpty(settings.SenderName) ? settings.SenderAddress : settings.SenderName, settings.SenderAddress));
message.To.Add(new MailboxAddress(model.To, model.To));
using (var client = new SmtpClient())
{
client.Connect(settings.Host, settings.Port); // Let MailKit figure out the correct SecureSocketOptions.
// Note: since we don't have an OAuth2 token, disable
// the XOAUTH2 authentication mechanism.
client.AuthenticationMechanisms.Remove("XOAUTH2");
if (settings.Authentication)
{
client.Authenticate(settings.Username, settings.Password);
}
//Log.Info("sending message to {0} \r\n from: {1}\r\n Are we authenticated: {2}", message.To, message.From, client.IsAuthenticated);
await client.SendAsync(message);
await client.DisconnectAsync(true);
}
}
catch (Exception e)
{
//Log.Error(e);
throw new InvalidOperationException(e.Message);
}
}
}
}