コード例 #1
0
        public void SendMail()
        {
            if (Client == null)
            {
                Log.Error($"Mailgun Email was not sent. Check AppSettings. By: [{From}] to:[{To.Join(", ")}] cc:[{Cc.Join(", ")}] bcc:[{Bcc.Join(", ")}] subject: [{Subject}]");
                return;
            }

            var request = new RestRequest();

            request.Method = Method.POST;

            request.AddParameter("from", From);

            foreach (var to in To)
            {
                request.AddParameter("to", to);
            }

            foreach (var cc in Cc)
            {
                request.AddParameter("cc", cc);
            }

            foreach (var bcc in Bcc)
            {
                request.AddParameter("bcc", bcc);
            }

            request.AddParameter("subject", Subject);

            string baseAttachmentsPath = AppSettings.Get <string>("EmailAttachments");
            var    attachments         = AttachmentsIO.getAttachmentsFromFolder(AttachmentsFolder, "EmailAttachments");

            foreach (var attachment in attachments)
            {
                string filePath = baseAttachmentsPath + attachment.Directory + "\\" + attachment.FileName;
                request.AddFile("attachment", filePath);
            }

            if (!string.IsNullOrWhiteSpace(Template))
            {
                request.AddParameter("template", Template);

                foreach (var param in TemplateParameters)
                {
                    request.AddParameter($"v:{param.Key}", param.Value);
                }
            }
            else
            {
                request.AddParameter("html", Body);
            }

            var response = Client.Execute(request);
            var content  = response.Content;

            if (!response.IsSuccessful)
            {
                throw new Exception(content);
            }

            Log.Info($"Mailgun Email sent by: [{From}] to:[{To.Join(", ")}] cc:[{Cc.Join(", ")}] bcc:[{Bcc.Join(", ")}] subject: [{Subject}]");
        }
コード例 #2
0
ファイル: Email.cs プロジェクト: JFigue27/MDC
        public void SendMail()
        {
            try
            {
                From         = AppSettings.Get <string>("smtp.from");
                FromPassword = AppSettings.Get <string>("smtp.password");

                var smtpServer = AppSettings.Get <string>("smtp.server");
                var smtpPort   = AppSettings.Get <string>("smtp.port");
                var smtpSSL    = AppSettings.Get("smtp.ssl", true);

                if (string.IsNullOrWhiteSpace(smtpServer) || string.IsNullOrWhiteSpace(smtpPort))
                {
                    return;
                }

                smtp = new SmtpClient(smtpServer, int.Parse(smtpPort));

                smtp.EnableSsl             = smtpSSL;
                smtp.DeliveryMethod        = SmtpDeliveryMethod.Network;
                smtp.UseDefaultCredentials = false;
                smtp.Credentials           = new System.Net.NetworkCredential(From, FromPassword);

                MailMessage message = new MailMessage();
                message.From = new MailAddress(From, From);

                foreach (var to in To)
                {
                    message.To.Add(new MailAddress(to));
                }
                foreach (var cc in Cc)
                {
                    message.CC.Add(new MailAddress(cc));
                }
                foreach (var bcc in Bcc)
                {
                    message.Bcc.Add(new MailAddress(bcc));
                }

                message.Subject      = Subject;
                message.IsBodyHtml   = true;
                message.BodyEncoding = System.Text.Encoding.UTF8;

                message.Body = Body;

                string baseAttachmentsPath = AppSettings.Get <string>("EmailAttachments");
                var    attachments         = AttachmentsIO.getAttachmentsFromFolder(AttachmentsFolder, "EmailAttachments");
                foreach (var attachment in attachments)
                {
                    string   filePath = baseAttachmentsPath + attachment.Directory + "\\" + attachment.FileName;
                    FileInfo file     = new FileInfo(filePath);
                    message.Attachments.Add(new System.Net.Mail.Attachment(new FileStream(filePath, FileMode.Open, FileAccess.Read), attachment.FileName));
                }

                smtp.Send(message);

                Log.Info($"SMTP Email sent by: [{From}] to:[{To.Join(", ")}] cc:[{Cc.Join(", ")}] bcc:[{Bcc.Join(", ")}] subject: [{Subject}]");
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }