ASP.NET 2.0 (and later) has a built in class, System.Net.Mail, to send email.
VB.NET Code Sample
| <%@ Page Language="VB" %> |
| <%@ Import Namespace="System.Net.Mail" %> |
| <script runat="server"> |
| Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) |
| |
| Dim strFrom = "postmaster@HostingAccountDomain.com" |
| Dim strTo = "postmaster@HostingAccountDomain.com" |
| Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), New MailAddress(strTo)) |
| MailMsg.BodyEncoding = Encoding.Default |
| MailMsg.Subject = "This is a test" |
| MailMsg.Body = "This is a sample message using SMTP authentication" |
| MailMsg.Priority = MailPriority.High |
| MailMsg.IsBodyHtml = True |
| 'Smtpclient to send the mail message |
| |
| Dim SmtpMail As New SmtpClient |
| Dim basicAuthenticationInfo As New System.Net.NetworkCredential("postmaster@mydoamin.com", "password") |
| |
| SmtpMail.Host = "mail.HostingAccountDomain.com" |
| SmtpMail.UseDefaultCredentials = False |
| SmtpMail.Credentials = basicAuthenticationInfo |
| |
| SmtpMail.Send(MailMsg) |
| lblMessage.Text = "Mail Sent" |
| End Sub |
| </script> |
| <html> |
| <body> |
| <form runat="server"> |
| <asp:Label id="lblMessage" runat="server"></asp:Label> |
| </form> |
| </body> |
| </html> |
C# Code Sample
| <%@ Import Namespace="System.Net" %> |
| <%@ Import Namespace="System.Net.Mail" %> |
| |
| <script language="C#" runat="server"> |
| protected void Page_Load(object sender, EventArgs e) |
| { |
| //create the mail message |
| MailMessage mail = new MailMessage(); |
| |
| //set the addresses |
| mail.From = new MailAddress("postmaster@HostingAccountDomain.com"); |
| mail.To.Add("postmaster@HostingAccountDomain.com"); |
| |
| //set the content |
| mail.Subject = "This is an email"; |
| mail.Body = "This is from system.net.mail using C sharp with smtp authentication."; |
| //send the message |
| SmtpClient smtp = new SmtpClient("mail.HostingAccountDomain.com"); |
| |
| NetworkCredential Credentials = new NetworkCredential("postmaster@HostingAccountDomain.com", "password"); |
| smtp.Credentials = Credentials; |
| smtp.Send(mail); |
| lblMessage.Text = "Mail Sent"; |
| } |
| </script> |
| <html> |
| <body> |
| <form runat="server"> |
| <asp:Label id="lblMessage" runat="server"></asp:Label> |
| </form> |
| </body> |
| </html> |
If you run in to problems using this code, please post in our
community forum. Technical
support cannot assist with specific coding related issues.
Article ID: 650, Created On: 8/14/2009, Modified: 8/29/2011