Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Configuring the App Config to work with smtp

Posted on 2007-04-10
4
Medium Priority
?
2,247 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 1500 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

705 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