Solved

Configuring the App Config to work with smtp

Posted on 2007-04-10
4
2,245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

623 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