Initial Commit ...
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Net.Mail;
|
||||
using System.Threading.Tasks;
|
||||
using xCommons.Extensions;
|
||||
using xExceptions.Constants;
|
||||
using xMessageService.Interfaces;
|
||||
using xMessageService.Models;
|
||||
|
||||
namespace xMessageService.Providers {
|
||||
public partial class XMailService : IXMailService {
|
||||
private SmtpClient smtpClient;
|
||||
private readonly string defaultProvider;
|
||||
private readonly IDictionary<string, XMailConfiguration> configurations;
|
||||
|
||||
public XMailService (
|
||||
string defaultProvider,
|
||||
IDictionary<string, XMailConfiguration> configurations
|
||||
) {
|
||||
this.defaultProvider = defaultProvider;
|
||||
this.configurations = configurations;
|
||||
|
||||
//
|
||||
// Get instance of SMTP Client ...
|
||||
PrepareSmtpClient ();
|
||||
}
|
||||
|
||||
public Task SendMailAsync (
|
||||
XMessage message,
|
||||
string provider = null
|
||||
) {
|
||||
//
|
||||
// Run Send Mail Asynchronous ...
|
||||
return Task.Run (() => {
|
||||
SendMail (message, provider);
|
||||
});
|
||||
}
|
||||
|
||||
#region Private ...
|
||||
/// <summary>
|
||||
/// Validate and Retrieve Provider Configuration for Service
|
||||
/// </summary>
|
||||
/// <param name="provider"></param>
|
||||
/// <returns></returns>
|
||||
private XMailConfiguration GetConfiguration (string provider = null) {
|
||||
//
|
||||
if (provider.IsNullOrEmpty ()) {
|
||||
provider = defaultProvider;
|
||||
}
|
||||
|
||||
//
|
||||
if (!configurations.Keys.Contains (provider)) {
|
||||
XException.MessageServiceInitialFailed.Throw ();
|
||||
}
|
||||
|
||||
//
|
||||
return configurations[provider];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Prepare SMTP Client by Using Specified/Default Provider
|
||||
/// </summary>
|
||||
/// <param name="provider"></param>
|
||||
private void PrepareSmtpClient (string provider = null) {
|
||||
//
|
||||
var config = GetConfiguration (provider);
|
||||
if (config.IsNull()) {
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
smtpClient = new SmtpClient () {
|
||||
Host = config.Host,
|
||||
Port = config.Port,
|
||||
EnableSsl = config.EnableSsl,
|
||||
UseDefaultCredentials = false,
|
||||
Credentials = new NetworkCredential (
|
||||
config.Username,
|
||||
config.Password)
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Prepare Mail Message With Specified/Default Provider
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
/// <param name="provider"></param>
|
||||
/// <returns></returns>
|
||||
private MailMessage PrepareMailMessage (
|
||||
XMessage message,
|
||||
string provider = null
|
||||
) {
|
||||
//
|
||||
if (message == null ||
|
||||
message.Message.IsNullOrEmpty () ||
|
||||
message.Recievers.Count == 0) {
|
||||
XException.InvalidMessage.Throw ();
|
||||
}
|
||||
|
||||
//
|
||||
var config = GetConfiguration (provider);
|
||||
var mailMessage = new MailMessage () {
|
||||
From = new MailAddress (config.Username),
|
||||
Subject = config.Title,
|
||||
Body = message.Message
|
||||
};
|
||||
|
||||
//
|
||||
foreach (var reciever in message.Recievers) {
|
||||
mailMessage.To.Add (reciever);
|
||||
}
|
||||
|
||||
//
|
||||
return mailMessage;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send Mail Using Specified/Default Provider
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
/// <param name="provider"></param>
|
||||
private void SendMail (
|
||||
XMessage message,
|
||||
string provider = null
|
||||
) {
|
||||
//
|
||||
if (smtpClient.IsNull()) {
|
||||
//
|
||||
Console.WriteLine ($"SMTP Error: MessageServiceInitialFailed ...");
|
||||
XException.MessageServiceInitialFailed.Throw ();
|
||||
}
|
||||
|
||||
//
|
||||
var mailMessage = PrepareMailMessage (message, provider);
|
||||
|
||||
//
|
||||
try {
|
||||
smtpClient.Send (mailMessage);
|
||||
} catch (Exception ex) {
|
||||
//
|
||||
Console.WriteLine ($"SMTP Error: {ex.Message}");
|
||||
XException.MessageServiceInitialFailed.Throw ();
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user