As you know .NET 1.x used System.Web.Mail namespace which was a wrapper around old CDONTS and CDOSYS dlls. Microsoft.Net 2.0 introduces System.Net.Mail Namespace which is written from the ground up without any interop. So, it is not dependent on COM libraries. In most cases you face with situations for sending a simple email with plain text, with attachment or authenticated email. I wrote this example in three simple subroutines. Depends on your need or you can use either one or mix and match. There is more variety or flavors can be added like Cc, bcc, html format with or without images etc. I targeted the common uses.
This namespace is much more versatile than old CDO dependent System.Web.Mail. You can do a lot more in this new namespace. More features were presented in Microsoft Seminar. For detail information you can visit http://www.systemnetmail.com
I have created an Email Component using .NET 2.0, which can be used in your Web based or Windows based application to send email. If you are just sending regular message without any attachment or authentication, then you do not need this component. You can directly use System.Net.Mail namespace and add 2 lines of code.But if you are going to use attachments and perform user authentication dynamically then you can utilize this component.
Example for Basic and Advance use is given below. I created this component before I attended the Microsoft Seminar, so I have not yet implement additional cool features for email Content such as LinkedResource, ContentLink and AlternateView etc. Not sure if everyone needs it.
Basic Email - Just 2 lines of Code (VB.NET)
You can create four text boxes on a web page for From, To, Subject and Body and then replace the hard coded values with corresponding text box text. The following code assumes that you are using the local SMTP service of the machine on which this code is running.
Private Sub SendEmail()
Dim objEMail As New System.Net.Mail.SmtpClient
objEMail.Host = "localhost" ' If you have not defined the host in web.config
objEMail.Send("From_You@YourDomain.com", "To_Friend@Domain.com", "Subject: - Testing .NET 2.0 Email", "Body: - You Got mail from .NET 2.0 NameSpace")
End Sub
Plain Text
Public Function SendEmail(ByVal addressFrom As String, ByVal addressTo As String, _
ByVal subject As String, ByVal bodyText As String) As Boolean
Try
Dim emailMsg As New System.Net.Mail.MailMessage(addressFrom, addressTo, subject, bodyText)
Dim smtpClient As New System.Net.Mail.SmtpClient()
smtpClient.Send(emailMsg)
Return True
Catch
Return False
End Try
End Function
Attachment
Public Function SendEmailWithAttachment(ByVal addressFrom As String, ByVal addressTo As String, _
ByVal subject As String, ByVal bodyText As String, _
ByVal fileNameWithFullPath As String) As Boolean
Try
Dim emailMsg As New System.Net.Mail.MailMessage(addressFrom, addressTo, subject, bodyText)
Dim smtpClient As New System.Net.Mail.SmtpClient()
'Attach file if it exists
If File.Exists(fileNameWithFullPath) Then
If Not emailMsg.Attachments Is Nothing Then
Dim fileAttachment As New Net.Mail.Attachment(fileNameWithFullPath)
emailMsg.Attachments.Add(fileAttachment)
End If
End If
smtpClient.Send(emailMsg)
Return True
Catch
Return False
End Try
End Function
Authenticated
The following function sends an authenticated Email that means it uses email server, port number, user id and password as additional parameters. You may wish to turnoff the authentication anytime and use the same code. If you have customers who have use your application to send emails to their respective clients, you will pass their stored email server information dynamically through this function.
Public Function SendAuthenticatedEmail(ByVal addressFrom As String, ByVal addressTo As String, _
ByVal subject As String, ByVal bodyText As String, _
ByVal isAutorizationRequired As Boolean, _
ByVal emailServer As String, ByVal emailServerPort As Integer, _
ByVal emailUser As String, ByVal emailUserPassword As String) As Boolean
Try
Dim emailMsg As New System.Net.Mail.MailMessage(addressFrom, addressTo, subject, bodyText)
Dim smtpClient As New Net.Mail.SmtpClient
If emailServer IsNot Nothing Then
smtpClient = New Net.Mail.SmtpClient(emailServer, emailServerPort)
If isAutorizationRequired Then
If emailUser IsNot Nothing And emailUserPassword IsNot Nothing Then
smtpClient.UseDefaultCredentials = False
smtpClient.Credentials = New System.Net.NetworkCredential(emailUser, emailUserPassword)
End If
Else
smtpClient.Credentials = Net.CredentialCache.DefaultNetworkCredentials()
End If
ElseIf smtpClient.Host Is Nothing Then
smtpClient = New Net.Mail.SmtpClient(Environment.MachineName)
smtpClient.Credentials = Net.CredentialCache.DefaultNetworkCredentials()
End If
smtpClient.Send(emailMsg)
Return True
Catch
Return False
End Try
End FunctionEnd Sub
Web.Config
You can define smtp mail settings under system.net of the web.config file. If you do so, then you do not need to provide the same (as shown above) in your code.
<system.net>
<mailSettings>
<smtp deliveryMethod="network" from="you@yourdomain.com" >
<network host="mysmtpserver.net" port="25" userName="username" password="password" defaultCredentials="true/false"/>
</smtp>
</mailSettings>
</system.net>
If you are using default settings of smtp then you do not need to provide port, userName, password and defaultCredentials value. In case of secure authentication, you will need to provide userName, password and set the defaultCredentials value to false.