Share via


Microsoft.NET: SMTP Server Relay Setting and Sending Email Using C# code


Setup SMTP Server

1. Start ->> Run -> inetmgr

2. Now check if SMTP server already installed with IIS (if not then open Control Panel ->> Add/Remove Programs ->> click on add/ remove windows components, you will get a window , here you can select Internet Information Services and double click on this, now you will get list IIS of components, here you can select SMTP Server and install it).

2. Once SMTP Server is installed, then right click on SMTP Server ->> click properties, it will open a window, now select Access Tab on the top, click on Relay button, here you will get radio button option (1. Only list below, 2. All except the list below), select only list below and add your IP address and mask (127.0.0.1) in the list.

Click OK, you are done. 

Code to use SMTP Server and send Email

Now in your application, you can specify your SMTP Server IP address and credentials for sending emails. You can use below sample code for sending emails using C# code.

static public  void SendMail(string fromEmail, string fromName, string toAddress,
                                    string toName, string ccAddress, string subject, string body
                                    )
        {
            SendMail(new System.Net.Mail.MailAddress(fromEmail, fromName), toAddress, ccAddress, subject, body);
        }
 
        static public  void SendMail(System.Net.Mail.MailAddress fromAddress, string  toAddress, string  ccAddress,
                                    string subject, string body)
        {
            //Read SMTP Server Name or IP from Config xml file
            string SmtpServer = ConfigSettings.GetProperty(Constants.SMTP_SERVER);
 
            //Read User Name from Config xml file
            string SmtpUserName = ConfigSettings.GetProperty(Constants.SMTP_USERNAME);
             
            //Read User Password from Config xml file
            string SmtpUserPass = ConfigSettings.GetProperty(Constants.SMTP_PASSWORD);
             
            //Read port setting from Config xml file
            string smtpPort = ConfigSettings.GetProperty(Constants.SMTP_PORT);
 
            System.Net.Mail.SmtpClient smtpSend = new  System.Net.Mail.SmtpClient(SmtpServer);
 
            using (System.Net.Mail.MailMessage emailMessage = new System.Net.Mail.MailMessage())
            {
                emailMessage.To.Add(toAddress);
 
                if (!string.IsNullOrEmpty(ccAddress))
                    emailMessage.CC.Add(ccAddress);
 
                emailMessage.From = fromAddress;
                emailMessage.Subject = subject;
                emailMessage.Body = body;
                emailMessage.IsBodyHtml = true;
 
 
                if (!Regex.IsMatch(emailMessage.Body, @"^([0-9a-z!@#\$\%\^&\*\(\)\-=_\+])", RegexOptions.IgnoreCase) ||
                        !Regex.IsMatch(emailMessage.Subject, @"^([0-9a-z!@#\$\%\^&\*\(\)\-=_\+])", RegexOptions.IgnoreCase))
                {
                    emailMessage.BodyEncoding = Encoding.Unicode;
                }
 
                if (SmtpUserName != null && SmtpUserPass != null && smtpPort != null)
                {
 
                    smtpSend.Port = Convert.ToInt32(smtpPort);
                    smtpSend.UseDefaultCredentials = false;
                    smtpSend.Credentials = new  System.Net.NetworkCredential(SmtpUserName, SmtpUserPass);
                }
 
                try
                {
                    smtpSend.Send(emailMessage);
                }
                catch (Exception ex)
                {
                    AppException.HandleException(ex);
                }
 
            }
        }

References

This article is originally posted on http://shaikhnizam.blogspot.in/2011/05/smtp-server-relay-setting-and-sending.html