locked
c# How to send html email RRS feed

  • Question

  • Dear Exprt,

    I have HTML template for email, how can i send this template using c# on button


    Support@Mytechnet.me

    Monday, April 15, 2013 11:08 PM

Answers

  • Hello,

    There is an example, on how to do that:

    using System;
    using System.Windows.Forms;
    using System.Net.Mail;
    
    namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    MailMessage mail = new MailMessage();
                    SmtpClient SmtpServer = new SmtpClient("xxx");
    
                    mail.From = new MailAddress(xxx@xxx.xxx);
                    mail.To.Add("yyy");
                    mail.Subject = "Send HTML Template";
    
                    mail.IsBodyHtml = true;
                    string htmlBody;
    
                    htmlBody = "Template HTML here";
    
                    mail.Body = htmlBody;
    
                    SmtpServer.Port = 25;
                    SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
                    SmtpServer.EnableSsl = true;
    
                    SmtpServer.Send(mail);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
        }
    }

    João Sousa (MCTS) Senior Software Engineer

    • Proposed as answer by Carsten Siemens Wednesday, April 17, 2013 4:39 PM
    • Marked as answer by Bob Shen Friday, May 3, 2013 5:38 AM
    Tuesday, April 16, 2013 1:32 AM
  • Hi MyTechnet.Me,

    I think you should use SmtpClient in sending email. 
    Have a look on this class. The implementation is indicated below. :)

    http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx

    Eric 


    Failure is not the worst thing in the world. The very worst is not to try. Email Address : ericjohnadamos@gmail.com. http://ericjohnadamos.blogspot.com/

    • Marked as answer by Bob Shen Friday, May 3, 2013 5:38 AM
    Tuesday, April 16, 2013 1:14 AM
  • Deleted
    • Marked as answer by Bob Shen Friday, May 3, 2013 5:38 AM
    Tuesday, April 16, 2013 6:06 AM

All replies

  • Hi MyTechnet.Me,

    I think you should use SmtpClient in sending email. 
    Have a look on this class. The implementation is indicated below. :)

    http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx

    Eric 


    Failure is not the worst thing in the world. The very worst is not to try. Email Address : ericjohnadamos@gmail.com. http://ericjohnadamos.blogspot.com/

    • Marked as answer by Bob Shen Friday, May 3, 2013 5:38 AM
    Tuesday, April 16, 2013 1:14 AM
  • Hello,

    There is an example, on how to do that:

    using System;
    using System.Windows.Forms;
    using System.Net.Mail;
    
    namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    MailMessage mail = new MailMessage();
                    SmtpClient SmtpServer = new SmtpClient("xxx");
    
                    mail.From = new MailAddress(xxx@xxx.xxx);
                    mail.To.Add("yyy");
                    mail.Subject = "Send HTML Template";
    
                    mail.IsBodyHtml = true;
                    string htmlBody;
    
                    htmlBody = "Template HTML here";
    
                    mail.Body = htmlBody;
    
                    SmtpServer.Port = 25;
                    SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
                    SmtpServer.EnableSsl = true;
    
                    SmtpServer.Send(mail);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
        }
    }

    João Sousa (MCTS) Senior Software Engineer

    • Proposed as answer by Carsten Siemens Wednesday, April 17, 2013 4:39 PM
    • Marked as answer by Bob Shen Friday, May 3, 2013 5:38 AM
    Tuesday, April 16, 2013 1:32 AM
  • Deleted
    • Marked as answer by Bob Shen Friday, May 3, 2013 5:38 AM
    Tuesday, April 16, 2013 6:06 AM
  • mail.IsBodyHtml = true;
    string htmlBody = "create html page" ;
    mail.Body = htmlBody;

    Full Source : HTML Email

    Lee


    You do realize this question is back from 2013, please keep responses to current threads, thank you.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, May 2, 2016 2:06 PM