git_bsmd/nsw/Source/bsmd.email/BSMDMail.cs
Daniel Schick f596558610 Version 3.1.1 Korrekturen und
DB Erweiterung für ENI-2 (Historisierung)
2016-05-26 08:12:25 +00:00

145 lines
5.3 KiB
C#

//
// Class: Mail
// Current CLR: 4.0.30319.34209
// System: Microsoft Visual Studio 10.0
// Author: dani
// Created: 5/28/2015 10:24:12 AM
//
// Copyright (c) 2015 Informatikbüro Daniel Schick. All rights reserved.
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Mime;
using System.Net.Mail;
using log4net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
namespace bsmd.email
{
public class BSMDMail : IDisposable
{
private SmtpClient client;
ILog log = LogManager.GetLogger(typeof(BSMDMail));
internal BSMDMail()
{
this.client = new SmtpClient();
NetworkCredential basicCredential = new NetworkCredential(Properties.Settings.Default.SMTPUser,
Properties.Settings.Default.SMTPPassword);
// setup up the host, increase the timeout to 5 minutes
this.client.Host = Properties.Settings.Default.SMTPServer;
this.client.UseDefaultCredentials = false;
this.client.Credentials = basicCredential;
this.client.Timeout = (60 * 5 * 1000);
this.client.EnableSsl = true;
// evil workaround for invalid BSMD E-Mail certificate!
ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{ return true; };
}
internal void Send(MailMessage message)
{
// doch nicht asynchron, da nur eine Nachricht gleichzeitig geschickt werden kann und ich so den sync code spare
try
{
this.client.Send(message);
log.InfoFormat("Email sending to {0} successful.", message.To.ToString());
}
catch (SmtpException smtpException)
{
log.ErrorFormat("SMTP error while sending e-mail:{0}", smtpException.Message);
}
catch (Exception ex)
{
log.ErrorFormat("other error while sending e-mail:{0}", ex.Message);
}
// http://stackoverflow.com/questions/7276375/what-are-best-practices-for-using-smtpclient-sendasync-and-dispose-under-net-4
// asynchron schicken und wenn's erledigt ist gleich disposen (wichtig!)
// in meinem Fall nicht so recht wirksam, da eine Mail meist an mehrere Empfänger geschickt wird
/*
smtpClient.SendCompleted += (s, e) =>
{
smtpClient.Dispose();
message.Dispose();
};
smtpClient.SendAsync(message, null);
*/
}
public static void SendNSWReportWithAttachments(string subject, List<string> filenameList)
{
using (BSMDMail mailer = new BSMDMail())
{
foreach (string recipient in Properties.Settings.Default.Recipient)
{
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress(Properties.Settings.Default.Sender);
message.From = fromAddress;
message.Subject = string.Format(subject);
message.IsBodyHtml = false;
message.Body = "see attachment";
message.To.Add(recipient);
foreach (string filename in filenameList)
{
Attachment attachment = new Attachment(filename, MediaTypeNames.Application.Octet);
ContentDisposition disposition = attachment.ContentDisposition;
disposition.CreationDate = File.GetCreationTime(filename);
disposition.ModificationDate = File.GetLastWriteTime(filename);
disposition.ReadDate = File.GetLastAccessTime(filename);
disposition.FileName = Path.GetFileName(filename);
disposition.Size = new FileInfo(filename).Length;
disposition.DispositionType = DispositionTypeNames.Attachment;
message.Attachments.Add(attachment);
}
mailer.Send(message);
message.Dispose();
}
}
}
public static void SendSystemInfo(string subject, string body, string recipient)
{
using (BSMDMail mailer = new BSMDMail())
{
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress(Properties.Settings.Default.Sender);
message.From = fromAddress;
message.Subject = subject;
message.IsBodyHtml = false;
message.Body = body;
message.To.Add(recipient);
MailAddress adminAddress = new MailAddress(Properties.Settings.Default.AdminEmail);
message.CC.Add(adminAddress);
mailer.Send(message);
}
}
public void Dispose()
{
this.client.Dispose();
}
}
}