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.SmtpClientobjEMail.Host = "localhost" ' If you have not defined the host in web.configobjEMail.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 SubPlain Text
Public Function SendEmail(ByVal addressFrom As String, ByVal addressTo As String, _ByVal subject As String, ByVal bodyText As String) As BooleanTry
Dim emailMsg As New System.Net.Mail.MailMessage(addressFrom, addressTo, subject, bodyText)Dim smtpClient As New System.Net.Mail.SmtpClient()smtpClient.Send(emailMsg)Return TrueCatch
Return FalseEnd TryEnd 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 BooleanTry
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) ThenIf Not emailMsg.Attachments Is Nothing ThenDim fileAttachment As New Net.Mail.Attachment(fileNameWithFullPath)emailMsg.Attachments.Add(fileAttachment)End IfEnd IfsmtpClient.Send(emailMsg)Return TrueCatch
Return FalseEnd TryEnd 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 BooleanTry
Dim emailMsg As New System.Net.Mail.MailMessage(addressFrom, addressTo, subject, bodyText)Dim smtpClient As New Net.Mail.SmtpClientIf emailServer IsNot Nothing ThensmtpClient = New Net.Mail.SmtpClient(emailServer, emailServerPort)
If isAutorizationRequired ThenIf emailUser IsNot Nothing And emailUserPassword IsNot Nothing ThensmtpClient.UseDefaultCredentials = False
smtpClient.Credentials = New System.Net.NetworkCredential(emailUser, emailUserPassword)
End IfElse
smtpClient.Credentials = Net.CredentialCache.DefaultNetworkCredentials()End IfElseIf smtpClient.Host Is Nothing ThensmtpClient = New Net.Mail.SmtpClient(Environment.MachineName)
smtpClient.Credentials = Net.CredentialCache.DefaultNetworkCredentials()End IfsmtpClient.Send(emailMsg)Return TrueCatch
Return FalseEnd TryEnd Function
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.
Friday, September 22, 2006
.NET 2.0 Email
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment