Thursday, May 19, 2011

SMTP Server Relay setting and sending email from c# code

Use below steps to set up your SMTP server first
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, your 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) 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 ( in the list
Click OK, you are done, 
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 from 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())

                if (!string.IsNullOrEmpty(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);

                catch (Exception ex)



  1. you can easily install Wordpress, Joomla, Magento, Drupal and several other applications in a matter of seconds.

    Dedicated Server Price

  2. Nice one. It will more beneficial If you taught how to create username and password.

    Send Emails in asp .net