コード例 #1
0
        public static void SendEmail(OrderCollection orderCollection)
        {
            EmailContextInformation emailContextInformation = new EmailContextInformation();
            MailAddress             fromAddress             = new MailAddress(emailContextInformation.EmailAddress, "T-Tek Product Team");

            string fromPassword = emailContextInformation.Password;


            var smtp = new SmtpClient
            {
                Host                  = "smtp.gmail.com",
                Port                  = 587,
                EnableSsl             = true,
                DeliveryMethod        = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials           = new NetworkCredential(fromAddress.Address, fromPassword)
            };

            foreach (Order order in orderCollection)
            {
                MailAddress toAddress = new MailAddress("*****@*****.**", "Customer");
                string      subject   = String.Format(emailContextInformation.Subject, order.Item.Title);
                string      body      = String.Format(emailContextInformation.Body, order.Item.ASIN);
                using (var message = new MailMessage(fromAddress, toAddress)
                {
                    Subject = subject,
                    Body = body
                })
                {
                    smtp.Send(message);
                }
            }
        }
コード例 #2
0
        public static void SendEmailByOrderCollection(ServiceCliamDefinition serviceCliamDefinition, OrderCollection orderCollection, TextBox textBox)
        {
            textBox.Text = String.Empty;
            EmailContextInformation emailContextInformation = new EmailContextInformation();
            MailAddress             fromAddress             = new MailAddress(emailContextInformation.EmailAddress, "T-Tek Product Team");

            string fromPassword = emailContextInformation.Password;


            var smtp = new SmtpClient
            {
                Host                  = "smtp.gmail.com",
                Port                  = 587,
                EnableSsl             = true,
                DeliveryMethod        = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials           = new NetworkCredential(fromAddress.Address, fromPassword),
                Timeout               = 3000
            };

            foreach (Order order in orderCollection)
            {
                textBox.Text += String.Format("Sending email to {0}...\n", order.Email);
                MailAddress   toAddress = new MailAddress(order.Email);
                string        subject   = emailContextInformation.GetEmailSubjectContext(order);
                string        body      = emailContextInformation.GetEmailBodyContext(serviceCliamDefinition, order);
                AlternateView avHtml    = AlternateView.CreateAlternateViewFromString(body, null, MediaTypeNames.Text.Html);

                using (var message = new MailMessage(fromAddress, toAddress)
                {
                    Subject = subject,
                })
                {
                    message.AlternateViews.Add(avHtml);
                    message.IsBodyHtml = true;
                    smtp.Send(message);

                    textBox.Text += String.Format("Success!\n");
                }
            }
        }