How to tips and tricks for Microsoft Visual Studio .net

Tuesday, January 6, 2009

How to - Send a SMTP E-Mail in ASP.Net


One of the things that a lot of web developers need and want to do, is to send E-Mails from their pages in response to various actions.


It is pretty easy to do, and I have put a small web page example together to show you how to do it.

[Web Page HTML]


<table style="font-family: Tahoma; font-size: 8pt">
<tr>
<td style="text-align:center">
<a href="Default.aspx">Home</a>
</td>
</tr>
</table>
<table style="font-family: Tahoma; font-size: 8pt; width: 550px">
<tr>
<td style="width: 550px; text-align: center">
<asp:Label ID="Label1" runat="server" Text="Send E-Mail" style="font-family: Tahoma; font-size: 16pt"></asp:Label>
</td>
</tr>
</table>
<table style="font-family: Tahoma; font-size: 8pt; width: 550px">
<tr>
<td style="width: 100px">
<asp:Label ID="lblTo" runat="server" Text="To"></asp:Label>
</td>
<td style="width: 400px">
<asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txtTo" runat="server" ErrorMessage="Required"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td style="width: 100px">
<asp:Label ID="lblSubject" runat="server" Text="Subject"></asp:Label>
</td>
<td style="width: 400px">
<asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="txtSubject" runat="server" ErrorMessage="Required"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td style="width: 100px">
<asp:Label ID="lblMessage" runat="server" Text="Message"></asp:Label>
</td>
<td style="width: 400px">
<asp:TextBox ID="txtMessage" runat="server" TextMode="MultiLine" Rows="10" style="width: 100%"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" ControlToValidate="txtMessage" runat="server" ErrorMessage="Required"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td style="width: 100%" colspan="2">
<asp:Button ID="btnSend" runat="server" Text="Send E-Mail" OnClick="btnSend_Click" />
<asp:Label ID="lblStatus" runat="server" Text=""></asp:Label>
</td>
</tr>
</table>



This will give you a page with a text box to enter the E-Mail address of the recipient, a subject and a message to be sent.

Copy and paste it between your page's opening and closing "form" element.

Next you will need to use the following code behind examples to make the page send the E-Mails...



[VB Code Behind Example]


Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim lvFromAddress As String = "Senders_E-MailAddress"
Dim lvToAddress As String = txtTo.Text
Dim lvSubject As String = txtSubject.Text
Dim lvMessage As String = txtMessage.Text

lblStatus.Text = SendMail(lvFromAddress, lvToAddress, lvSubject, lvMessage)
End Sub

Private Function SendMail(ByVal FromAddress As String, ByVal ToAddress As String, ByVal Subject As String, ByVal Message As String) As String
Dim lvReturn As String = ""
Try
'create the mail message
Dim mail As New MailMessage()
Dim MailServer As String = "localhost"


'set the addresses
mail.From = New MailAddress(FromAddress)
mail.To.Add(ToAddress)

'set the content
mail.Subject = Subject
mail.Body = Message
mail.IsBodyHtml = True

Dim lvMailSender As New SmtpClient(MailServer)
lvMailSender.Send(mail)
lvReturn = "Message sent"
Catch
'lvError = Err.Number
lvReturn = "Error " & Err.Number & " - " & Err.Description
End Try

Return lvReturn
End Function

[C# Code Behind Example]

protected void btnSend_Click(object sender, EventArgs e)
{
String lvFromAddress = "Senders_E-MailAddress";
String lvToAddress = txtTo.Text;
String lvSubject = txtSubject.Text;
String lvMessage = txtMessage.Text;

lblStatus.Text = SendMail(lvFromAddress, lvToAddress, lvSubject, lvMessage);
}

protected String SendMail(String FromAddress, String ToAddress, String Subject, String Message)
{
String lvReturn = "";
try
{
//create the mail message
MailMessage mail=new MailMessage();
String MailServer = "localhost";

//set the addresses
mail.From = new MailAddress(FromAddress);
mail.To.Add(ToAddress);

//set the content
mail.Subject = Subject;
mail.Body = Message;
mail.IsBodyHtml = true;

SmtpClient lvMailSender;
lvMailSender = new SmtpClient(MailServer);
lvMailSender.Send(mail);
lvReturn = "Message sent";
return lvReturn;
}
catch(Exception ex)
{
lvReturn = "Error - " + ex.Message;
return lvReturn;
}
}


Its pretty simple to use.
Please let me know if you have any problems with the code.

No comments:

Post a Comment