Introduction
It is very easy to send emails from ASP.Net using any language like VB or
C#, but there is no any article which explains that how to use credentials from
web.config file. In general, to send email we also use to type the sender's
email ID that is FROM address. It is very bored work to do the same thing
always. There should be any other way to define the FROM email address once,
like in web.config file and use it entirely in website. Let's take a look.
Perquisite
To understand this project, you should have general email sending technique.
Because, in this article, basics are not covered.
Credentials
To call the credentials from web.config file we use to write
Dim
mysmtp As New
System.Net.Mail.SmtpClient()
In Default.aspx.vb page. And in web.config file we use to write the following
<system.net>
<mailSettings>
<smtp>
<network
host="smtp.domainname.com"
userName="username@domainname.com"
password="xxxxxxxxx"
port="25"/>
</smtp>
</mailSettings>
</system.net>
Above codes should be placed within <configuration> tag.
FROM address in web.config file
In web.config file we use to write the following
<appSettings>
<add
key="mydefaultFROM"
value="username@domainname.com"/>
</appSettings>
In above code value should be same as username in very above coding, key is just
a calling name used inside Default.aspx.vb page as follows
mailmsg.From = New System.Net.Mail.MailAddress(txtFrom.Text.Trim())
mailmsg.From = New
System.Net.Mail.MailAddress(System.Configuration.ConfigurationSettings.AppSettings("mydefaultFROM"))
In above coding, use any one, like, if you want to use textbox on form to write
FROM email address then use 'red' colored coding but want to use web.config file
use below the 'red' colored coding.
Default.aspx Coding
<%@
Page Language="VB"
AutoEventWireup="false"
CodeFile="Default.aspx.vb"
Inherits="_Default"
%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml"
>
<head
runat="server">
<title>Sending
Email with Attachement in ASP.Net using VB Language</title>
</head>
<body>
<form
id="form1"
runat="server">
<div>
<strong><span
style="text-decoration:
underline; font-size: 14pt; color: #3300ff;">Sending Email with
Attachement in ASP.Net using
VB Language</span></strong><br
/>
<br
/>
<span
style="font-size: 10pt;
font-family: Verdana"><strong>Note:</strong>
Please note
that, in this project I have used to way to represent the FROM
address. First way
is to write the from address at run time and second one is to
include the from address
in web.config file and the same mention in Default.aspx.vb page, it
will work perfectly
with giving FROM address at runtime. From address can be any mail
id. </span>
<br
/>
<br
/>
<table
style="width: 465px;
height: 61px" bgcolor="#dcdcdc">
<tr>
<td
style="width: 66px">
</td>
<td
style="width: 76px">
<br
/>
<asp:Label
ID="lblInfo"
runat="server"
Width="212px"
Font-Names="Berlin Sans
FB Demi" ForeColor="Red"></asp:Label><br
/>
</td>
<td
style="width: 53px">
</td>
</tr>
<tr>
<td
style="width: 66px">
From:</td>
<td
style="width: 76px">
<asp:TextBox
ID="txtFrom"
runat="server"></asp:TextBox></td>
<td
style="width: 53px">
</td>
</tr>
<tr>
<td
style="width: 66px">
To:</td>
<td
style="width: 76px">
<asp:TextBox
ID="txtTo"
runat="server"></asp:TextBox></td>
<td
style="width: 53px">
</td>
</tr>
<tr>
<td
style="width: 66px">
Subject:</td>
<td
style="width: 76px">
<asp:TextBox
ID="txtSubject"
runat="server"></asp:TextBox></td>
<td
style="width: 53px">
</td>
</tr>
<tr>
<td
style="width: 66px">
Message:</td>
<td
style="width: 76px">
<asp:TextBox
ID="txtMessage"
runat="server"
Height="60px"
TextMode="MultiLine"
Width="199px"></asp:TextBox></td>
<td
style="width: 53px">
</td>
</tr>
<tr>
<td
style="width: 66px">
Attachement:</td>
<td
style="width: 76px">
<asp:FileUpload
ID="FileUpload1"
runat="server"
/></td>
<td
style="width: 53px">
</td>
</tr>
<tr>
<td
style="width: 66px">
</td>
<td
style="width: 76px">
</td>
<td
style="width: 53px">
</td>
</tr>
<tr>
<td
style="width: 66px">
</td>
<td
style="width: 76px">
<asp:Button
ID="send"
runat="server"
Text="Send"
Width="78px"
/></td>
<td
style="width: 53px">
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Default.aspx.vb Coding
Partial
Class _Default
Inherits System.Web.UI.Page
Protected Sub
send_Click(ByVal sender
As Object, ByVal
e As System.EventArgs)
Handles send.Click
If txtTo.Text = ""
Or txtMessage.Text =
"" Then
lblInfo.Text =
"Complete your email."
Else
'******************************************************************
'declaring the mailing class
'******************************************************************
Dim mailmsg
As System.Net.Mail.MailMessage =
New System.Net.Mail.MailMessage()
'******************************************************************
'declaring the FROM email address
we can also define the standard
'******************************************************************
'Normal FROM email address using (only one can run at
once)
'------------------------------------------------------------------
mailmsg.From = New
System.Net.Mail.MailAddress(txtFrom.Text.Trim())
'FROM address in web.config file and then we can call
that as
'-----------------------------------------------------------------
'mailmsg.From = New
System.Net.Mail.MailAddress(System.Configuration.ConfigurationSettings.AppSettings("mydefaultFROM"))
'*****************************************************************
'declaring the TO email address,
there can be multiple email addresses
'*****************************************************************
mailmsg.To.Add(New
System.Net.Mail.MailAddress(txtTo.Text.Trim()))
'mailmsg.CC.Add
'mailmsg.BCC.Add
'mailmsg.ReplyTo.Add
'*****************************************************************
'declaring the Subject of email
'*****************************************************************
mailmsg.Subject = txtSubject.Text.Trim()
'*****************************************************************
'declaring the Mail Body of email,
here we can change to send
'the database data or form result
as we can say
'*****************************************************************
mailmsg.Body = txtMessage.Text.Trim()
'*****************************************************************
'sending attachements
'*****************************************************************
'mailmsg.Attachments.Add(New
System.Net.Mail.Attachment("C:\abhi.txt"))
If FileUpload1.HasFile =
True Then
Dim fu
As FileUpload = FileUpload1
Dim ct
As String = fu.PostedFile.ContentType
'Get file name from fully qualified file name
Dim fn
As String =
fu.PostedFile.FileName
Dim c
As Integer = fn.LastIndexOf("\")
fn = fn.Substring(c + 1)
mailmsg.Attachments.Add(New
System.Net.Mail.Attachment(fu.PostedFile.InputStream, fn, ct))
End If
'*****************************************************************
'setting up the smpt configuration
'Dim mysmtp As
System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient()
'*****************************************************************
Dim mysmtp
As New
System.Net.Mail.SmtpClient()
'****************************************************************
'final step to send email
'****************************************************************
Try
mysmtp.Send(mailmsg)
Catch mysmtpexp As
System.Net.Mail.SmtpException
'log error details will be here
Catch ex
As Exception
'log error details will be here
End
Try
'****************************************************************
'display the message for
conformation
'****************************************************************
lblInfo.Text = "Email Sent"
End If
End Sub
End
Class
Web.config Coding
<?xml
version="1.0"?>
<configuration>
<appSettings>
<add
key="mydefaultFROM"
value="username@domainname.com"/>
</appSettings>
<connectionStrings/>
<system.web>
<compilation
debug="true"/>
<authentication
mode="Windows"/>
</system.web>
<system.net>
<mailSettings>
<smtp>
<network
host="smtp.domainname.com"
userName="username@domainname.com"
password="xxxxxxxxx"
port="25"/>
</smtp>
</mailSettings>
</system.net>
</configuration>

HAVE A HAPPY CODING!