smtp mail setting in web config

Asked By Reena Jain
18-Nov-09 02:08 AM
Earn up to 0 extra points for answering this tough question.
I was using this mail setting in web.config file
   <appSettings>
        
        <add key="MailServer" value="mail.expertx.us"/>
        <add key="Mailpassword" value="exp223inf"/>
        <add key="MailuserName" value="info@expertx.us"/>
        <add key="Mailhost" value="mail.expertx.us"/>
        <add key="MailPort" value="25"/>
        <add key="ToEmailId" value="rajmera@gmail.com"/>
        <add key="FromEmailId" value="rajmera@gmail.com"/>
        <add key="Bcc" value="rajmera@gmail.com"/>
        
    </appSettings>

 but since client is using godaddy hosting and they giving the below smtp settings for web.config file:

<system.net>
<mailSettings>
<smtp from="username@domain.com">
<network
host="smtpout.secureserver.net"
port="25"
userName="username@domain.com"
password="yourpassword" />
</smtp>
</mailSettings>
</system.net>


also given one note what is for:-Please note that SMTP Authentication sould be basic (anonymous will not work).

Now how should I change the setting in web.config file to add ‘key’ because I’m using following coding in .cs page for sending mails

public bool SendMail(string MailFrom, string MailTo, string Subject, string Message, string AttachedFile, string strCC, string strBcc)
        {
            try
            {
                //Get NetworkCredential UserName and Password for sending mail

                string userName = ConfigurationManager.AppSettings["MailuserName"].ToString();
                string password = ConfigurationManager.AppSettings["Mailpassword"].ToString();
                string smtpHost = ConfigurationManager.AppSettings["MailServer"].ToString();
                int smtpPort = Convert.ToInt32(ConfigurationManager.AppSettings["MailPort"].ToString());

                SmtpClient oSmtpClient = new SmtpClient();
                MailMessage oMailMessage = new MailMessage();
                MailAddress oMailAddress = new MailAddress(MailTo);
                MailAddress fromMailAddress = new MailAddress(MailFrom);
                oMailMessage.To.Add(oMailAddress);
                oMailMessage.From = fromMailAddress;
                
                if (strBcc != "")
                {
                    MailAddress mBcc = new MailAddress(strBcc);

                    oMailMessage.Bcc.Add(mBcc);
                }
                if (strCC != "")
                {
                    MailAddress mCc = new MailAddress(strCC);
                    oMailMessage.CC.Add(mCc);
                }
                oMailMessage.Subject = Subject.Replace("\r\n", "");
                oMailMessage.IsBodyHtml = true;
               
                oMailMessage.Body = Message;
                oSmtpClient.Host = smtpHost;
                oSmtpClient.Port = smtpPort;
                if (!(string.IsNullOrEmpty(AttachedFile)))
                {                    
                     Attachment objAttachment = new Attachment(AttachedFile);
                     oMailMessage.Attachments.Add(objAttachment);
                }
                System.Net.NetworkCredential oNetworkCredential = new System.Net.NetworkCredential(userName, password);
                oSmtpClient.UseDefaultCredentials = false;
                oSmtpClient.Credentials = oNetworkCredential;
                oSmtpClient.Send(oMailMessage);
               return true;
            }
            catch (Exception ex) { }
            {

            }
            return true;
        }

So how should I adjust the new smpt setting with above code

Thanks in advance

  smtp mail setting in web config

mv ark replied to Reena Jain
30-Nov-09 09:09 PM
Adapt this snippet -

Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
        MailSettingsSectionGroup settings = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
        Response.Write("host: " + settings.Smtp.Network.Host + "<br />");
        Response.Write("port: " + settings.Smtp.Network.Port + "<br />");
        Response.Write("Username: " + settings.Smtp.Network.UserName + "<br />");
        Response.Write("Password: " + settings.Smtp.Network.Password + "<br />");
        Response.Write("from: " + settings.Smtp.From + "<br />");

For more details, refer to this - http://dotnetslackers.com/Community/blogs/kaushalparik/archive/2008/09/06/accessing-web-config-file-smtp-mail-settings-programmatically.aspx

  thanks, i will try it and i think it will work for me

Reena Jain replied to mv ark
01-Dec-09 01:12 AM
end of post
Create New Account