コード例 #1
0
        public void SendMail(String strBody, int strQuoteID, String strClientEmail, string fromEmailRep = "")
        {
            String ContactEmail = String.Empty;

            ContactEmail = quoteDAL.getContactPersonEmail(strQuoteID);
            var comId = quoteDAL.getCompayID(strQuoteID);

            String theName  = String.Empty;
            String theEmail = String.Empty;

            List <String> SplitString = ContactEmail.Split('|').ToList();

            theName  = SplitString[1];
            theEmail = SplitString[0];
            var fromInfo = "*****@*****.**";

            if (!string.IsNullOrEmpty(fromEmailRep))
            {
                fromInfo = fromEmailRep;
            }

            var fromAddress = new MailAddress("*****@*****.**", "DELTONE SOLUTIONS PTY LTD");
            var toAddress   = new MailAddress(theEmail, theName);
            var BccAddress  = new MailAddress("*****@*****.**", "EMAIL SENT TO CUSTOMER");



            const String fromPassword     = "******";
            var          netWorkCrdential = new NetworkCredential("*****@*****.**", fromPassword);


            String Subject = "DELTONE QUOTATION #: " + strQuoteID;

            var EmailFooter = quoteDAL.getEmailFooterText(strQuoteID);

            if (EmailFooter.Contains("To confirm the order"))
            {
                Subject = "CONFIRMATION OF ORDER #: " + strQuoteID;
            }

            String imgView      = "<img src=\"cid:companylogo\" height='80' width='780'>";
            String bottomBanner = "<img src=\"cid:bottombanner\" height='105' width='550'>";
            String body         = HTMLBody(strQuoteID, imgView, bottomBanner);

            AlternateView  avHTML    = AlternateView.CreateAlternateViewFromString(body, null, MediaTypeNames.Text.Html);
            LinkedResource logo      = new LinkedResource("C:\\temp\\DeltoneCRM\\DeltoneCRM\\Images\\top-banner-email-780.png");
            LinkedResource btmbanner = new LinkedResource("C:\\temp\\DeltoneCRM\\DeltoneCRM\\Images\\bottom-banner-email.jpg");

            logo.ContentId      = "companylogo";
            btmbanner.ContentId = "bottombanner";
            avHTML.LinkedResources.Add(logo);
            avHTML.LinkedResources.Add(btmbanner);

            try
            {
                var smtp = new SmtpClient
                {
                    Host                  = "smtp.office365.com",
                    Port                  = 587,
                    EnableSsl             = true,
                    DeliveryMethod        = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = false,
                    Credentials           = netWorkCrdential
                };
                //  var attachements = new List<Attachment>();
                MailMessage deltonemail = new MailMessage(fromAddress, toAddress);
                deltonemail.Subject    = Subject;
                deltonemail.IsBodyHtml = true;
                deltonemail.Body       = body;
                deltonemail.Bcc.Add(BccAddress);

                string fileInvoicePath = "C:\\inetpub\\wwwroot\\DeltoneCRM\\CompanyUploads\\Quotes\\" + comId + "\\" + strQuoteID.ToString() + "\\";
                // string fileInvoicePath = "C:\\WebProjects\\DeltoneCRM-GIT\\DeltoneCRM\\CompanyUploads\\Quotes\\" + comId + "\\" + strQuoteID.ToString() + "\\";

                //if (Directory.Exists(fileInvoicePath))
                //{
                //    DirectoryInfo diSource = new DirectoryInfo(fileInvoicePath);
                //    foreach (FileInfo fi in diSource.GetFiles())
                //    {
                //        var comFilePath = fileInvoicePath + fi.Name;
                //        if (File.Exists(comFilePath))
                //        {
                //            System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(comFilePath);
                //            attachment.Name = fi.Name;
                //            attachements.Add(attachment);
                //        }
                //    }
                //}

                //if (attachements != null)
                //    if (attachements.Count() > 0)
                //        foreach (var item in attachements)
                //            deltonemail.Attachments.Add(item);

                deltonemail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess | DeliveryNotificationOptions.OnFailure | DeliveryNotificationOptions.Delay;

                deltonemail.AlternateViews.Add(avHTML);

                smtp.Send(deltonemail);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString() + " : " + ex.StackTrace.ToString());
            }
        }