Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

.net email questions

Hi,

I am writing a web site which sends order confirmations to users via email.

Two changes that are needed are as follows:

1) PDF files are going to be attached to the email. It could be one, it could be 20!

I have written the code to attach any number of PDF files to the confirmation email, but the resulting email could be too big for some email servers. Also the time it takes to generate can leave the user (in the browser) wondering in the application has crashed.

I was thinking of "chopping" up the emails into several parts e.g. part 1 is the text confirmation, then parts 2 onwards would contain each PDF.

This would get around the size issue but I would still be left with the slowdown issue.

If I started the email generation in another thread, would the user get the "Thank you for your order" page immediately and then be safe to close the browser leaving the other thread doing it's work. Obviously the ASP.NET app is still running on the web server anyway.

What would be the best way to deal with both these issues?

2) directly emailing a 'proof' to a user via email client.

When the user clicks on an 'email PDF' button, I need to create an email with the pdf attached and invoke that users email client so that they can then choose a recipient and add extra text before sending it.

How do I create an email message and invoke the users email client in .net?

I am guessing that I would not be using webmail.


Thanks in advance

Mike Grace
Tuesday, October 12, 2004

Definitely use the threaded model for sending the emails, and also use your multiple email stratagey for size restrictions. You could also do a sum(filesize) and only do multiple emails if needed.

You could also just email them a link where they could download the PDF, if that is a option.

Below is an example of a class for emailing (threaded). just create an instance of the class and call the sendSimpleEmail(). It expects there to be a var in your web.config file, and a few other things you will need to strip out (like the errorLogger calls), but it should get you started in the right direction.

Imports System
Imports System.Configuration
Imports System.Data
Imports System.Data.OracleClient
Imports System.Web.Mail
Imports System.Web.Mail.MailMessage
Imports System.Threading

Namespace qTools
    Public Class eMail
        Inherits System.ComponentModel.Component

#Region " Component Designer generated code "

        Public Sub New(ByVal Container As System.ComponentModel.IContainer)
            MyClass.New()

            'Required for Windows.Forms Class Composition Designer support
            Container.Add(Me)
        End Sub

        Public Sub New()
            MyBase.New()

            'This call is required by the Component Designer.
            InitializeComponent()

            'Add any initialization after the InitializeComponent() call

        End Sub

        'Component overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

        'Required by the Component Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Component Designer
        'It can be modified using the Component Designer.
        'Do not modify it using the code editor.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            components = New System.ComponentModel.Container
        End Sub

#End Region


        '*************************************************************************
        '
        '  sendSimpleEmail
        ' 
        '  send a simple Email using the SMTP Mail Transport
        '
        '*************************************************************************
        Public Function sendSimpleEmail(ByVal SED As simpleEmailData)
            If Len(SED.eTo) > 0 And _
              Len(SED.eFrom) > 0 And _
              Len(SED.eSubject) > 0 And _
              Len(SED.eBody) > 0 Then

                If ConfigurationSettings.AppSettings("USE_THREADED_EMAILER") = "TRUE" Then
                    threaded_sendEmail(SED)
                Else
                    Try
                        SmtpMail.SmtpServer = ConfigurationSettings.AppSettings("SMTP_SERVER")
                        SmtpMail.Send(SED.eFrom, SED.eTo, SED.eSubject, SED.eBody)
                    Catch ex As Exception
                        Dim el As errorLogger = New errorLogger
                        Dim ed As exceptionData = New exceptionData

                        ed.FunctionName = "sendSimpleEmail"

                        ed.BreadCrumb = Date.Now.ToString
                        ed.Message = ex.Message
                        ed.Name = Me.ToString
                        ed.Source = ex.Source
                        ed.StackTrace = ex.StackTrace
                        ed.TargetSiteName = ex.TargetSite.Name

                        'log the error
                        el.logError(ed)

                        'error occured sending email, alert user somehow. (message in message center?)
                    End Try
                End If

            End If
        End Function


        '*************************************************************************
        '
        '  threaded_sendEmail
        ' 
        '  thread spawner to send email in background so app can continue
        '
        '*************************************************************************
        Public Sub threaded_sendEmail(ByVal SED As simpleEmailData)
            Dim tws As New Threaded_Emailer(SED)
            Dim t As New Thread(AddressOf tws.ThreadProc)

            t.Priority = ThreadPriority.Lowest
            t.IsBackground = True
            t.Start()
        End Sub

    End Class


    '*******************************************************
    '
    ' threaded_Emailer Class
    '
    ' Class to send emails using Threads
    '
    '*******************************************************
    Public Class Threaded_Emailer
        Private eMessage As MailMessage

        Public Sub New(ByVal SED As simpleEmailData)
            eMessage = New MailMessage
            eMessage.To = SED.eTo
            eMessage.From = SED.eFrom
            eMessage.Subject = SED.eSubject
            eMessage.Body = SED.eBody
            eMessage.BodyFormat = MailFormat.Html
        End Sub

        Public Sub ThreadProc()
            'send email
            Try
                SmtpMail.SmtpServer = ConfigurationSettings.AppSettings("SMTP_SERVER")
                SmtpMail.Send(eMessage)
            Catch ex As Exception
                Dim el As errorLogger = New errorLogger
                Dim ed As exceptionData = New exceptionData

                ed.FunctionName = "ThreadProc"
                ed.BreadCrumb = Date.Now.ToString
                ed.Message = ex.Message
                ed.Name = Me.ToString
                ed.Source = ex.Source
                ed.StackTrace = ex.StackTrace
                ed.TargetSiteName = ex.TargetSite.Name

                'log the error
                el.logError(ed)

                'error occured sending email, alert user somehow. (message in message center?)
            End Try
        End Sub
    End Class

    '*******************************************************
    '
    ' simpleEmailData Class
    '
    ' Class to hold basic info for simple eMail message
    '
    '*******************************************************
    Public Class simpleEmailData
        Public eTo As String
        Public eFrom As String
        Public eSubject As String
        Public eBody As String
    End Class

End Namespace

Roger
Wednesday, October 13, 2004

Thanks Roger,

I will look into this.

Mike Grace
Thursday, October 14, 2004

This is what messagequeues are for. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/bdadotnetasync1.asp

Just me (Sir to you)
Monday, October 18, 2004

vgcgfgdgfdgfdgdfgfdgfdgfd

fgfgf
Friday, November 19, 2004

*  Recent Topics

*  Fog Creek Home