コード例 #1
0
        /// <summary>
        /// Generate the mail containing invoice information
        /// send the mail to the user
        /// </summary>
        /// <param name="invoice"></param>
        private void MailSender(InvoiceDTO invoice)
        {
            string email = this.Session["Email"].ToString();

            if (email != null)
            {
                //Mail sending procedure
                //Body creation
                AccountBL  accountBL = new AccountBL();
                AccountDTO customer  = accountBL.GetCustomer(email);
                List <ProductSelectionDTO> products = (List <ProductSelectionDTO>)(this.Session["Cart"]);

                //Introduction
                string body = "Hi, " + customer.GetFirstName() + " " + customer.GetLastName() + ",\n\nHere your order of " + invoice.GetOrderDate().ToString("dd/MM/yyyy") + " :\n\n";

                //List of product
                foreach (ProductSelectionDTO p in products)
                {
                    body += p.GetQuantity() + " " + p.GetProduct().GetName() + " " + p.GetOrigSize() + " at " + p.GetOrigPrice() + "/Unit\n";
                }
                //Invoice costs
                body += "\nShipping cost :" + invoice.GetShippingCost() + "AUS$\nTax : " + invoice.GetTax() + "%\nTotal Amount : " + invoice.GetTotal() + " AUS$\nEstimate arrival date : " + invoice.GetArrivalDate().ToString("dd/MM/yyyy");
                body += "\n\nThank you for your confidence.\nHope to see you soon on LaitBrasseur.com !";

                //Message creation (To / From/ link to verification)
                MailMessage mm = new MailMessage();
                mm.To.Add(new MailAddress(email));
                mm.From = new MailAddress("*****@*****.**");
                mm.Body = body;

                mm.IsBodyHtml = false;
                mm.Subject    = "Your order " + invoice.GetOrderDate().ToString("dd/MM/yyyy");

                //SMTP client initialization (gmail with projet address)
                SmtpClient smcl = new SmtpClient();
                smcl.Host        = "smtp.gmail.com";
                smcl.Port        = 587;
                smcl.Credentials = new NetworkCredential("*****@*****.**", "clementjanina");
                smcl.EnableSsl   = true;
                smcl.Send(mm);

                //Feedback
                lblResult.CssClass = "text-success";
                lblResult.Text    += "A confirmation email has been sent.";
            }
            else
            {
                lblResult.Text = "There is a problem with your email.";
            }
        }