I have finally got around to releasing a cool assembly (dll) that will send SMTP mail for you. It's written in .NET 1.1 and either uses a given SMTP server or the current servers SMTP service (default), if it's installed. The cool thing is that if you are using the server SMTP service and it's not started, then this assembly will try to start it for you.
We are currently using this assembly where I work to send email from ASP.NET and SQL Server.
Here is an example of using the assembly to send mail with the current server SMTP service:
Dim objTest As New VSDNTips.Mail.WebMail()
objTest.Send("#ITDevelopers@mycompany.com", "leaddeveloper@mycompany.com", _ "Work Project Tomorrow", "Work Proceeds tomorrow as planned.", _ VSDNTips.Mail.WebMail.MailFormat.HTML)
Here is an example of using an external SMTP server:
Dim objTest As New VSDNTips.Mail.WebMail("mail.mycompany.com")
Here is a real world example that sends email based on an unhandled exception in ASP.NET:
Private Sub Global_Error(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Error Dim serverError As Exception = Server.GetLastError().GetBaseException() Dim errorMessage As New System.Text.StringBuilder("Error Caught in Global_Error event:" & vbCrLf)
errorMessage.AppendFormat("Error in: {0}{1}", Request.Url.ToString(), vbCrLf) errorMessage.AppendFormat("Error Message: {0}{1}", serverError.Message.ToString(), vbCrLf) errorMessage.AppendFormat("Stack Trace: {0}{1}", serverError.StackTrace.ToString(), vbCrLf)
Try Dim mailServer As New VSDNTips.Mail.WebMail("mail.mycompany.com") mailServer.Send("#ITDevelopers@mycompany.com", "leaddeveloper@mycompany.com", _ String.Format("Error in {0} v{1}", System.Reflection.Assembly.GetExecutingAssembly.GetName.Name, _ System.Reflection.Assembly.GetExecutingAssembly.GetName.Version().ToString()), _ errorMessage.ToString, VSDNTips.Mail.WebMail.MailFormat.Text) Catch End Try
Response.Write("<h3>There has been an error in this program. The MyCompany IT staff has been alerted. Please try again later.</h3>")
Server.ClearError() End Sub
To download, click on the link below:
VSDNTIPS.Mail.zip (2.67 KB)