posted on Monday, February 20, 2006 1:38 AM
by
Obi
How to Send EMail from VS.Net using System.Web.Mail
It is not uncommon in a project to implement a functionality of sending out email from an ASP.Net page in the form of a Feedback or something similar. Here's how you can leverage the Web.Mail class to build a simple yet powerful functionality:
Imports System.Web.Mail
Public Class SendEmail
Private Sub cmdSendEmail_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdSendEmail.Click
Dim objMail As New MailMessage()
objMail.From = Me.txtFrom.Text.ToString
objMail.To = Me.txtTo.Text.ToString
objMail.Subject = Me.txtSubject.Text.ToString
objMail.Body = Me.txtMessage.Text.ToString
objMail.BodyFormat = MailFormat.Html
SmtpMail.Send(objMail)
Me.lblMessage.Text = "Email has been sent"
End Sub
End Class
*********************************************************************************
Below is the code I wrote for a Windows app using Framework 1.1
Public Sub SendEmail(ByVal sFromEmailAddress As String, ByVal sToEmailAddress As String, _
ByVal sEmailSubject As String, ByVal sEmailBody As String)
Dim objMail As New MailMessage
Try
SmtpMail.SmtpServer.Insert(0, "MY_SERVER")
objMail.From = sFromEmailAddress
objMail.To = sToEmailAddress
objMail.Subject = sEmailSubject
objMail.Body = sEmailBody
objMail.BodyFormat = MailFormat.Text
objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "0")
SmtpMail.Send(objMail)
Catch ex As Exception
Throw ex.InnerException
End Try
End Sub
***********************************************************************************
While the above is a plain vanilla code to do in framework 1.1. However, to do it in 2.0 is little different. Please see below:
It seems that the protocol for sending email in .NET 2.0 has changed from what it was in 1.1. The System.Web.Mail namespace has been replaced with the new System.Net.Mail names code samplepace. The following is a C# code sample on how to send mail with 2.0:
System.Net.Mail.SmtpClient o_Client = new System.Net.Mail.SmtpClient(this.MailServer);
System.Net.Mail.MailAddress o_FromAddress = new System.Net.Mail.MailAddress(this.FromEmail, this.FromName);
System.Net.Mail.MailAddress o_ToAddress = new System.Net.Mail.MailAddress(this.ToEmail, this.ToEmail);
System.Net.Mail.MailMessage o_Message = new System.Net.Mail.MailMessage(o_FromAddress, o_ToAddress);
o_Message.Subject = this.Subject;
o_Message.Body = this.Body;
o_Client.Send(o_Message);
The properties contained in “this“ should be obvious. As you can see, we now get a “friendly” send name so we no longer have to rig it as we did in 1.1.
VB.Net Version in .Net 2.0
Private Sub SendEmail(ByVal MsgFrom As String)
Dim objMM As New MailMessage(MsgFrom, "ToAddress@mail.com")
'Create the SmtpClient object
Dim smtp As New SmtpClient
'Assign the MailMessage's properties
objMM.Subject = txtSubject.Text
objMM.Body = txtBody.Text
objMM.IsBodyHtml = False
objMM.CC.Add(CELL_PHONE) 'Constant value
objMM.CC.Add(WEB_MASTER) 'Constant value
'Send the MailMessage
smtp.Send(objMM)
End Sub
Obi Oberoi