Solved

Configuring the App Config to work with smtp

Posted on 2007-04-10
4
2,241 Views
Last Modified: 2013-11-26
I have the following code snippet that is working to send an email via smtp.  I would like to use an appconfig file to store the host,and to and from email addresses.....I cant seem to figure out how to setup the app config file so that it will work...here is what I ha ve so f ar.

here is my snippet from my config file...
<system.net>
   <mailSettings>
              <smtp from="server@default.net">
                    <network host="127.0.0.1"/></smtp>
</mailSettings>
</system.net>

and here is the code without referencing the appconfig

 Dim mMailMessage As New MailMessage()

            'sender e-mail address.
            mMailMessage.From = New MailAddress("server@blah.net")
            'recipient e-mail address.
            mMailMessage.To.Add(New MailAddress("blah@blah.com"))
            ' Set the subject of the mail message
            mMailMessage.Subject = "ALERT"
            ' Set the body of the mail message
            mMailMessage.Body = "<HTML><BODY><B>ALERT! The system did not process the ADS_Blend Job</B></BODY></HTML>"
            ' Set the format of the mail message body as HTML
            mMailMessage.IsBodyHtml = True
            ' Set the priority of the mail message to normal
            mMailMessage.Priority = MailPriority.Normal

            ' Instantiate a new instance of SmtpClient
            Dim mSmtpClient As New SmtpClient()
            mSmtpClient.Host = "127.0.0.1"
            ' Send the mail message
            mSmtpClient.Send(mMailMessage)

0
Comment
Question by:Robb Hill
  • 3
4 Comments
 
LVL 15

Accepted Solution

by:
igor_alpha earned 500 total points
ID: 18887306
Hi robbhill,
You don't need to manually retrive app.config settings. Settings would be placed automatically based on values in app.config.
Just remove following strings:
mMailMessage.From = New MailAddress("server@blah.net")
and
mSmtpClient.Host = "127.0.0.1"

and you will see that values setted automatically.

To get values of config section manually use following syntax:
Dim smtpSec As SmtpSection = CType(ConfigurationManager.GetSection("system.net/mailSettings/smtp"), SmtpSection)
Response.Write(smtpSec.Network.Host + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "")
Response.Write(smtpSec.Network.UserName + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "")
Response.Write(smtpSec.Network.Password + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "")
Response.Write(smtpSec.Network.Port + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "")
0
 
LVL 11

Author Comment

by:Robb Hill
ID: 18889118
i have never seen this syntax.....what would the app config look like if I wanted to add a recipient email address
0
 
LVL 11

Author Comment

by:Robb Hill
ID: 18889363
I am trying to get this piece from the app config file:

 mMailMessage.To.Add(New MailAddress("blah@blah.com"))

0
 
LVL 11

Author Comment

by:Robb Hill
ID: 18890537
actually I was looking to use the .net 2.0 my.settings feature.  I figured it out....its all auto generated....by right clicking on the properties of the project....then chosing settings ..i was able to setup a application level variable.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question