• Top Posts

    Send emails using gmail account and C# with HTML body


      public static void SendVendorConformed(string ToEmail, string VendorName,string TemplateUrl)  
         {  
           string config_email = "frommail";  
           string config_password = "password";  
           string config_adminemail = ToEmail;  
           var fromAddress = new MailAddress(config_email, "Title");  
           var toAddress = new MailAddress(ToEmail, VendorName);  
           var toAddressDefault = new MailAddress(config_adminemail, "Title");  
           string subject = "Rupdarshi";  
           //StreamReader str = new StreamReader(TemplateUrl);  
           string MailText = File.ReadAllText(TemplateUrl);  
           //MailText = MailText.Replace("{{name}}!", VendorName);  
           MailText = MailText.Replace("{{name}}", VendorName);  
           MailText = MailText.Replace("{{username}}", VendorName);  
           MailText = MailText.Replace("{{email}}", ToEmail);  
           //str.Close();  
           var smtp = new SmtpClient  
           {  
             Host = "smtp.gmail.com",  
             Port = 587,  
             EnableSsl = true,  
             DeliveryMethod = SmtpDeliveryMethod.Network,  
             UseDefaultCredentials = false,  
             Credentials = new NetworkCredential(config_email, config_password)  
           };  
           using (var message = new MailMessage(fromAddress, toAddress)  
           {  
             Subject = subject,  
             IsBodyHtml = true,  
             Body = MailText,  
           })  
           {  
             smtp.Send(message);  
           }  
           using (var message = new MailMessage(fromAddress, toAddressDefault)  
           {  
             Subject = subject,  
             IsBodyHtml = true,  
             Body = MailText,  
           })  
           {  
             smtp.Send(message);  
           }  
         }  
    

    No comments

    Post Top Ad

    ad728

    Post Bottom Ad

    ad728