Send Emails using ASP.NET

Sending Emails has always been an important function of any web based application. Many web applications use emails as the basic means of communication with its users. It is used to verify a users email id; so that the user does not put someone else’s mail id instead of his or sending reminder emails, notification and much more. Hence, in this series we see sending of emails from sending the basic email to sending complete HTML pages using various programming languages. I will mainly concentrate on PHP, ASP.NET and Java.

Sending Email using ASP.NET

Sending emails in ASP.NET is very easy especially with the use of MailMessage and the SMTPClient classes. These two classes are available in the System.Net.mail namespace. The MailMessage class creates the mail message to send and it contains all the properties which can be used in a mail message like adding CC and BCC, adding headers and much more.

The SMTPClient class is used to send the email message using the send() function.  This function takes an argument of type MailMessage.

The code to send the email is as follows:

Default.aspx File: (this file merely contains HTML form which will take several fields of Email).

<div id=”EmailForm” runat=”server”>
<form runat=”server”>
<table>
<tr><td>Username</td><td>
<asp:TextBox ID=”userName” runat=”server”></asp:TextBox></td></tr>
<tr><td>Password</td><td><input type=”password” runat=”server” /></td></tr>
<tr><td>Outgoing SMTP server</td><td><asp:TextBox ID=”mailServer” runat=”server”></asp:TextBox></td><td>Port</td><td><asp:TextBox ID=”port” runat=”server” Width=”25″></asp:TextBox></td></tr>
<tr><td>To:</td><td><asp:TextBox ID=”to1″ runat=”server”></asp:TextBox></td></tr>
<tr><td>Subject</td><td>
<asp:TextBox ID=”subjectinfo” runat=”server”></asp:TextBox></td></tr>
<tr><td>Message<td><textarea id=”message” cols=”20″ rows=”2″ runat=”server”></textarea></td></tr>
<tr><td colspan=”2″  align=”center”><asp:Button ID=”Button1″ runat=”server” Text=”Send Email” /></td></tr>

</table>
</form>
<div id=”result” runat=”server”>

</div>
</div>

The default.aspx.vb file which handles the code for the click on the button.

Imports System.Net

Partial Public Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim portno As Integer
If port.Text = “” Then
portno = 25
Else
portno = port.Text
End If
Dim mailmessage As System.Net.Mail.MailMessage = New Mail.MailMessage(userName.Text, to1.Text)
Dim smtp As Mail.SmtpClient = New Mail.SmtpClient(mailServer.Text, portNo)

mailmessage.BodyEncoding = Encoding.Default
mailmessage.IsBodyHtml = False
mailmessage.Subject = subjectinfo.Text
mailmessage.Body = message.Value

Try
smtp.Send(mailmessage)
result.InnerHtml = “<b>Message sent successfully</b>”
userName.Text = “”

Catch ex As Exception
result.InnerHtml = “<b>” & ex.Message & “</b>”

End Try

End Sub
End Class

Now, to send email from a SMTP server that does not require any authentication for outgoing mails can use the above code, but to send emails from an SMTP server that requires authentication( most servers these days do due to SPAM) you can make the following changes to the web.config file.

<system.net>
<mailSettings>

<smtp from=”from@yourdomain.com”>
<network host=”mail.yourdomain.com”
userName=”user@yourdomain.com”
password=”password-here” />
</smtp>
</mailSettings>
</system.net>

You can download the entire source code here. The source code in this file is a bit different than the above code as i made a few modifications at the last minute. Do make appropriate changes to the web.config file as shown in the above. All code in this post is written using vb.net.

I will also be writing code for sending emails using PHP and Java as well as writing code for more advanced functions of Mail. So, do check back for more.

This entry was posted in .NET, Programming and tagged , . Bookmark the permalink.

8 Responses to Send Emails using ASP.NET

  1. Steve says:

    My congratulation with first place in overall count on Olympic Games. Michael Phelps was the best!

  2. lsi says:

    it would be better with other languages support, but thanks..

  3. GarykPatton says:

    I think I will try to recommend this post to my friends and family, cuz it’s really helpful.

  4. CrisBetewsky says:

    Where did you take from such kind of information? Can you give me the source?

  5. Renato says:

    Many thanks for the illuminating article. I’ve been reading as well as relishing your site. I’m going to be back again!

  6. model angency says:

    Thanks for any other informative blog. The place else may I get that kind of information written in such an ideal method? I have a mission that I’m just now working on, and I’ve been on the look out for such info.

  7. I’m adding your blog rss feed so that I can see your new posts. Continue the good work!

  8. Clare Gonzelez says:

    I haven’t checked in here for some time because I thought it was getting boring, but the last few posts are good quality so I guess I will add you back to my everyday bloglist. You deserve it my friend :)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>