Solved

Configuring the App Config to work with smtp

Posted on 2007-04-10
4
2,239 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now