コード例 #1
0
        public async System.Threading.Tasks.Task <ActionResult> SendInvoiceByMailAsync(int InvoiceID)
        {
            Invoice inv  = new InvoiceRepository().GetInvoiceByID(InvoiceID);
            var     body = System.IO.File.ReadAllText(Server.MapPath(@"~\Content\EmailTemplate.cshtml"));

            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            SmtpClient smtpClient           = new SmtpClient();

            try
            {
                msg.From = new MailAddress("*****@*****.**");
                msg.To.Add(new MailAddress(inv.Customer.Email));
                msg.Subject    = "Nota: " + inv.ID + " voor de bijeenkomst op: " + inv.MeetDate.ToString("dd/MM/yyyy");
                msg.Body       = string.Format(body, inv.Customer.Name, inv.MeetDate.ToString("dd/MM/yyyy"), inv.ID);
                msg.IsBodyHtml = true;
                MemoryStream strm = new MemoryStream(((ViewAsPdf)GetPDFInvoice(inv.ID)).BuildPdf(ControllerContext));
                System.Net.Mail.Attachment pdfatt = new System.Net.Mail.Attachment(strm, InvoiceID + ".pdf", "application/pdf");
                msg.Attachments.Add(pdfatt);
                smtpClient.Host        = "smtp.gmail.com";
                smtpClient.Port        = 587;
                smtpClient.Credentials = new NetworkCredential("*****@*****.**", "tsrhcbrixvjnbgza");
                smtpClient.EnableSsl   = true;
                await smtpClient.SendMailAsync(msg);

                IInvoiceRepository InvRepo = new InvoiceRepository();
                InvRepo.ChangeInvoiceStatus(InvoiceID, "Sent");
            }
            catch (Exception e)
            {
                throw e;
            }

            return(RedirectToAction("Invoicing"));
        }