|
| 1 | +// <copyright file="EMailPlugin.cs" company="SoluiNet"> |
| 2 | +// Copyright (c) SoluiNet. All rights reserved. |
| 3 | +// </copyright> |
| 4 | + |
| 5 | +namespace SoluiNet.DevTools.Communication.EMail |
| 6 | +{ |
| 7 | + using System; |
| 8 | + using System.Collections.Generic; |
| 9 | + using System.Diagnostics.CodeAnalysis; |
| 10 | + using System.Globalization; |
| 11 | + using System.Linq; |
| 12 | + using System.Net; |
| 13 | + using System.Text; |
| 14 | + using System.Threading; |
| 15 | + using System.Threading.Tasks; |
| 16 | + using MailKit.Net.Smtp; |
| 17 | +#if BUILD_FOR_WINDOWS |
| 18 | + using System.Windows.Controls; |
| 19 | +#endif |
| 20 | + using Microsoft.AspNetCore.Builder; |
| 21 | + using Microsoft.AspNetCore.Hosting; |
| 22 | + using Microsoft.Extensions.Configuration; |
| 23 | + using Microsoft.Extensions.DependencyInjection; |
| 24 | + using MimeKit; |
| 25 | + using NLog; |
| 26 | + using RestSharp; |
| 27 | + using RestSharp.Authenticators; |
| 28 | + using SoluiNet.DevTools.Core; |
| 29 | + using SoluiNet.DevTools.Core.Application; |
| 30 | + using SoluiNet.DevTools.Core.Exceptions; |
| 31 | + using SoluiNet.DevTools.Core.Plugin; |
| 32 | + using SoluiNet.DevTools.Core.Plugin.Events; |
| 33 | + using SoluiNet.DevTools.Core.Tools; |
| 34 | + using SoluiNet.DevTools.Core.Tools.Plugin; |
| 35 | + using SoluiNet.DevTools.Core.Tools.String; |
| 36 | + using SoluiNet.DevTools.Core.UI.Blazor.Plugin; |
| 37 | +#if BUILD_FOR_WINDOWS |
| 38 | + using SoluiNet.DevTools.Core.UI.WPF.Plugin; |
| 39 | +#endif |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// A plugin which provides data exchange methods for E-Mails. |
| 43 | + /// </summary> |
| 44 | + public class EMailPlugin : IAllowsDataExchange, IContainsSettings, IUtilitiesDevPlugin, IHandlesEvent<IInitializedEvent>, |
| 45 | + ICommunicationReceiver, ICommunicationSender |
| 46 | + { |
| 47 | + /// <summary> |
| 48 | + /// Initializes a new instance of the <see cref="EMailPlugin"/> class. |
| 49 | + /// </summary> |
| 50 | + public EMailPlugin() |
| 51 | + { |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Gets the technical name of the plugin. |
| 56 | + /// </summary> |
| 57 | + public string Name |
| 58 | + { |
| 59 | + get |
| 60 | + { |
| 61 | + return "EMail"; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Gets the label for the menu. |
| 67 | + /// </summary> |
| 68 | + public string MenuItemLabel |
| 69 | + { |
| 70 | + get { return "EMail"; } |
| 71 | + } |
| 72 | + |
| 73 | + /// <inheritdoc/> |
| 74 | + public string CommunicationChannel |
| 75 | + { |
| 76 | + get |
| 77 | + { |
| 78 | + return "EMail"; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /// <inheritdoc/> |
| 83 | + public ICollection<string> SupportedCommunicationEntities |
| 84 | + { |
| 85 | + get |
| 86 | + { |
| 87 | + return new List<string>() { "Message" }; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /// <inheritdoc/> |
| 92 | + public Dictionary<string, ICollection<object>> Resources |
| 93 | + { |
| 94 | + get |
| 95 | + { |
| 96 | + throw new NotImplementedException(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Gets the logger. |
| 102 | + /// </summary> |
| 103 | + private static Logger Logger |
| 104 | + { |
| 105 | + get |
| 106 | + { |
| 107 | + return LogManager.GetCurrentClassLogger(); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | +#if BUILD_FOR_WINDOWS |
| 112 | + /// <summary> |
| 113 | + /// Call this method if the plugin should be displayed. |
| 114 | + /// </summary> |
| 115 | + /// <param name="displayInPluginContainer">The delegate which should be called for displaying the plugin.</param> |
| 116 | + public void Execute(Action<UserControl> displayInPluginContainer) |
| 117 | + { |
| 118 | + if (displayInPluginContainer == null) |
| 119 | + { |
| 120 | + throw new ArgumentNullException(nameof(displayInPluginContainer)); |
| 121 | + } |
| 122 | + |
| 123 | + displayInPluginContainer(new EMailUserControl()); |
| 124 | + } |
| 125 | +#endif |
| 126 | + |
| 127 | + /// <summary> |
| 128 | + /// Get data from E-Mail. |
| 129 | + /// </summary> |
| 130 | + /// <param name="entityName">The type of the message.</param> |
| 131 | + /// <param name="searchData">The search parameters in a dictionary. All parameters will be combined via AND.</param> |
| 132 | + /// <returns>Returns a list of issues that are matching the overgiven parameters.</returns> |
| 133 | + public ICollection<object> GetData(string entityName, IDictionary<string, object> searchData) |
| 134 | + { |
| 135 | + throw new NotImplementedException(); |
| 136 | + } |
| 137 | + |
| 138 | + /// <summary> |
| 139 | + /// Get data from E-Mail. |
| 140 | + /// </summary> |
| 141 | + /// <param name="whereClause">The where clause.</param> |
| 142 | + /// <returns>Returns a list of issues that are matching the overgiven parameters.</returns> |
| 143 | + public ICollection<object> GetData(string whereClause) |
| 144 | + { |
| 145 | + throw new NotImplementedException(); |
| 146 | + } |
| 147 | + |
| 148 | + /// <summary> |
| 149 | + /// Get data by key. |
| 150 | + /// </summary> |
| 151 | + /// <param name="key">The key.</param> |
| 152 | + /// <returns>Returns a <see cref="object"/> for the requested key.</returns> |
| 153 | + /// <exception cref="NotImplementedException">Throws a <see cref="NotImplementedException"/> until this method has been implemented.</exception> |
| 154 | + public object GetDataByKey(string key) |
| 155 | + { |
| 156 | + throw new NotImplementedException(); |
| 157 | + } |
| 158 | + |
| 159 | + /// <summary> |
| 160 | + /// Get data for general purposes. |
| 161 | + /// </summary> |
| 162 | + /// <returns>Returns a dictionary of data for general purposes.</returns> |
| 163 | + /// <exception cref="NotImplementedException">Throws a <see cref="NotImplementedException"/> because this method won't be needed for Telegram data exchange.</exception> |
| 164 | + public IDictionary<string, object> GetGeneralData() |
| 165 | + { |
| 166 | + throw new NotImplementedException(); |
| 167 | + } |
| 168 | + |
| 169 | + /// <summary> |
| 170 | + /// Handle the event. |
| 171 | + /// </summary> |
| 172 | + /// <param name="eventArgs">The event arguments.</param> |
| 173 | + /// <typeparam name="T">The event type.</typeparam> |
| 174 | + public void HandleEvent<T>(Dictionary<string, object> eventArgs) |
| 175 | + where T : IEventType |
| 176 | + { |
| 177 | + if (typeof(T).IsAssignableFrom(typeof(IInitializedEvent))) |
| 178 | + { |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + /// <inheritdoc /> |
| 183 | + /// <exception cref="NotImplementedException">Throws a <see cref="NotImplementedException"/> until the functionality has been implemented.</exception> |
| 184 | + public void Receive(string entity = "") |
| 185 | + { |
| 186 | + throw new NotImplementedException(); |
| 187 | + } |
| 188 | + |
| 189 | + /// <inheritdoc /> |
| 190 | + /// <exception cref="NotImplementedException">Throws a <see cref="NotImplementedException"/> until the functionality has been implemented.</exception> |
| 191 | + /// <exception cref="ArgumentNullException">Throws an <see cref="ArgumentNullException"/> if the message is null or empty.</exception> |
| 192 | + public void Send(string message, string entity = "", string receiver = "", Dictionary<string, object> additionalParameters = null) |
| 193 | + { |
| 194 | + if (string.IsNullOrEmpty(message)) |
| 195 | + { |
| 196 | + throw new ArgumentNullException(nameof(message)); |
| 197 | + } |
| 198 | + |
| 199 | + if (string.IsNullOrEmpty(entity)) |
| 200 | + { |
| 201 | + entity = "Message"; |
| 202 | + } |
| 203 | + |
| 204 | + var mailMessage = new MimeMessage(); |
| 205 | + |
| 206 | + try |
| 207 | + { |
| 208 | + var sender = ApplicationContext.Configuration.PluginConfiguration.GetByKey("EMailSender").ToString(); |
| 209 | + |
| 210 | + mailMessage.From.Add( |
| 211 | + new MailboxAddress( |
| 212 | + string.IsNullOrEmpty(sender) ? "solui.net" : sender, |
| 213 | + ApplicationContext.Configuration.PluginConfiguration.GetByKey("EMailSenderAddress").ToString())); |
| 214 | + mailMessage.To.Add( |
| 215 | + new MailboxAddress( |
| 216 | + receiver, |
| 217 | + receiver)); |
| 218 | + |
| 219 | + mailMessage.Subject = additionalParameters != null && additionalParameters.ContainsKey("Subject") |
| 220 | + ? additionalParameters["Subject"].ToString() |
| 221 | + : "Info"; |
| 222 | + |
| 223 | + mailMessage.Body = new TextPart("plain") |
| 224 | + { |
| 225 | + Text = message, |
| 226 | + }; |
| 227 | + |
| 228 | + using (var smtpClient = new SmtpClient()) |
| 229 | + { |
| 230 | + smtpClient.Connect( |
| 231 | + ApplicationContext.Configuration.PluginConfiguration.GetByKey("EMailSender").ToString(), |
| 232 | + Convert.ToInt32(ApplicationContext.Configuration.PluginConfiguration.GetByKey("EMailPort"), CultureInfo.InvariantCulture), |
| 233 | + true); |
| 234 | + |
| 235 | + smtpClient.Authenticate( |
| 236 | + ApplicationContext.Configuration.PluginConfiguration.GetByKey("EMailSenderAddress").ToString(), |
| 237 | + ApplicationContext.Configuration.PluginConfiguration.GetByKey("EMailSenderPassword").ToString()); |
| 238 | + |
| 239 | + smtpClient.Send(mailMessage); |
| 240 | + |
| 241 | + smtpClient.Disconnect(true); |
| 242 | + } |
| 243 | + } |
| 244 | + finally |
| 245 | + { |
| 246 | + mailMessage.Dispose(); |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + /// <summary> |
| 251 | + /// Set data in E-Mail. |
| 252 | + /// </summary> |
| 253 | + /// <param name="identifier">The identifier of the message.</param> |
| 254 | + /// <param name="valueData">The properties which should be changed.</param> |
| 255 | + /// <returns>Returns the changed issue.</returns> |
| 256 | + public object SetData(object identifier, IDictionary<string, object> valueData) |
| 257 | + { |
| 258 | + throw new NotImplementedException(); |
| 259 | + } |
| 260 | + |
| 261 | + /// <inheritdoc/> |
| 262 | + public void ConfigureServices(IServiceCollection services, IConfiguration configuration) |
| 263 | + { |
| 264 | + throw new NotImplementedException(); |
| 265 | + } |
| 266 | + |
| 267 | + /// <inheritdoc/> |
| 268 | + public void Configure(IApplicationBuilder app, IWebHostEnvironment environment) |
| 269 | + { |
| 270 | + throw new NotImplementedException(); |
| 271 | + } |
| 272 | + } |
| 273 | +} |
0 commit comments