Some people may not know that .NET 2.0 provides APIs for accessing everything in a configuration file. The most common question I see relates to accessing the mail settings in the system.net node programmatically.
C#
using
System.Configuration;
using
System.Web.Configuration;
using
System.Net.Configuration;
Configuration
configurationFile =
WebConfigurationManager
.OpenWebConfiguration(
"PathToConfigFile"
);
MailSettingsSectionGroup
mailSettings = configurationFile.GetSectionGroup(
"system.net/mailSettings"
)
as
MailSettingsSectionGroup
;
if
(mailSettings !=
null
)
{
int
port = mailSettings.Smtp.Network.Port;
string
host = mailSettings.Smtp.Network.Host;
string
password = mailSettings.Smtp.Network.Password;
string
username = mailSettings.Smtp.Network.UserName;
}
VB.NET
Imports
System.Configuration
Imports
System.Web.Configuration
Imports
System.Net.Configuration
Dim
configurationFile
As
Configuration = WebConfigurationManager.OpenWebConfiguration(
"PathToConfigFile"
)
Dim
mailSettings
As
MailSettingsSectionGroup = configurationFile.GetSectionGroup(
"system.net/mailSettings"
)
If
Not
mailSettings
Is
Nothing
Then
Dim
port
As
Integer
= mailSettings.Smtp.Network.Port
Dim
host
As
String
= mailSettings.Smtp.Network.Host
Dim
password
As
String
= mailSettings.Smtp.Network.Password
Dim
username
As
String
= mailSettings.Smtp.Network.UserName
End
If
Share this post:
|
|
|
|
|
Below is a C# and VB.NET class that demonstrates using System.Net.Mail to send an email.
Download C# System.Net.Mail Helper
Download VB.NET System.Net.Mail Helper
Calling the function from code
MailHelper
.SendMailMessage(
""
,
""
,
""
,
""
,
"Sample Subject"
,
"Sample body of text for mail message"
)
MailHelper.cs
using
System.Net.Mail;
public
class
MailHelper
{
///
///
Sends an mail message
///
///
Sender address
///
Recepient address
///
Bcc recepient
///
Cc recepient
///
Subject of mail message
///
Body of mail message
public
static
void
SendMailMessage(
string
from,
string
to,
string
bcc,
string
cc,
string
subject,
string
body)
{
// Instantiate a new instance of MailMessage
MailMessage
mailMessage =
new
MailMessage
();
// Set the sender address of the mail message
mailMessage.From =
new
MailAddress
(from);
// Set the recepient address of the mail message
mailMessage.To.Add(
new
MailAddress
(to));
// Check if the bcc value is null or an empty string
if
(!
string
.IsNullOrEmpty(bcc))
{
// Set the Bcc address of the mail message
mailMessage.Bcc.Add(
new
MailAddress
(bcc));
}
// Check if the cc value is null or an empty value
if
(!
string
.IsNullOrEmpty(cc))
{
// Set the CC address of the mail message
mailMessage.CC.Add(
new
MailAddress
(cc));
}
// Set the subject of the mail message
mailMessage.Subject = subject;
// Set the body of the mail message
mailMessage.Body = body;
// Set the format of the mail message body as HTML
mailMessage.IsBodyHtml =
true
;
// Set the priority of the mail message to normal
mailMessage.Priority =
MailPriority
.Normal;
// Instantiate a new instance of SmtpClient
SmtpClient
smtpClient =
new
SmtpClient
();
// Send the mail message
smtpClient.Send(mailMessage);
}
}
MailHelper.vb
Imports
System.Net.Mail
Public
Class
MailHelper
'''
''' Sends an mail message
'''
''' Sender address
''' Recepient address
''' Bcc recepient
''' Cc recepient
''' Subject of mail message
''' Body of mail message
Public
Shared
Sub
SendMailMessage(
ByVal
from
As
String
,
ByVal
recepient
As
String
,
ByVal
bcc
As
String
,
ByVal
cc
As
String
,
ByVal
subject
As
String
,
ByVal
body
As
String
)
' Instantiate a new instance of MailMessage
Dim
mailMessage
As
New
MailMessage()
' Set the sender address of the mail message
mailMessage.From =
New
MailAddress(from)
' Set the recepient address of the mail message
mailMessage.To.Add(
New
MailAddress(recepient))
' Check if the bcc value is nothing or an empty string
If
Not
String
.IsNullOrEmpty(bcc)
Then
' Set the Bcc address of the mail message
mailMessage.Bcc.Add(
New
MailAddress(bcc))
End
If
' Check if the cc value is nothing or an empty value
If
Not
String
.IsNullOrEmpty(cc)
Then
' Set the CC address of the mail message
mailMessage.CC.Add(
New
MailAddress(cc))
End
If
' Set the subject of the mail message
mailMessage.Subject = subject
' Set the body of the mail message
mailMessage.Body = body
' Set the format of the mail message body as HTML
mailMessage.IsBodyHtml =
True
' Set the priority of the mail message to normal
mailMessage.Priority = MailPriority.Normal
' Instantiate a new instance of SmtpClient
Dim
smtpClient
As
New
SmtpClient()
' Send the mail message
smtpClient.Send(mailMessage)
End
Sub
End
Class
Web.config
xml
version
=
"
1.0
"
?>
<
configuration
>
<
system.net
>
<
mailSettings
>
<
smtp
from
=
"
"
>
<
network
host
=
"
smtp.yourdomain.com
"
port
=
"
25
"
userName
=
"
yourUserName
"
password
=
"
yourPassword
"
/>
smtp
>
mailSettings
>
system.net
>
configuration
>
Share this post:
|
|
|
|
|
.NET 2.0 introduces XML comments to VB.NET 2.0. An XML comment starts with ''' (3 single quotes). While this is new to VB.NET 2.0, XML comments have been a supported feature of C# since version 1.0. On a side note, C# XML comments start with ///.
'''
''' I'm a VB.NET XML comment
'''
Need to access an HttpContext object in a class, but can't because the class doesn't inherit System.Web.UI.Page? Here is a simple helper class that will allow you to access some of the most common HttpContext objects.
C#
using
System;
using
System.Web;
using
System.Web.Caching;
using
System.Web.SessionState;
///
///
Encapsulates all HTTP-specific information about an individual HTTP request.
///
public
class
HttpContextHelper
{
///
///
Gets the System.Web.HttpApplicationState object for the current HTTP request.
///
public
static
HttpApplicationState
CurrentApplication
{
get
{
return
HttpContext
.Current.Application; }
}
///
///
Gets the System.Web.Caching.Cache object for the current HTTP request.
///
public
static
Cache
CurrentCache
{
get
{
return
HttpContext
.Current.Cache; }
}
///
///
Gets the System.Web.HttpRequest object for the current HTTP request.
///
public
static
HttpRequest
CurrentRequest
{
get
{
return
HttpContext
.Current.Request; }
}
///
///
Gets the System.Web.HttpResponse object for the current HTTP response.
///
public
static
HttpResponse
CurrentResponse
{
get
{
return
HttpContext
.Current.Response; }
}
///
///
Gets the System.Web.HttpServerUtility object that provides methods used in
///
processing Web requests.
///
public
static
HttpServerUtility
CurrentServer
{
get
{
return
HttpContext
.Current.Server; }
}
///
///
Gets the System.Web.SessionState.HttpSessionState object for the current
///
HTTP request.
///
public
static
HttpSessionState
CurrentSession
{
get
{
return
HttpContext
.Current.Session; }
}
}
VB.NET
Imports
System
Imports
System.Web
Imports
System.Web.Caching
Imports
System.Web.SessionState
'''
''' Encapsulates all HTTP-specific information about an individual HTTP request.
'''
Public
Class
HttpContextHelper
'''
''' Gets the System.Web.HttpApplicationState object for the current HTTP request.
'''
Public
Shared
ReadOnly
Property
CurrentApplication()
As
HttpApplicationState
Get
Return
HttpContext.Current.Application
End
Get
End
Property
'''
''' Gets the System.Web.Caching.Cache object for the current HTTP request.
'''
Public
Shared
ReadOnly
Property
CurrentCache()
As
Cache
Get
Return
HttpContext.Current.Cache
End
Get
End
Property
'''
''' Gets the System.Web.HttpRequest object for the current HTTP request.
'''
Public
Shared
ReadOnly
Property
CurrentRequest()
As
HttpRequest
Get
Return
HttpContext.Current.Request
End
Get
End
Property
'''
''' Gets the System.Web.HttpResponse object for the current HTTP response.
'''
Public
Shared
ReadOnly
Property
CurrentResponse()
As
HttpResponse
Get
Return
HttpContext.Current.Response
End
Get
End
Property
'''
''' Gets the System.Web.HttpServerUtility object that provides methods used in
'''
processing Web requests.
'''
Public
Shared
ReadOnly
Property
CurrentServer()
As
HttpServerUtility
Get
Return
HttpContext.Current.Server
End
Get
End
Property
'''
''' Gets the System.Web.SessionState.HttpSessionState object for the current
'''
HTTP request.
'''
Public
Shared
ReadOnly
Property
CurrentSession()
As
HttpSessionState
Get
Return
HttpContext.Current.Session
End
Get
End
Property
End
Class
A common DropDownList issue I've seen on many forums and lists is about a DropDownList populating items 2x or not being able to get the selected value on postback when binding to a data source. This is due to the DropDownList having data bound to it again on postback before the form is processed. To prevent this, you will need to ensure that the DropDownList is only bound to data on the initial load of the page.
VB.NET
Private Sub
Page_Load(
ByVal
sender
As Object
,
ByVal
e
As
EventArgs)
If Not
IsPostBack
Then
' Code here to bind data to DropDownList
End If
End Sub
C#
private void
Page_Load(
object
sender,
EventArgs
e)
{
if
(!IsPostBack)
{
// Code here to bind data to DropDownList
}
}
R