Total Pageviews

Tuesday, 29 November 2011

How to eMail using c#


using System;
using System.Net.Mail;

namespace Tools
{
    public class eMail
    {
        public class SMTP
        {
            private string _userName = "";
            private string _password = "";
            private string _smtpserver = "";

            private string _from = "";
            private string _subject = "";
            private string _body = "";

            MailMessage _smtp_mail = new MailMessage();

            public string SMTPServer
            {
                set { _smtpserver = value.Trim(); }
            }

            public string From
            {
                set { _from = value.Trim(); }
            }

            public string Body
            {
                set { _body = value.Trim(); }
            }

            public string Subject
            {
                set { _subject = value.Trim(); }
            }

            public SMTP()
            {
                this._userName = "";
                this._password = "";
                this._smtpserver = "localhost";
            }

            public SMTP(string user, string pass, string smtp_server)
            {
                this._userName = user;
                this._password = pass;
                this._smtpserver = smtp_server;
                MailMessage _mail = new MailMessage();
            }

            public void Credentials(string user, string pass)
            {
                this._userName = user;
                this._password = pass;
            }

            public void AddToRecipient(string recipient)
            {
                _smtp_mail.To.Add(new MailAddress(recipient));
            }

            public void AddBccRecipient(string recipient)
            {
                _smtp_mail.Bcc.Add(new MailAddress(recipient));
            }

            public void AddCcRecipient(string recipient)
            {
                _smtp_mail.CC.Add(new MailAddress(recipient));
            }

            public void Send()
            {
                _send();
            }

            public void Send(string From, string To, string Subject, string Body)
            {
                _smtp_mail.To.Add(new MailAddress(To));
                this._from = From;
                this._subject = Subject;
                this._body = Body;

                _send();
            }

            private void _send()
            {
                //set the addresses
                _smtp_mail.From = new MailAddress(this._from);

                //set the content
                _smtp_mail.Subject = this._subject;
                _smtp_mail.Body = this._body;

                //send the message
                SmtpClient smtp = new SmtpClient(this._smtpserver);

                if (!(_userName.Trim() == "" && _password.Trim() == ""))
                {
                    smtp.Credentials = new System.Net.NetworkCredential(_userName, _password);
                }

                try
                {
                    smtp.Send(_smtp_mail);
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
        }
    }
}

No comments:

Post a Comment