Beispiel #1
0
        public async Task SendAsync(MailMessageDTO message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            // Get MIME message
            var msg = message.ToMimeMessage();

            // Send message
            using (var mx = new SmtpClient())
            {
                await mx.ConnectAsync(this.HostName, this.Port, AllowSsl?SecureSocketOptions.Auto : SecureSocketOptions.None);

                if (AllowSsl)
                {
                    mx.ServerCertificateValidationCallback = this.ServerCertificateValidationCallback;
                }
                if (!string.IsNullOrEmpty(this.UserName) && !string.IsNullOrEmpty(this.Password))
                {
                    await mx.AuthenticateAsync(this.UserName, this.Password);
                }
                await mx.SendAsync(msg);

                await mx.DisconnectAsync(true);
            }
        }
        public async Task SendAsync(MailMessageDTO message)
        {
            var sb = new StringBuilder();

            sb.AppendLine("From: " + message.From);
            sb.AppendLine("To: " + string.Join("; ", message.To));
            sb.AppendLine("CC: " + string.Join("; ", message.Cc));
            sb.AppendLine("BCC: " + string.Join("; ", message.Bcc));
            sb.AppendLine("Reply-To: " + string.Join("; ", message.ReplyTo));
            sb.AppendLine("Subject: " + message.Subject);
            sb.AppendLine();
            sb.AppendLine(message.BodyHtml);
            sb.AppendLine();
            sb.AppendLine(message.BodyText);

            var fileName = Path.Combine(directory, Guid.NewGuid() + ".txt");

            using (var fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write))
            {
                using (var sw = new StreamWriter(fs, Encoding.UTF8))
                {
                    await sw.WriteAsync(sb.ToString());
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Sends an e-mail message to a specified recipients.
        /// </summary>
        public Task SendMailAsync(string[] to, string subject, string body,
                                  IEnumerable <string> ccAddresses        = null,
                                  IEnumerable <string> bccAddresses       = null,
                                  IEnumerable <string> replyToAddresses   = null,
                                  IEnumerable <AttachmentDTO> attachments = null
                                  )
        {
            var message = new MailMessageDTO();

            foreach (var recipient in to)
            {
                message.To.Add(new MailAddressDTO(recipient));
            }
            message.Subject  = subject;
            message.BodyHtml = body;

            if (ccAddresses != null)
            {
                foreach (var address in ccAddresses)
                {
                    message.Cc.Add(new MailAddressDTO(address));
                }
            }
            if (bccAddresses != null)
            {
                foreach (var address in bccAddresses)
                {
                    message.Bcc.Add(new MailAddressDTO(address));
                }
            }
            if (replyToAddresses != null)
            {
                foreach (var address in replyToAddresses)
                {
                    message.ReplyTo.Add(new MailAddressDTO(address));
                }
            }
            if (attachments != null)
            {
                foreach (var attachment in attachments)
                {
                    message.Attachments.Add(attachment);
                }
            }

            return(SendMailAsync(message));
        }
Beispiel #4
0
        public async Task SendAsync(MailMessageDTO message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            // Convert to message
            var msg = message.ToMimeMessage();

            // Write to temp file to avoid pickup of incomplete message
            var tempFileName = Path.GetTempFileName();

            using (var sw = File.CreateText(tempFileName))
            {
                // Write envelope sender
                await sw.WriteLineAsync($"X-Sender: <{message.From.Address}>");

                // Write envelope receivers
                var receivers = message.To
                                .Union(message.Cc)
                                .Union(message.Bcc);
                foreach (var item in receivers.Select(x => x.Address))
                {
                    await sw.WriteLineAsync($"X-Receiver: <{item}>");
                }

                // Flush data and write rest of message
                await sw.FlushAsync();

                msg.WriteTo(sw.BaseStream);
            }

            var msgFileName = Path.Combine(FolderName, string.Join(".", DateTime.Now.ToString("yyyyMMdd-HHmmss"), Guid.NewGuid().ToString("N"), "eml"));

            // Create final directory if not exists
            if (!Directory.Exists(FolderName))
            {
                Directory.CreateDirectory(FolderName);
            }

            // Move the file to final destination
            File.Move(tempFileName, msgFileName);
        }
Beispiel #5
0
        public async Task SendAsync(MailMessageDTO message)
        {
            using (var smtpMessage = new MailMessage())
            {
                if (message.From != null)
                {
                    smtpMessage.From = ConvertAddress(message.From);
                }
                foreach (var to in message.To)
                {
                    smtpMessage.To.Add(ConvertAddress(to));
                }
                foreach (var cc in message.Cc)
                {
                    smtpMessage.CC.Add(ConvertAddress(cc));
                }
                foreach (var bcc in message.Bcc)
                {
                    smtpMessage.Bcc.Add(ConvertAddress(bcc));
                }
                foreach (var replyTo in message.ReplyTo)
                {
                    smtpMessage.ReplyToList.Add(ConvertAddress(replyTo));
                }
                smtpMessage.Subject = message.Subject;
                if (!string.IsNullOrEmpty(message.BodyHtml))
                {
                    smtpMessage.Body       = message.BodyHtml;
                    smtpMessage.IsBodyHtml = true;
                }
                else
                {
                    smtpMessage.Body       = message.BodyText;
                    smtpMessage.IsBodyHtml = false;
                }

                foreach (var attachment in message.Attachments)
                {
                    smtpMessage.Attachments.Add(ConvertAttachment(attachment));
                }

                await client.SendMailAsync(smtpMessage);
            }
        }
Beispiel #6
0
        public static MimeMessage ToMimeMessage(this MailMessageDTO dto)
        {
            var msg = new MimeMessage();

            // Add standard header fields
            msg.From.Add(dto.From.ToMailboxAddress());
            msg.To.AddRange(dto.To.ToMailboxAddress());
            msg.Cc.AddRange(dto.Cc.ToMailboxAddress());
            msg.Bcc.AddRange(dto.Bcc.ToMailboxAddress());
            msg.Sender = dto.Sender.ToMailboxAddress();
            msg.ReplyTo.AddRange(dto.ReplyTo.ToMailboxAddress());
            msg.Subject = dto.Subject;

            // Add custom header fields
            foreach (var item in dto.CustomHeaders)
            {
                msg.Headers.Add(item.Key, item.Value);
            }

            // Construct body
            var bb = new BodyBuilder
            {
                TextBody = dto.BodyText,
                HtmlBody = dto.BodyHtml
            };

            // Add attachments
            foreach (var item in dto.Attachments)
            {
                ContentType ct;
                var         r = ContentType.TryParse(item.MimeType, out ct);
                if (!r)
                {
                    ct = new ContentType("application", "octet-stream");
                }
                bb.Attachments.Add(item.Name, item.Stream, ct);
            }

            msg.Body = bb.ToMessageBody();
            return(msg);
        }
Beispiel #7
0
        /// <summary>
        /// Sends a specified e-mail message.
        /// </summary>
        public async Task SendMailAsync(MailMessageDTO message)
        {
            if (From != null)
            {
                message.From = From;
            }
            if (!string.IsNullOrEmpty(SubjectFormatString))
            {
                message.Subject = string.Format(SubjectFormatString, message.Subject);
            }
            if (!string.IsNullOrEmpty(BodyTextFormatString) && !string.IsNullOrEmpty(message.BodyText))
            {
                message.BodyText = string.Format(BodyTextFormatString, message.BodyText);
            }
            if (!string.IsNullOrEmpty(BodyHtmlFormatString) && !string.IsNullOrEmpty(message.BodyHtml))
            {
                message.BodyHtml = string.Format(BodyHtmlFormatString, message.BodyHtml);
            }

            if (OverrideToAddresses != null && OverrideToAddresses.Any())
            {
                message.To.Clear();
                message.Cc.Clear();
                message.Bcc.Clear();

                foreach (var address in OverrideToAddresses)
                {
                    message.To.Add(address);
                }
            }

            var sendingArgs = new MessageSendingEventArgs(message);

            OnMessageSending(sendingArgs);

            if (!sendingArgs.Cancel)
            {
                await sender.SendAsync(sendingArgs.Message);
            }
        }