I am using my own custom connection string in web.config file to connect to smtp server to send emails in my web application how can I parse connection string to get server port user etc.
Download Code | Run Sample
DbConnectionStringBuilder class available in System.Data.Common Namespace provides the base class from which the strongly typed connection string builders. This class is can be very useful to parse your own connection string.
Let's take example of connection string that you might need for connecting with SMTP server. Information that you need to connect to SMTP server are.
- Server address
- Server Port
- User Name
- Pass word
If you put all things together then your connection string will look somewhat like.
"server=smtp.gmail.com; port=587; user=you@gmail.com; password=yourpassword"
And code to parse your connection string will look like following.
DbConnectionStringBuilder builder
= new DbConnectionStringBuilder();
builder.ConnectionString
= @"server=smtp.gmail.com; port=587;
user=you@gmail.com; password=yourpassword";
string server = builder["server"];
string port = builder["port"]