dmdb/model/FormMail.cs

119 lines
5.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Reflection;
using System.Text;
using System.Xml.Serialization;
using dezentrale.core;
namespace dezentrale.model
{
public class FormMail : ReplaceReflectEntity<FormMail>
{
//[XmlIgnore] public string TemplateName { get; set; } = "n/a";
public string To { get; set; } = "{Nickname} <{EMail}>";
public string Subject { get; set; }
public string Body { get; set; }
//public override string ToString() { return TemplateName; }
public FormMail() { }
public FormMail(FormMail copyFrom)
{
if(copyFrom != null)
{
//this.TemplateName = copyFrom.TemplateName;
this.To = copyFrom.To;
this.Subject = copyFrom.Subject;
this.Body = copyFrom.Body;
this.AllowedObjectFields = copyFrom.AllowedObjectFields;
}
}
public LogSubEvent Send(object replaceReflect = null, List<string> files = null)
{
FormMail src = replaceReflect == null ? this : ReplaceReflect(replaceReflect);
LogSubEvent ret = new LogSubEvent()
{
Type = LogEvent.eEventType.EMail,
Topic = src.Subject,
Details = $"From: {Program.config.Smtp.From}\nTo: {src.To}\n\nBody:\n{src.Body}",
};
try
{
if (!Program.config.Smtp.Enabled)
{
throw new Exception("FormMail.Send() Error: SMTP is disabled!");
}
//Build up Mail Message
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress(Program.config.Smtp.From);
message.From = fromAddress;
message.To.Add(new MailAddress(src.To));
if (!string.IsNullOrEmpty(Program.config.Smtp.CcTo))
{
string[] addr = Program.config.Smtp.CcTo.Split(',', ';');
foreach (string s in addr)
if (!string.IsNullOrEmpty(s)) message.CC.Add(new MailAddress(s));
}
message.SubjectEncoding = Encoding.UTF8;
message.BodyEncoding = Encoding.UTF8;
//For spam reasons, we need to include an User-Agent and a valid message ID
//string mid;
//message.Headers.Add("User-Agent", $"dezentrale-members ver. {Program.VersionNumber} https://hg.macht-albern.de/dezentrale-members/");
//message.Headers.Add("User-Agent", @"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.7.0 Lightning/6.2.7");
message.Headers.Add("Message-Id",
$"<{Guid.NewGuid()}@{fromAddress.Host}>");
//System.Windows.Forms.MessageBox.Show($"Message-Id: {mid}");
message.Subject = src.Subject;
message.Body = src.Body;
if (files != null)
foreach (string f in files)
message.Attachments.Add(new System.Net.Mail.Attachment(f));
#pragma warning disable CS0618 //Suppress deprecation warning
/* Warning CS0618: 'SmtpClient' is obsolete: 'SmtpClient
* and its network of types are poorly designed, we strongly
* recommend you use https://github.com/jstedfast/MailKit
* and https://github.com/jstedfast/MimeKit instead
* (CS0618) (dezentrale-members)
*
* I'm pretty much aware about this fancy new mail client
* project by some $user, which lets me keep track of POP3 and
* IMAP accounts and everything, but at this point I simply
* need a possibility to send mails via SMTP, and this is a
* feature, SmtpClient does very well, without dragging
* dependencies to other code into our project.
*/
SmtpClient client = new SmtpClient(Program.config.Smtp.Host, Program.config.Smtp.Port)
#pragma warning restore CS0618
{
DeliveryMethod = SmtpDeliveryMethod.Network,
EnableSsl = Program.config.Smtp.SSL,
UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredential(Program.config.Smtp.UserName, Program.config.Smtp.Password),
};
client.Send(message);
//client.Send(Program.config.Smtp.From, src.To, src.Subject, src.Body);
}
catch (Exception ex)
{
Console.WriteLine($"FormMail.Send() Error: {ex.Message}\n");
if (ex.InnerException != null) Console.WriteLine($" inner exception: {ex.InnerException.Message}");
throw ex;
//ret.Type = LogEvent.eEventType.Error;
}
return ret;
}
}
}