153 lines
4.6 KiB
C#
153 lines
4.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.Composition;
|
|
using System.DirectoryServices.AccountManagement;
|
|
using System.Net.Mail;
|
|
using JD.Core.Logging;
|
|
using JD.Interfaces.Logging;
|
|
|
|
namespace JD.Core
|
|
{
|
|
public class MailManager : IDisposable
|
|
{
|
|
static MailManager()
|
|
{
|
|
Instance = new MailManager();
|
|
}
|
|
|
|
public static MailManager Instance { get; }
|
|
|
|
public const string DefaultMailServer = "relay.ubisoft.org";
|
|
|
|
private string _mailServer;
|
|
private SmtpClient _mailClient;
|
|
PrincipalContext _principalContext;
|
|
|
|
protected MailManager()
|
|
{
|
|
|
|
}
|
|
|
|
public void Init(string mailServer)
|
|
{
|
|
_mailServer = mailServer;
|
|
_mailClient = new SmtpClient(mailServer) {UseDefaultCredentials = true};
|
|
}
|
|
|
|
private void InitLDAP()
|
|
{
|
|
_principalContext = new PrincipalContext(ContextType.Domain, "10.0.0.9");
|
|
}
|
|
|
|
public bool SendMail(MailInfo mailInfo, string mailServer = null, bool fromMyself = false, bool toMyself = false)
|
|
{
|
|
try
|
|
{
|
|
if (_mailServer == null)
|
|
{
|
|
Init(mailServer ?? DefaultMailServer);
|
|
}
|
|
|
|
var mail = new MailMessage
|
|
{
|
|
Subject = mailInfo.Subject,
|
|
Body = mailInfo.Body
|
|
};
|
|
|
|
if (fromMyself)
|
|
{
|
|
if (_principalContext == null)
|
|
InitLDAP();
|
|
|
|
var user = UserPrincipal.FindByIdentity(_principalContext, System.IO.Path.GetFileName(System.Security.Principal.WindowsIdentity.GetCurrent().Name));
|
|
if (user == null)
|
|
return false;
|
|
|
|
mail.From = new MailAddress(user.EmailAddress, user.Name);
|
|
}
|
|
else
|
|
{
|
|
mail.From = new MailAddress(mailInfo.From.Mail, mailInfo.From.Name);
|
|
}
|
|
foreach (var recipient in mailInfo.To)
|
|
{
|
|
mail.To.Add(string.IsNullOrEmpty(recipient.Name) ? new MailAddress(recipient.Mail) : new MailAddress(recipient.Mail, recipient.Name));
|
|
}
|
|
|
|
if (toMyself)
|
|
{
|
|
if (_principalContext == null)
|
|
InitLDAP();
|
|
|
|
var user = UserPrincipal.FindByIdentity(_principalContext, System.IO.Path.GetFileName(System.Security.Principal.WindowsIdentity.GetCurrent().Name));
|
|
if (user == null)
|
|
return false;
|
|
|
|
mail.To.Add(new MailAddress(user.EmailAddress, user.Name));
|
|
}
|
|
|
|
mail.IsBodyHtml = mailInfo.IsHtml;
|
|
_mailClient.Send(mail);
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var log = JD.Interfaces.Composition.Objects.GetInstance(typeof(ILog)) as ILog;
|
|
if (log != null)
|
|
{
|
|
log.Error(LogCategories.Get("Mail"), "Failed to send mail. ");
|
|
log.Exception(ex);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public struct MailInfo
|
|
{
|
|
public MailInfo(string fromName, string fromMail, string subject, string body, bool isHtml)
|
|
{
|
|
From = new MailRecipient {Name = fromName, Mail = fromMail} ;
|
|
To = new List<MailRecipient>();
|
|
Subject = subject;
|
|
Body = body;
|
|
IsHtml = isHtml;
|
|
}
|
|
|
|
public MailRecipient From { get; }
|
|
|
|
public List<MailRecipient> To { get; }
|
|
|
|
public string Subject { get; }
|
|
|
|
public string Body { get; }
|
|
|
|
public bool IsHtml { get; }
|
|
|
|
public void AddRecipient(string mail)
|
|
{
|
|
To.Add(new MailRecipient { Name = null, Mail = mail });
|
|
}
|
|
|
|
public void AddRecipient(string mail, string name)
|
|
{
|
|
To.Add(new MailRecipient { Name = name, Mail = mail });
|
|
}
|
|
}
|
|
|
|
public struct MailRecipient
|
|
{
|
|
public string Name { get; set; }
|
|
public string Mail { get; set; }
|
|
}
|
|
|
|
[Export(typeof(ILogCategory))]
|
|
public class MailLogCategory : LogCategory { }
|
|
|
|
public void Dispose()
|
|
{
|
|
_mailClient?.Dispose();
|
|
_principalContext?.Dispose();
|
|
}
|
|
}
|
|
}
|