Пример #1
0
        public async void SendEmail(Order order)
        {
            var orders = await _instrumentRepository.GetOrdersByOrderNumberAsync(order.OrderNumber);

            if (orders == null)
            {
                return;
            }

            var          fromAddress  = "*****@*****.**";
            var          toAddress    = orders[0].Email;
            const string fromPassword = "******";
            string       subject      = $"Bill - #{orders[0].OrderNumber}";

            string body = $"Thank you for your purchase, {orders[0].FirstName} {orders[0].LastName}!\n\n" +
                          $"Billing City: {orders[0].BillingCity}\nBilling State: {orders[0].BillingState}\nBilling Postal Code: {orders[0].BillingPostalCode}\n" +
                          $"Billing Address: {orders[0].BillingAddress}\n\nHere is your list of order: \n\n";

            int total = 0;

            foreach (var item in orders)
            {
                body  += $"Instrument: {item.InstrumentName}, Instrument Code: {item.Code}, Quantity: {item.Quantity}, Price: {item.Price.ToString("N0", new CultureInfo("hu-HU"))} Ft\n";
                total += item.Price;
            }
            body += $"\nTotal: {total.ToString("N0", new CultureInfo("hu-HU"))} Ft\n\nBest regards,\nWebshop team";

            SmtpClient Client = new SmtpClient()
            {
                Host                  = "smtp.gmail.com",
                Port                  = 587,
                EnableSsl             = true,
                DeliveryMethod        = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials           = new NetworkCredential()
                {
                    UserName = fromAddress,
                    Password = fromPassword
                }
            };
            MailAddress FromEmail = new MailAddress(fromAddress, "Webshop");
            MailAddress ToEmail   = new MailAddress(toAddress, orders[0].FirstName + " " + orders[0].LastName);

            MailMessage Message = new MailMessage()
            {
                From    = FromEmail,
                Subject = subject,
                Body    = body
            };

            Message.To.Add(ToEmail);

            try
            {
                Client.Send(Message);
                MessageBox.Show("Sent Successfully", "Done");
            }
            catch (Exception e)
            {
                MessageBox.Show("Something went wrong \n" + e.InnerException.Message, "Error");
            }
        }