Thursday, September 15, 2011

Send Email via SMTP using VB.NET

Create an aspx-file and enter the code for form:
<div align="left" id="dataform">
<table align="left" cellpadding="5" cellspacing="0" border="0" width="100%" bgcolor="#f2f2f2">
<tr>
<td colspan="2" align="center"><h3>Data Request Form</h3></td>
</tr>
<tr>
<td align="center" colspan="2"><asp:Label ID="FinalMessage" runat="server" Text=""></asp:Label></td>
</tr>
<tr>
<td align="right" width="250" valign="top"><asp:Label ID="Lbl_fname" runat="server" Text="Fist Name"></asp:Label></td>
<td align="left" valign="top">
<asp:TextBox ID="txt_fname" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" id="reqFName"
controltovalidate="txt_fname" style="color:Red;" >*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right" width="250" valign="top"><asp:Label ID="Lbl_lname" runat="server" Text="Last Name"></asp:Label></td>
<td align="left" valign="top">
<asp:TextBox ID="txt_lname" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" id="reqLName"
controltovalidate="txt_lname" style="color:Red;">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right" width="250" valign="top"><asp:Label ID="Lbl_email" runat="server" Text="Email"></asp:Label></td>
<td align="left" valign="top">
<asp:TextBox ID="txt_email" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txt_email" style="color:Red;">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator id="RegularExpressionValidator1" runat="server" ControlToValidate="txt_email"
ErrorMessage="You must enter a valid email address" style="color:Red;"
ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
</asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td align="right" width="250" valign="top"><asp:Label ID="Lbl_org" runat="server" Text="Organization"></asp:Label></td>
<td align="left" valign="top">
<asp:TextBox ID="txt_org" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" id="reqOrg"
controltovalidate="txt_org" style="color:Red;">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right" width="250" valign="top"><asp:Label ID="Lbl_phone" runat="server" Text="Phone"></asp:Label></td>
<td align="left" valign="top">
<asp:TextBox ID="txt_phone" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right" width="250" valign="top"><asp:Label ID="Lbl_fax" runat="server" Text="Fax"></asp:Label></td>
<td align="left" valign="top">
<asp:TextBox ID="txt_fax" runat="server"></asp:TextBox>
</td>
</tr>
<tr><td colspan="2" height="30px"></td></tr>
<tr>
<td align="right" width="250" valign="top"><asp:Label ID="Lbl_data" runat="server" Text="Data Requested"></asp:Label></td>
<td align="left" valign="top">
<asp:TextBox ID="txt_data" runat="server" TextMode="multiline" Rows="10" Columns="45"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" id="reqdata"
controltovalidate="txt_data" style="color:Red;">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td width="250"> </td>
<td align="left"><asp:Button ID="Send" runat="server" Text="Send" type="Submit"/>
<asp:Button ID="Reset" runat="server" Text="Reset" OnClientClick="this.form.reset();return false;" /></td>
</tr>
<tr>
<td height="20" colspan="2"> </td>
</tr>
</table>
</div>
Open the corresponding aspx.vb-file and enter "Imports System.Net.Mail" on a top. Enter the code below:Protected Sub Send_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Send.Click
'original code http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.from.aspx

Dim email, name, org, phone, fax, data As String
email = txt_email.Text.Trim()
name = txt_fname.Text.Trim() + " " + txt_lname.Text.Trim()
org = txt_org.Text.Trim()
phone = txt_phone.Text.Trim()
fax = txt_fax.Text.Trim()
data = txt_data.Text.Trim()

Dim [from] As New MailAddress(email, name)
Dim [to] As New MailAddress("astaroseletskaya@cdrg.org", "Alla Staroseletskaya")
Dim message As New MailMessage([from], [to])
message.Subject = "Data Request"
message.Body = "This message was sent by" + name + " from email address " + email + ". Organization: " + org + " Phone: " + phone + " Fax: " + fax + " Request: " + data

Dim smtpClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient()
Try
smtpClient.Send(message)
FinalMessage.Text = "Message Sent"
Catch ex As Exception
FinalMessage.Text = ex.ToString()
End Try
End Sub

1 comment: