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.