Home
Manage Your Code
Snippet: Class to send an Email (C#)
Title: Class to send an Email Language: C#
Description: Class to Send An Email in Asp.net C# Views: 254
Author: Pragnesh Patel Date Added: 7/1/2008
Copy Code  
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Net;
11using System.Net.Mail;
12using System.Net.Mime;
13using System.Text;
14
15 
16
17
18
19    public class CustEMail
20    {
21        string m_MailTo = "";
22        string m_MailCC = "test@test.com";
23        string m_MailBCC = "";
24        string m_MailFrom = ConfigurationManager.AppSettings.Get("MailFrom").ToString();
25        string m_MailSubject = "";
26        string m_ReadReceiptTo = "";
27        string m_ReplyTo = "";
28        string m_MailBody = "";
29
30
31        public CustEMail()
32        {            
33            //

34            // TODO: Add constructor logic here

35            //

36        
37        }
38        
39        public string MailTo
40        {
41            get { return m_MailTo; }
42            set { m_MailTo = value; }
43        }
44
45        public string MailCC
46        {
47            get { return m_MailCC; }
48            set { m_MailCC = value; }
49        }
50
51        public string MailBCC
52        {
53            get { return m_MailBCC; }
54            set { m_MailBCC = value; }
55        }
56        
57        public string MailFrom
58        {
59            get { return m_MailFrom; }
60            set { m_MailFrom = value; }
61        }
62
63        public string MailSubject
64        {
65            get { return m_MailSubject; }
66            set { m_MailSubject = value; }
67        }
68
69        public string ReadReceiptTo
70        {
71            get { return m_ReadReceiptTo; }
72            set { m_ReadReceiptTo = value; }
73        }
74
75        public string ReplyTo
76        {
77            get { return m_ReplyTo; }
78            set { m_ReplyTo = value; }
79        }
80
81        public string MailBody
82        {
83            get { return m_MailBody; }
84            set { m_MailBody = value; }
85        }
86
87        public bool Send(string Body)
88        {
89   
90
91            MailMessage mail = new MailMessage();
92
93            //set the addresses

94            mail.From = new MailAddress(m_MailFrom ,"Your Company Name");
95
96
97            int extIndx;           
98            string Mto = "";           
99            do
100            {
101                extIndx = MailTo.IndexOf(";");
102                Mto = MailTo.Substring(0 , extIndx);
103                mail.To.Add(Mto);
104                Mto = Mto + ";";
105                MailTo = MailTo.Replace(Mto , "");
106            }
107            while (MailTo.Contains(";"));
108
109            if (MailTo != ""  && MailTo != ";")
110            {
111                mail.To.Add(MailTo);
112            }
113
114            //set the content

115            mail.Subject = MailSubject;
116            mail.Body = Body;
117            mail.IsBodyHtml = true;
118            mail.Headers.Add("X-Company", "Your Company Name");
119            mail.Headers.Add("X-Location", "test@test.com");
120
121            // Add the Read Reciept If needed

122            if (ReadReceiptTo != "")
123            {
124                ReadReceiptTo = "<" + ReadReceiptTo + ">";
125                mail.Headers.Add("Disposition-Notification-To", ReadReceiptTo);          
126            }
127
128            // Add the Reply to the mail

129            if (ReplyTo != "")
130            {
131                mail.ReplyTo = new MailAddress(ReplyTo);
132            }
133                 
134        
135            // Create smtp client objec to send an email

136            SmtpClient smtp = new SmtpClient(ConfigurationManager.AppSettings.Get("MailServer").ToString() );
137                       
138            
139            try
140            {
141                smtp.Send(mail);
142                //SmtpMail.Send(msg);

143                return true;
144            }
145            catch (Exception ex)
146            {                
147              return false;
148            }
149
150         
151        }
152
153    }
154
155