public static MailMessage CreateFrameworkMailMessage([NotNull] this ISmtpEmailMessage source)
        {
            var target = new MailMessage {
                BodyEncoding = Encoding.UTF8, SubjectEncoding = Encoding.UTF8
            };

            var(success, displayName, mailAddress) = MailAddressParser.TryParse(source.From);
            if (!success)
            {
                throw new InvalidOperationException("Unable to assign 'From' address for smtp message, missing or invalid value");
            }

            target.From = new MailAddress(displayName, mailAddress, Encoding.UTF8);

            foreach (var to in source.To)
            {
                (success, displayName, mailAddress) = MailAddressParser.TryParse(to);
                if (!success)
                {
                    throw new InvalidOperationException("Unable to assign 'To' addresses for smtp message, missing or invalid value");
                }

                target.To.Add(new MailAddress(displayName, mailAddress, Encoding.UTF8));
            }

            target.Subject = source.Subject;
            target.Body    = source.Body;

            return(target);
        }
示例#2
0
        public async Task SendEmailMessageAsync(ISmtpEmailMessage emailMessage)
        {
            var configuration = _Configuration.Value();

            if (configuration == null || !configuration.IsValid())
            {
                throw new InvalidOperationException("Smtp configuration not present or valid");
            }

            assume(configuration.Server?.Address != null);
            assume(configuration.Authentication != null);

            MailMessage mm = emailMessage.CreateFrameworkMailMessage();

            using (_Logger.LogScope(
                       LogLevel.Debug,
                       $"Sending email with subject '{mm.Subject}' from '{mm.From}' to '{string.Join(", ", mm.To)}' via '{configuration.Server.Address}:{configuration.Server.Port}'")
                   )
            {
                using (var client = new System.Net.Mail.SmtpClient(configuration.Server.Address, configuration.Server.Port))
                {
                    client.Credentials = new NetworkCredential(
                        configuration.Authentication.Username, configuration.Authentication.Password);

                    var tcs = new TaskCompletionSource <bool>();

                    void onClientOnSendCompleted(object sender, AsyncCompletedEventArgs args)
                    {
                        assume(args != null);
                        if (args.Error != null)
                        {
                            tcs.SetException(args.Error);
                        }
                        else if (args.Cancelled)
                        {
                            tcs.SetCanceled();
                        }
                        else
                        {
                            tcs.SetResult(true);
                        }
                    }

                    client.SendCompleted += onClientOnSendCompleted;
                    client.SendAsync(mm, null);
                    await tcs.Task.NotNull();
                }
            }
        }