From c66db81f91cef04fd88253c8d16d75b6e9086809 Mon Sep 17 00:00:00 2001 From: Riku Virtanen Date: Wed, 29 Nov 2023 15:37:41 +0200 Subject: [PATCH] PR review changed --- .../SendEmailTests.cs | 316 +++++++++--------- .../Definitions/Attachment.cs | 62 ++-- .../Definitions/AttachmentFromString.cs | 30 +- .../Definitions/AttachmentOptions.cs | 24 +- .../Definitions/AttachmentType.cs | 26 +- .../Definitions/Input.cs | 118 +++---- .../Definitions/Options.cs | 87 +++-- .../Definitions/Result.cs | 42 +-- .../Frends.Smtp.SendEmail.csproj | 2 +- .../Frends.Smtp.SendEmail/SendEmailTask.cs | 230 +++++++------ Frends.SMTP.SendEmail/README.md | 2 +- 11 files changed, 467 insertions(+), 472 deletions(-) diff --git a/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail.Tests/SendEmailTests.cs b/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail.Tests/SendEmailTests.cs index da8a2d8..a452b6e 100644 --- a/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail.Tests/SendEmailTests.cs +++ b/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail.Tests/SendEmailTests.cs @@ -3,197 +3,195 @@ using System.IO; using Frends.SMTP.SendEmail.Definitions; -namespace Frends.SMTP.SendEmail.Tests +namespace Frends.SMTP.SendEmail.Tests; + +[TestFixture] +public class SendEmailTests { - [TestFixture] - public class SendEmailTests + // ****************************************** FILL THESE ****************************************************** + private static readonly string USERNAME = Environment.GetEnvironmentVariable("Frends_SMTP_Username"); + private static readonly string PASSWORD = Environment.GetEnvironmentVariable("Frends_SMTP_Password"); + private static readonly string SMTPADDRESS = Environment.GetEnvironmentVariable("Frends_SMTP_Address"); + private static readonly string TOEMAILADDRESS = Environment.GetEnvironmentVariable("Frends_SMTP_Email"); + private static readonly string FROMEMAILADDRESS = Environment.GetEnvironmentVariable("Frends_SMTP_Email"); + private const int PORT = 587; + private const bool USESSL = true; + private const bool USEWINDOWSAUTHENTICATION = false; + // ************************************************************************************************************ + + + private const string TEMP_ATTACHMENT_SOURCE = "emailtestattachments"; + private const string TEST_FILE_NAME = "testattachment.txt"; + private const string TEST_FILE_NOT_EXISTING = "doesntexist.txt"; + + private string _localAttachmentFolder; + private string _filepath; + private Input _input; + private Input _input2; + private Options _options; + + [SetUp] + public void EmailTestSetup() { - // ****************************************** FILL THESE ****************************************************** - private static readonly string USERNAME = Environment.GetEnvironmentVariable("Frends_SMTP_Username"); - private static readonly string PASSWORD = Environment.GetEnvironmentVariable("Frends_SMTP_Password"); - private static readonly string SMTPADDRESS = Environment.GetEnvironmentVariable("Frends_SMTP_Address"); - private static readonly string TOEMAILADDRESS = Environment.GetEnvironmentVariable("Frends_SMTP_Email"); - private static readonly string FROMEMAILADDRESS = Environment.GetEnvironmentVariable("Frends_SMTP_Email"); - private const int PORT = 587; - private const bool USESSL = true; - private const bool USEWINDOWSAUTHENTICATION = false; - // ************************************************************************************************************ - - - private const string TEMP_ATTACHMENT_SOURCE = "emailtestattachments"; - private const string TEST_FILE_NAME = "testattachment.txt"; - private const string TEST_FILE_NOT_EXISTING = "doesntexist.txt"; - - private string _localAttachmentFolder; - private string _filepath; - private Input _input; - private Input _input2; - private Options _options; - - [SetUp] - public void EmailTestSetup() - { - _localAttachmentFolder = Path.Combine(Path.GetTempPath(), TEMP_ATTACHMENT_SOURCE); - - if (!Directory.Exists(_localAttachmentFolder)) - Directory.CreateDirectory(_localAttachmentFolder); - - _filepath = Path.Combine(_localAttachmentFolder, TEST_FILE_NAME); - - if (!File.Exists(_filepath)) - { - File.Create(_filepath).Dispose(); - } - - _input = new Input() - { - From = FROMEMAILADDRESS, - To = TOEMAILADDRESS, - Cc = "", - Bcc = "", - Message = "testmsg", - IsMessageHtml = false, - SenderName = "EmailTestSender", - MessageEncoding = "utf-8" - }; - - _input2 = new Input() - { - From = FROMEMAILADDRESS, - To = TOEMAILADDRESS, - Cc = null, - Bcc = null, - Message = "testmsg", - IsMessageHtml = false, - SenderName = "EmailTestSender", - MessageEncoding = "utf-8" - }; - - _options = new Options() - { - UserName = USERNAME, - Password = PASSWORD, - SMTPServer = SMTPADDRESS, - Port = PORT, - UseSsl = USESSL, - UseWindowsAuthentication = USEWINDOWSAUTHENTICATION, - }; + _localAttachmentFolder = Path.Combine(Path.GetTempPath(), TEMP_ATTACHMENT_SOURCE); - } - [TearDown] - public void EmailTestTearDown() + if (!Directory.Exists(_localAttachmentFolder)) + Directory.CreateDirectory(_localAttachmentFolder); + + _filepath = Path.Combine(_localAttachmentFolder, TEST_FILE_NAME); + + if (!File.Exists(_filepath)) { - if (Directory.Exists(_localAttachmentFolder)) - Directory.Delete(_localAttachmentFolder, true); + File.Create(_filepath).Dispose(); } - [Test] - public void SendEmailWithPlainText() + _input = new Input() { - var input = _input; - input.Subject = "Email test - PlainText"; + From = FROMEMAILADDRESS, + To = TOEMAILADDRESS, + Cc = "", + Bcc = "", + Message = "testmsg", + IsMessageHtml = false, + SenderName = "EmailTestSender", + MessageEncoding = "utf-8" + }; + + _input2 = new Input() + { + From = FROMEMAILADDRESS, + To = TOEMAILADDRESS, + Cc = null, + Bcc = null, + Message = "testmsg", + IsMessageHtml = false, + SenderName = "EmailTestSender", + MessageEncoding = "utf-8" + }; + + _options = new Options() + { + UserName = USERNAME, + Password = PASSWORD, + SMTPServer = SMTPADDRESS, + Port = PORT, + UseSsl = USESSL, + UseWindowsAuthentication = USEWINDOWSAUTHENTICATION, + }; - var result = SMTP.SendEmail(input, null, _options, default); - Assert.IsTrue(result.EmailSent); - } + } + [TearDown] + public void EmailTestTearDown() + { + if (Directory.Exists(_localAttachmentFolder)) + Directory.Delete(_localAttachmentFolder, true); + } - [Test] - public void SendEmailWithFileAttachment() - { - var input = _input; - input.Subject = "Email test - FileAttachment"; + [Test] + public void SendEmailWithPlainText() + { + var input = _input; + input.Subject = "Email test - PlainText"; - var attachment = new Attachment - { - AttachmentType = AttachmentType.FileAttachment, - FilePath = _filepath, - SendIfNoAttachmentsFound = false, - ThrowExceptionIfAttachmentNotFound = true - }; + var result = SMTP.SendEmail(input, null, _options, default); + Assert.IsTrue(result.EmailSent); + } + [Test] + public void SendEmailWithFileAttachment() + { + var input = _input; + input.Subject = "Email test - FileAttachment"; - var Attachments = new AttachmentOptions { Attachments = new Attachment[] { attachment } }; + var attachment = new Attachment + { + AttachmentType = AttachmentType.FileAttachment, + FilePath = _filepath, + SendIfNoAttachmentsFound = false, + ThrowExceptionIfAttachmentNotFound = true + }; - var result = SMTP.SendEmail(input, Attachments, _options, default); - Assert.IsTrue(result.EmailSent); - } - [Test] - public void SendEmailWithStringAttachment() + var Attachments = new AttachmentOptions { Attachments = new Attachment[] { attachment } }; + + var result = SMTP.SendEmail(input, Attachments, _options, default); + Assert.IsTrue(result.EmailSent); + } + + [Test] + public void SendEmailWithStringAttachment() + { + var input = _input; + input.Subject = "Email test - AttachmentFromString"; + var fileAttachment = new AttachmentFromString() { FileContent = "teststring � �", FileName = "testfilefromstring.txt" }; + var attachment = new Attachment() { - var input = _input; - input.Subject = "Email test - AttachmentFromString"; - var fileAttachment = new AttachmentFromString() { FileContent = "teststring � �", FileName = "testfilefromstring.txt" }; - var attachment = new Attachment() - { - AttachmentType = AttachmentType.AttachmentFromString, - StringAttachment = fileAttachment - }; - var Attachments = new AttachmentOptions { Attachments = new Attachment[] { attachment } }; - - var result = SMTP.SendEmail(input, Attachments, _options, default); - Console.WriteLine(result.StatusString); - Assert.IsTrue(result.EmailSent); - } + AttachmentType = AttachmentType.AttachmentFromString, + StringAttachment = fileAttachment + }; + var Attachments = new AttachmentOptions { Attachments = new Attachment[] { attachment } }; + + var result = SMTP.SendEmail(input, Attachments, _options, default); + Assert.IsTrue(result.EmailSent); + } + + [Test] + public void TrySendingEmailWithNoFileAttachmentFound() + { + var input = _input; + input.Subject = "Email test"; - [Test] - public void TrySendingEmailWithNoFileAttachmentFound() + var attachment = new Attachment { - var input = _input; - input.Subject = "Email test"; + FilePath = Path.Combine(_localAttachmentFolder, TEST_FILE_NOT_EXISTING), + SendIfNoAttachmentsFound = false, + ThrowExceptionIfAttachmentNotFound = false + }; - var attachment = new Attachment - { - FilePath = Path.Combine(_localAttachmentFolder, TEST_FILE_NOT_EXISTING), - SendIfNoAttachmentsFound = false, - ThrowExceptionIfAttachmentNotFound = false - }; + var Attachments = new AttachmentOptions { Attachments = new Attachment[] { attachment } }; - var Attachments = new AttachmentOptions { Attachments = new Attachment[] { attachment } }; + var result = SMTP.SendEmail(input, Attachments, _options, default); + Assert.IsFalse(result.EmailSent); + } - var result = SMTP.SendEmail(input, Attachments, _options, default); - Assert.IsFalse(result.EmailSent); - } + [Test] + public void TrySendingEmailWithNoCcAndBcc() + { + var input = _input2; + input.Subject = "Email test"; - [Test] - public void TrySendingEmailWithNoCcAndBcc() + var attachment = new Attachment { - var input = _input2; - input.Subject = "Email test"; + FilePath = Path.Combine(_localAttachmentFolder, TEST_FILE_NOT_EXISTING), + SendIfNoAttachmentsFound = false, + ThrowExceptionIfAttachmentNotFound = false + }; - var attachment = new Attachment - { - FilePath = Path.Combine(_localAttachmentFolder, TEST_FILE_NOT_EXISTING), - SendIfNoAttachmentsFound = false, - ThrowExceptionIfAttachmentNotFound = false - }; + var Attachments = new AttachmentOptions { Attachments = new Attachment[] { attachment } }; - var Attachments = new AttachmentOptions { Attachments = new Attachment[] { attachment } }; + var result = SMTP.SendEmail(input, Attachments, _options, default); + Assert.IsFalse(result.EmailSent); + } - var result = SMTP.SendEmail(input, Attachments, _options, default); - Assert.IsFalse(result.EmailSent); - } + [Test] + public void TrySendingEmailWithNoFileAttachmentFoundException() + { + var input = _input; + input.Subject = "Email test"; - [Test] - public void TrySendingEmailWithNoFileAttachmentFoundException() + var attachment = new Attachment { - var input = _input; - input.Subject = "Email test"; + FilePath = Path.Combine(_localAttachmentFolder, TEST_FILE_NOT_EXISTING), + SendIfNoAttachmentsFound = false, + ThrowExceptionIfAttachmentNotFound = true + }; - var attachment = new Attachment - { - FilePath = Path.Combine(_localAttachmentFolder, TEST_FILE_NOT_EXISTING), - SendIfNoAttachmentsFound = false, - ThrowExceptionIfAttachmentNotFound = true - }; + var Attachments = new AttachmentOptions { Attachments = new Attachment[] { attachment } }; - var Attachments = new AttachmentOptions { Attachments = new Attachment[] { attachment } }; + Assert.Throws(() => SMTP.SendEmail(input, Attachments, _options, default)); - Assert.Throws(() => SMTP.SendEmail(input, Attachments, _options, default)); - - } } } diff --git a/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/Definitions/Attachment.cs b/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/Definitions/Attachment.cs index 4d6c660..8133139 100644 --- a/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/Definitions/Attachment.cs +++ b/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/Definitions/Attachment.cs @@ -1,42 +1,42 @@ using System.ComponentModel; using System.ComponentModel.DataAnnotations; -namespace Frends.SMTP.SendEmail.Definitions +namespace Frends.SMTP.SendEmail.Definitions; + +/// +/// Attachment +/// +public class Attachment { /// - /// Attachment + /// Chooses if the attachment file is created from a string or copied from disk. /// - public class Attachment - { - /// - /// Chooses if the attachment file is created from a string or copied from disk. - /// - public AttachmentType AttachmentType { get; set; } + public AttachmentType AttachmentType { get; set; } - /// - /// Attachment from string. - /// - [UIHint(nameof(AttachmentType), "", AttachmentType.AttachmentFromString)] - public AttachmentFromString StringAttachment { get; set; } + /// + /// Attachment from string. + /// + [UIHint(nameof(AttachmentType), "", AttachmentType.AttachmentFromString)] + public AttachmentFromString StringAttachment { get; set; } - /// - /// Attachment file's path. Uses Directory.GetFiles(string, string) as a pattern matching technique. See https://msdn.microsoft.com/en-us/library/wz42302f(v=vs.110).aspx. - /// Exception: If the path ends in a directory, all files in that folder are added as attachments. - /// - [DefaultValue("")] - [UIHint(nameof(AttachmentType), "", AttachmentType.FileAttachment)] - public string FilePath { get; set; } + /// + /// Attachment file's path. Uses Directory.GetFiles(string, string) as a pattern matching technique. See https://msdn.microsoft.com/en-us/library/wz42302f(v=vs.110).aspx. + /// Exception: If the path ends in a directory, all files in that folder are added as attachments. + /// + [DefaultValue("")] + [UIHint(nameof(AttachmentType), "", AttachmentType.FileAttachment)] + public string FilePath { get; set; } - /// - /// If set true and no files match the given path, an exception is thrown. - /// - [UIHint(nameof(AttachmentType), "", AttachmentType.FileAttachment)] - public bool ThrowExceptionIfAttachmentNotFound { get; set; } + /// + /// If set true and no files match the given path, an exception is thrown. + /// + [UIHint(nameof(AttachmentType), "", AttachmentType.FileAttachment)] + public bool ThrowExceptionIfAttachmentNotFound { get; set; } - /// - /// If set true and no files match the given path, email will be sent nevertheless. - /// - [UIHint(nameof(AttachmentType), "", AttachmentType.FileAttachment)] - public bool SendIfNoAttachmentsFound { get; set; } - } + /// + /// If set true and no files match the given path, email will be sent nevertheless. + /// + [UIHint(nameof(AttachmentType), "", AttachmentType.FileAttachment)] + public bool SendIfNoAttachmentsFound { get; set; } } + diff --git a/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/Definitions/AttachmentFromString.cs b/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/Definitions/AttachmentFromString.cs index 7c6292f..bae919a 100644 --- a/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/Definitions/AttachmentFromString.cs +++ b/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/Definitions/AttachmentFromString.cs @@ -1,22 +1,22 @@ using System.ComponentModel; -namespace Frends.SMTP.SendEmail.Definitions +namespace Frends.SMTP.SendEmail.Definitions; + +/// +/// Parameters for adding attachment from string. +/// +public class AttachmentFromString { /// - /// Parameters for adding attachment from string. + /// Content of the attachment file /// - public class AttachmentFromString - { - /// - /// Content of the attachment file - /// - [DefaultValue("")] - public string FileContent { get; set; } + [DefaultValue("")] + public string FileContent { get; set; } - /// - /// Name of the attachment file - /// - [DefaultValue("")] - public string FileName { get; set; } - } + /// + /// Name of the attachment file + /// + [DefaultValue("")] + public string FileName { get; set; } } + diff --git a/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/Definitions/AttachmentOptions.cs b/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/Definitions/AttachmentOptions.cs index 0c38676..882e68b 100644 --- a/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/Definitions/AttachmentOptions.cs +++ b/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/Definitions/AttachmentOptions.cs @@ -1,16 +1,16 @@ -namespace Frends.SMTP.SendEmail.Definitions +namespace Frends.SMTP.SendEmail.Definitions; + +/// +/// Parameters for attachments. +/// +public class AttachmentOptions { /// - /// Parameters for attachments. + /// Array of Attachments to be send with the message. /// - public class AttachmentOptions - { - /// - /// Array of Attachments to be send with the message. - /// - /// [ - /// Attachment { AttachmentType = AttachmentType.FileAttachment, FilePath = C:\pathToTheFile\test.txt, SendIfNoAttachmentsFound = false, ThrowExceptionIfAttachmentNotFound = true }, - /// Attachment { AttachmentType = AttachmentType.AttachmentFromString, AttachmentFromString { FileContent = This is test file, FileName = testfile.txt } } ] - public Attachment[] Attachments { get; set; } - } + /// [ + /// Attachment { AttachmentType = AttachmentType.FileAttachment, FilePath = C:\pathToTheFile\test.txt, SendIfNoAttachmentsFound = false, ThrowExceptionIfAttachmentNotFound = true }, + /// Attachment { AttachmentType = AttachmentType.AttachmentFromString, AttachmentFromString { FileContent = This is test file, FileName = testfile.txt } } ] + public Attachment[] Attachments { get; set; } } + diff --git a/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/Definitions/AttachmentType.cs b/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/Definitions/AttachmentType.cs index 8b6e0e0..314e4f8 100644 --- a/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/Definitions/AttachmentType.cs +++ b/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/Definitions/AttachmentType.cs @@ -1,18 +1,18 @@ -namespace Frends.SMTP.SendEmail.Definitions +namespace Frends.SMTP.SendEmail.Definitions; + +/// +/// Enumeration for Attachment type. +/// +public enum AttachmentType { /// - /// Enumeration for Attachment type. + /// Select this if the attachment is a file. /// - public enum AttachmentType - { - /// - /// Select this if the attachment is a file. - /// - FileAttachment, + FileAttachment, - /// - /// Select this if the attachment file should be created from a string. - /// - AttachmentFromString - } + /// + /// Select this if the attachment file should be created from a string. + /// + AttachmentFromString } + diff --git a/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/Definitions/Input.cs b/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/Definitions/Input.cs index 799456a..21cce39 100644 --- a/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/Definitions/Input.cs +++ b/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/Definitions/Input.cs @@ -1,74 +1,74 @@ using System.ComponentModel; -namespace Frends.SMTP.SendEmail.Definitions +namespace Frends.SMTP.SendEmail.Definitions; + +/// +/// Input parameters. +/// +public class Input { /// - /// Input parameters. + /// Recipient addresses separated by ',' or ';' /// - public class Input - { - /// - /// Recipient addresses separated by ',' or ';' - /// - /// jane.doe@somedomain.com - [DefaultValue("jane.doe@somedomain.com")] - public string To { get; set; } + /// jane.doe@somedomain.com + [DefaultValue("jane.doe@somedomain.com")] + public string To { get; set; } - /// - /// Cc recipient addresses separated by ',' or ';' - /// - /// jane.doe@somedomain.com - [DefaultValue("jane.doe@somedomain.com")] - public string Cc { get; set; } + /// + /// Cc recipient addresses separated by ',' or ';' + /// + /// jane.doe@somedomain.com + [DefaultValue("jane.doe@somedomain.com")] + public string Cc { get; set; } - /// - /// Bcc recipient addresses separated by ',' or ';' - /// - /// jane.doe@somedomain.com - [DefaultValue("jane.doe@somedomain.com")] - public string Bcc { get; set; } + /// + /// Bcc recipient addresses separated by ',' or ';' + /// + /// jane.doe@somedomain.com + [DefaultValue("jane.doe@somedomain.com")] + public string Bcc { get; set; } - /// - /// Sender address. - /// - /// jane.doe@somedomain.com - [DefaultValue("john.doe@somedomain.com")] - public string From { get; set; } + /// + /// Sender address. + /// + /// jane.doe@somedomain.com + [DefaultValue("john.doe@somedomain.com")] + public string From { get; set; } - /// - /// Name of the sender. - /// - /// Jane Doe - [DefaultValue("")] - public string SenderName { get; set; } + /// + /// Name of the sender. + /// + /// Jane Doe + [DefaultValue("")] + public string SenderName { get; set; } - /// - /// Email message's subject. - /// - /// Hello Jane - [DefaultValue("Hello Jane")] - public string Subject { get; set; } + /// + /// Email message's subject. + /// + /// Hello Jane + [DefaultValue("Hello Jane")] + public string Subject { get; set; } - /// - /// Body of the message. - /// - /// You've got mail! - [DefaultValue("You've got mail!")] - public string Message { get; set; } + /// + /// Body of the message. + /// + /// You've got mail! + [DefaultValue("You've got mail!")] + public string Message { get; set; } - /// - /// Set this true if the message is HTML. - /// - /// true - [DefaultValue("false")] - public bool IsMessageHtml { get; set; } + /// + /// Set this true if the message is HTML. + /// + /// true + [DefaultValue("false")] + public bool IsMessageHtml { get; set; } - /// - /// Encoding of message body and subject. Use following table's name column for other options. https://msdn.microsoft.com/en-us/library/system.text.encoding(v=vs.110).aspx#Anchor_5 - /// - /// utf-8 - [DefaultValue("utf-8")] - public string MessageEncoding { get; set; } + /// + /// Encoding of message body and subject. Use following table's name column for other options. https://msdn.microsoft.com/en-us/library/system.text.encoding(v=vs.110).aspx#Anchor_5 + /// + /// utf-8 + [DefaultValue("utf-8")] + public string MessageEncoding { get; set; } - } } + diff --git a/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/Definitions/Options.cs b/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/Definitions/Options.cs index a71f7e1..aae9903 100644 --- a/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/Definitions/Options.cs +++ b/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/Definitions/Options.cs @@ -1,56 +1,55 @@ using System.ComponentModel; using System.ComponentModel.DataAnnotations; -namespace Frends.SMTP.SendEmail.Definitions +namespace Frends.SMTP.SendEmail.Definitions; + +/// +/// SMTP Options parameters. +/// +public class Options { /// - /// SMTP Options parameters. + /// SMTP server address. /// - public class Options - { - /// - /// SMTP server address. - /// - /// smtp.somedomain.com - [DefaultValue("smtp.somedomain.com")] - public string SMTPServer { get; set; } + /// smtp.somedomain.com + [DefaultValue("smtp.somedomain.com")] + public string SMTPServer { get; set; } - /// - /// SMTP server port. - /// - /// 25 - [DefaultValue("25")] - public int Port { get; set; } + /// + /// SMTP server port. + /// + /// 25 + [DefaultValue("25")] + public int Port { get; set; } - /// - /// Set this true if SMTP expects to be connected using SSL. - /// - /// true - [DefaultValue("false")] - public bool UseSsl { get; set; } + /// + /// Set this true if SMTP expects to be connected using SSL. + /// + /// true + [DefaultValue("false")] + public bool UseSsl { get; set; } - /// - /// Set this true if you want to use windows authentication to authenticate to SMTP server. - /// - /// false - [DefaultValue("true")] - public bool UseWindowsAuthentication { get; set; } + /// + /// Set this true if you want to use windows authentication to authenticate to SMTP server. + /// + /// false + [DefaultValue("true")] + public bool UseWindowsAuthentication { get; set; } - /// - /// Use this username to log in to the SMTP server - /// - /// testuser - [DefaultValue("")] - [UIHint(nameof(UseWindowsAuthentication), "", false)] - public string UserName { get; set; } + /// + /// Use this username to log in to the SMTP server + /// + /// testuser + [DefaultValue("")] + [UIHint(nameof(UseWindowsAuthentication), "", false)] + public string UserName { get; set; } - /// - /// Use this password to log in to the SMTP server - /// - /// Password123 - [PasswordPropertyText(true)] - [DefaultValue("")] - [UIHint(nameof(UseWindowsAuthentication), "", false)] - public string Password { get; set; } - } + /// + /// Use this password to log in to the SMTP server + /// + /// Password123 + [PasswordPropertyText(true)] + [DefaultValue("")] + [UIHint(nameof(UseWindowsAuthentication), "", false)] + public string Password { get; set; } } diff --git a/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/Definitions/Result.cs b/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/Definitions/Result.cs index 6839697..799ed7a 100644 --- a/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/Definitions/Result.cs +++ b/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/Definitions/Result.cs @@ -1,26 +1,26 @@ -namespace Frends.SMTP.SendEmail.Definitions +namespace Frends.SMTP.SendEmail.Definitions; + +/// +/// Result class of the Task. +/// +public class Result { - /// - /// Result class of the Task. - /// - public class Result + internal Result(bool emailSent, string status) { - internal Result(bool emailSent, string status) - { - EmailSent = emailSent; - StatusString = status; - } + EmailSent = emailSent; + StatusString = status; + } - /// - /// Value is true if email was sent. - /// - /// true - public bool EmailSent { get; private set; } + /// + /// Value is true if email was sent. + /// + /// true + public bool EmailSent { get; private set; } - /// - /// Contains information about the task's result. - /// - /// Email sent to: john.doe@somedomain.com - public string StatusString { get; private set; } - } + /// + /// Contains information about the task's result. + /// + /// Email sent to: john.doe@somedomain.com + public string StatusString { get; private set; } } + diff --git a/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/Frends.Smtp.SendEmail.csproj b/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/Frends.Smtp.SendEmail.csproj index 6614acc..1572356 100644 --- a/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/Frends.Smtp.SendEmail.csproj +++ b/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/Frends.Smtp.SendEmail.csproj @@ -2,7 +2,7 @@ - netstandard2.0;net6.0 + net6.0 1.1.0 latest Frends diff --git a/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/SendEmailTask.cs b/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/SendEmailTask.cs index 65874f3..a05ca87 100644 --- a/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/SendEmailTask.cs +++ b/Frends.SMTP.SendEmail/Frends.Smtp.SendEmail/SendEmailTask.cs @@ -8,143 +8,141 @@ using System.Threading; using Frends.SMTP.SendEmail.Definitions; -namespace Frends.SMTP.SendEmail +namespace Frends.SMTP.SendEmail; +/// +/// Main class of the Task. +/// +public static class SMTP { /// - /// Main class of the Task. + /// Sends email message with optional attachments. + /// [Documentation](https://tasks.frends.com/tasks/frends-tasks/Frends.SMTP.SendEmail) /// - public static class SMTP + /// Message parameters. + /// Parameters for adding attachments. + /// Connection parameters. + /// Token given by Frends to terminate the Task. + /// Object { bool EmailSent, string StatusString } + public static Result SendEmail([PropertyTab] Input message, [PropertyTab] AttachmentOptions attachments, [PropertyTab] Options SMTPSettings, CancellationToken cancellationToken) { - /// - /// Sends email message with optional attachments. - /// [Documentation](https://tasks.frends.com/tasks/frends-tasks/Frends.SMTP.SendEmail) - /// - /// Message parameters. - /// Parameters for adding attachments. - /// Connection parameters. - /// Token given by Frends to terminate the Task. - /// Object { bool EmailSent, string StatusString } - public static Result SendEmail([PropertyTab] Input message, [PropertyTab] AttachmentOptions attachments, [PropertyTab] Options SMTPSettings, CancellationToken cancellationToken) - { - using var client = InitializeSmtpClient(SMTPSettings); - using var mail = InitializeMailMessage(message, cancellationToken); - if (attachments != null) - foreach (var attachment in attachments.Attachments) - { - cancellationToken.ThrowIfCancellationRequested(); + using var client = InitializeSmtpClient(SMTPSettings); + using var mail = InitializeMailMessage(message, cancellationToken); + if (attachments != null) + foreach (var attachment in attachments.Attachments) + { + cancellationToken.ThrowIfCancellationRequested(); - if (attachment.AttachmentType == AttachmentType.FileAttachment) - { - ICollection allAttachmentFilePaths = GetAttachmentFiles(attachment.FilePath); + if (attachment.AttachmentType == AttachmentType.FileAttachment) + { + ICollection allAttachmentFilePaths = GetAttachmentFiles(attachment.FilePath); - if (attachment.ThrowExceptionIfAttachmentNotFound && allAttachmentFilePaths.Count == 0) - throw new FileNotFoundException($@"The given filepath ""attachment.FilePath"" had no matching files", attachment.FilePath); + if (attachment.ThrowExceptionIfAttachmentNotFound && allAttachmentFilePaths.Count == 0) + throw new FileNotFoundException($@"The given filepath ""attachment.FilePath"" had no matching files", attachment.FilePath); - if (allAttachmentFilePaths.Count == 0 && !attachment.SendIfNoAttachmentsFound) - return new Result(false, $@"No attachments found matching path ""{attachment.FilePath}"". No email sent."); + if (allAttachmentFilePaths.Count == 0 && !attachment.SendIfNoAttachmentsFound) + return new Result(false, $@"No attachments found matching path ""{attachment.FilePath}"". No email sent."); - foreach (var fp in allAttachmentFilePaths) - { - cancellationToken.ThrowIfCancellationRequested(); - mail.Attachments.Add(new System.Net.Mail.Attachment(fp)); - } + foreach (var fp in allAttachmentFilePaths) + { + cancellationToken.ThrowIfCancellationRequested(); + mail.Attachments.Add(new System.Net.Mail.Attachment(fp)); } - - if (attachment.AttachmentType == AttachmentType.AttachmentFromString - && !string.IsNullOrEmpty(attachment.StringAttachment.FileContent)) - mail.Attachments.Add(System.Net.Mail.Attachment.CreateAttachmentFromString - (attachment.StringAttachment.FileContent, attachment.StringAttachment.FileName)); } - client.Send(mail); + if (attachment.AttachmentType == AttachmentType.AttachmentFromString + && !string.IsNullOrEmpty(attachment.StringAttachment.FileContent)) + mail.Attachments.Add(System.Net.Mail.Attachment.CreateAttachmentFromString + (attachment.StringAttachment.FileContent, attachment.StringAttachment.FileName)); + } - return new Result(true, $"Email sent to: {mail.To}"); - } + client.Send(mail); + + return new Result(true, $"Email sent to: {mail.To}"); + } - /// - /// Initializes new SmtpClient with given parameters. - /// - private static SmtpClient InitializeSmtpClient(Options settings) + /// + /// Initializes new SmtpClient with given parameters. + /// + private static SmtpClient InitializeSmtpClient(Options settings) + { + var smtpClient = new SmtpClient { - var smtpClient = new SmtpClient - { - Port = settings.Port, - DeliveryMethod = SmtpDeliveryMethod.Network, - UseDefaultCredentials = settings.UseWindowsAuthentication, - EnableSsl = settings.UseSsl, - Host = settings.SMTPServer - }; + Port = settings.Port, + DeliveryMethod = SmtpDeliveryMethod.Network, + UseDefaultCredentials = settings.UseWindowsAuthentication, + EnableSsl = settings.UseSsl, + Host = settings.SMTPServer + }; - if (!settings.UseWindowsAuthentication && !string.IsNullOrEmpty(settings.UserName)) - smtpClient.Credentials = new NetworkCredential(settings.UserName, settings.Password); + if (!settings.UseWindowsAuthentication && !string.IsNullOrEmpty(settings.UserName)) + smtpClient.Credentials = new NetworkCredential(settings.UserName, settings.Password); - return smtpClient; - } + return smtpClient; + } - /// - /// Initializes new MailMessage with given parameters. Uses default value 'true' for IsBodyHtml - /// - private static MailMessage InitializeMailMessage(Input input, CancellationToken cancellationToken) + /// + /// Initializes new MailMessage with given parameters. Uses default value 'true' for IsBodyHtml + /// + private static MailMessage InitializeMailMessage(Input input, CancellationToken cancellationToken) + { + //split recipients, either by comma or semicolon + var separators = new[] { ',', ';' }; + + string[] recipients = string.IsNullOrEmpty(input.To) + ? Array.Empty() + : input.To.Split(separators, StringSplitOptions.RemoveEmptyEntries); + string[] ccRecipients = string.IsNullOrEmpty(input.Cc) + ? Array.Empty() + : input.Cc.Split(separators, StringSplitOptions.RemoveEmptyEntries); + string[] bccRecipients = string.IsNullOrEmpty(input.Bcc) + ? Array.Empty() + : input.Bcc.Split(separators, StringSplitOptions.RemoveEmptyEntries); + + //Create mail object + var mail = new MailMessage() { - //split recipients, either by comma or semicolon - var separators = new[] { ',', ';' }; - - string[] recipients = string.IsNullOrEmpty(input.To) - ? Array.Empty() - : input.To.Split(separators, StringSplitOptions.RemoveEmptyEntries); - string[] ccRecipients = string.IsNullOrEmpty(input.Cc) - ? Array.Empty() - : input.Cc.Split(separators, StringSplitOptions.RemoveEmptyEntries); - string[] bccRecipients = string.IsNullOrEmpty(input.Bcc) - ? Array.Empty() - : input.Bcc.Split(separators, StringSplitOptions.RemoveEmptyEntries); - - //Create mail object - var mail = new MailMessage() - { - From = new MailAddress(input.From, input.SenderName), - Subject = input.Subject, - Body = input.Message, - IsBodyHtml = input.IsMessageHtml - }; - //Add recipients - foreach (var recipientAddress in recipients) - { - cancellationToken.ThrowIfCancellationRequested(); - mail.To.Add(recipientAddress); - } - //Add CC recipients - foreach (var ccRecipient in ccRecipients) - { - cancellationToken.ThrowIfCancellationRequested(); - mail.CC.Add(ccRecipient); - } - //Add BCC recipients - foreach (var bccRecipient in bccRecipients) - { - cancellationToken.ThrowIfCancellationRequested(); - mail.Bcc.Add(bccRecipient); - } - //Set message encoding - Encoding encoding = Encoding.GetEncoding(input.MessageEncoding); + From = new MailAddress(input.From, input.SenderName), + Subject = input.Subject, + Body = input.Message, + IsBodyHtml = input.IsMessageHtml + }; + //Add recipients + foreach (var recipientAddress in recipients) + { + cancellationToken.ThrowIfCancellationRequested(); + mail.To.Add(recipientAddress); + } + //Add CC recipients + foreach (var ccRecipient in ccRecipients) + { + cancellationToken.ThrowIfCancellationRequested(); + mail.CC.Add(ccRecipient); + } + //Add BCC recipients + foreach (var bccRecipient in bccRecipients) + { + cancellationToken.ThrowIfCancellationRequested(); + mail.Bcc.Add(bccRecipient); + } + //Set message encoding + Encoding encoding = Encoding.GetEncoding(input.MessageEncoding); - mail.BodyEncoding = encoding; - mail.SubjectEncoding = encoding; + mail.BodyEncoding = encoding; + mail.SubjectEncoding = encoding; - return mail; - } + return mail; + } - /// - /// Gets all actual file names of attachments matching given file path - /// - private static ICollection GetAttachmentFiles(string filePath) - { - string folder = Path.GetDirectoryName(filePath); - string fileMask = Path.GetFileName(filePath) != "" ? Path.GetFileName(filePath) : "*"; + /// + /// Gets all actual file names of attachments matching given file path + /// + private static ICollection GetAttachmentFiles(string filePath) + { + string folder = Path.GetDirectoryName(filePath); + string fileMask = Path.GetFileName(filePath) != "" ? Path.GetFileName(filePath) : "*"; - string[] filePaths = Directory.GetFiles(folder, fileMask); + string[] filePaths = Directory.GetFiles(folder, fileMask); - return filePaths; - } + return filePaths; } -} \ No newline at end of file +} diff --git a/Frends.SMTP.SendEmail/README.md b/Frends.SMTP.SendEmail/README.md index 7566635..5a10b64 100644 --- a/Frends.SMTP.SendEmail/README.md +++ b/Frends.SMTP.SendEmail/README.md @@ -2,7 +2,7 @@ Frends task for sending emails with SMTP. Task sends emails via SMTP protocol and can handle attachments either from file or as raw string input. -[![Frends.Smtp.SendEmail Main](https://github.com/FrendsPlatform/Frends.SMTP/actions/workflows/SendEmail_main.yml/badge.svg)](https://github.com/FrendsPlatform/Frends.SMTP/actions) +[![Frends.Smtp.SendEmail Main](https://github.com/FrendsPlatform/Frends.SMTP/actions/workflows/SendEmail_build_and_test_on_main.yml/badge.svg)](https://github.com/FrendsPlatform/Frends.SMTP/actions) ![GitHub](https://img.shields.io/github/license/FrendsPlatform/Frends.SMTP?label=License) ![Coverage](https://app-github-custom-badges.azurewebsites.net/Badge?key=FrendsPlatform/Frends.SMTP/Frends.SMTP.SendEmail|main)