• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 313
  • Last Modified:

Send email process..

Hi!

Have made a application that send out email to customers.
But attach PDF files to every email i send out..
The system use about 15 sec on every email..

I want this function to be run in the background..
So the user of the system, can run other functions
while the email process is still running.

How can i do that ?

0
team2005
Asked:
team2005
  • 3
  • 3
  • 2
  • +1
1 Solution
 
CodeCruiserCommented:
You can do that by running the email sending in the background :-)
0
 
CodeCruiserCommented:
My answer is vague isn't it? That's because your question is vague. Show us some code. Tell us how you are sending the email. Is a winforms app, a windows service, a web app?
0
 
team2005Author Commented:
Hi!

I have a winform made in vb.net.
If example i want to send 1000 emails, the user of this system
cant wait for this job to finish... 1000 email can take hours to complete..
They must be able to run other functions, while the system send emails.
And when the job is complete, it must inform the user..

Maby have a smal top form that shows hows status on the job
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Rose BabuSenior Team ManagerCommented:
Hi,

you need to implement threads in your WinForm program with BackgroundWorker control.

the three concepts are involved in background worker process.

backgroundWorker1_DoWork
backgroundWorker1_ProgressChanged
backgroundWorker1_RunWorkerCompleted

write your email sending process in backgroundWorker1_DoWork, then start it by backgroundWorker1.RunWorkerAsync() function

look at the below tutorials will help you lot.

http://www.dotneat.net/2009/02/10/BackgroundworkerExample.aspx

http://www.codeproject.com/KB/cpp/BackgroundWorker_Threads.aspx

http://www.dotnetperls.com/backgroundworker


Happy Programming.
0
 
Yannick LapierreSoftware DirectorCommented:
You can use use a task object to let your mail sending methode work in back ground.
 it's easy to do if you have 1 start methode to send your mail: and use a Continuswith to fire an event and inform user.

You can see in the attached code an exemple of use.

If you want more detail, give use an exemple of your code (Methode declaration my be enought).
Private Function Compress(ByVal archive As ICompressArchiveInfo) As String Implements IArchiver.Compress

            Dim workItem As ArchiverWorkItem = CreateWorkItem()

            workItem.CompressionData = archive
            workItem.Task = CreateWorkTask(workItem)

            _WorkItemlist.Add(workItem.Id, workItem)

            workItem.Task.Start()

            Return workItem.Id.ToString

        End Function


    Private Function CreateWorkTask(item As ArchiverWorkItem) As System.Threading.Tasks.Task

            Dim result As System.Threading.Tasks.Task = Nothing

            If item.ActionType = WorkItemActionType.Compression Then
                result = New System.Threading.Tasks.Task(AddressOf DoCompressWork, CObj(item), Threading.Tasks.TaskCreationOptions.LongRunning + Threading.Tasks.TaskCreationOptions.PreferFairness)
            End If

            If item.ActionType = WorkItemActionType.Extraction Then
                result = New System.Threading.Tasks.Task(AddressOf DoExtractWork, CObj(item), Threading.Tasks.TaskCreationOptions.LongRunning + Threading.Tasks.TaskCreationOptions.PreferFairness)
            End If

            If result IsNot Nothing Then

                Dim CleanupTask As System.Threading.Tasks.Task
                CleanupTask = result.ContinueWith(Sub()
                                                      FinishWork(item.Id)
                                                  End Sub)

            End If

            Return result

        End Function

        Private Sub DoCompressWork(archive As ArchiverWorkItem)

            Dim targetFile As String = archive.CompressionData.Fullpath
            Dim archiver As New Gfk.GfkKynetec.SharedLibraries.Component.Archive.Engine(archive.CancellationToken.Token)
            Dim Ar As New Gfk.GfkKynetec.SharedLibraries.Component.Archive.ArchiveConstructorItem
            Dim ArchivedItem As Gfk.GfkKynetec.SharedLibraries.Component.Archive.ArchivedItem

            AddHandler archiver.Compressing, AddressOf CompressingEventHandler

            For Each FilePath As String In archive.CompressionData.FileList
                Ar.Files.Add(FilePath)
            Next

            For Each DirectoryPath As String In archive.CompressionData.DirectoryList
                Ar.Directories.Add(New Gfk.GfkKynetec.SharedLibraries.Component.Archive.DirectoryItem(DirectoryPath))
            Next

            ' Add attibut to Archive comments
            For Each elt As IAttribut In archive.CompressionData.Attributs
                Ar.Attributes.Add(elt.Name, elt.Value)
            Next

            Ar.ArchivePath = targetFile

            ' # Callback #
            archive.Callback.Start(archive.Id.ToString)
            ArchivedItem = archiver.Compress(Ar)

            If archiver.IsCancelled Then
                '# Callback #
                archive.Callback.Cancelled(archive.Id.ToString)
            End If

        End Sub

        Private Sub FinishWork(id As Guid)

            ' # Callback #
            _WorkItemlist.Item(id).Callback.Completed(id.ToString)
            _WorkItemlist.Remove(id)

        End Sub

Open in new window

0
 
team2005Author Commented:
Hi!

Have this included code now, but it dosent execute the sub -> sendeALLEemailer

Why ?




Private Sub worker_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)

        MsgBox("ALLE EMAILER ER SENDT", MsgBoxStyle.Information)
        Me.Button10.Enabled = True

    End Sub

    'worker.WorkerReportsProgress = True
    'AddHandler worker.DoWork, New DoWorkEventHandler(AddressOf worker_DoWork)
    'AddHandler worker.RunWorkerCompleted, New RunWorkerCompletedEventHandler(AddressOf worker_RunWorkerCompleted)
    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        Me.Button10.Enabled = False
        worker.WorkerReportsProgress = True
        AddHandler worker.DoWork, New DoWorkEventHandler(AddressOf sendeALLEemailer)
        AddHandler worker.RunWorkerCompleted, New RunWorkerCompletedEventHandler(AddressOf worker_RunWorkerCompleted)

    End Sub
    Private Sub sendeALLEemailer()
        Dim i As Integer
        Dim s1 As String

        Dim s11(100) As String
        Dim t1, t2 As Integer


        Me.test.Text = Me.headdummyen.Text

        t1 = 0
        t2 = header.Lines.Length
        For i = 0 To (t2 - 1)
            s11(i) = header.Lines(i)
            t1 = t1 + 1
        Next
        test.Text += Environment.NewLine + "<table>"

        For i = 0 To (t2 - 1)
            test.Text += Environment.NewLine + "<tr><td>" & s11(i) & "</td></tr>"

        Next
        test.Text += Environment.NewLine + "</table>"
        test.Text += Environment.NewLine + "</body>"
        test.Text += Environment.NewLine + "</html>"

        i = 1

        For Each itm As ListViewItem In kundermail.Items


            ' Navn

            Me.txtBody.Text = ""
            Me.txtBody.Text = Me.body2.Text
            Dim s22 As String = impkunderliste(i).navn
            'Me.Label11.Text = (i + 1) & "/" & Me.ListBox3.Items.Count
            Me.txtBody.Text = Me.txtBody.Text.Replace("*NAVN*", impkunderliste(i).navn & " " & impkunderliste(i).etternavn)
            Me.txtBody.Text = Me.txtBody.Text.Replace("*FAKTDATO*", hentfakturanr(impkunderliste(i).kundeid, 2))
            Me.txtBody.Text = Me.txtBody.Text.Replace("*MEDLEMSNR*", impkunderliste(i).medlemsnr)
            Me.txtBody.Text = Me.txtBody.Text.Replace("*FAKNR*", hentfakturanr(impkunderliste(i).kundeid, 1))
            Me.txtBody.Text = Me.txtBody.Text.Replace("*ADR1*", impkunderliste(i).adresse)
            Me.txtBody.Text = Me.txtBody.Text.Replace("*ADR2*", impkunderliste(i).postnr & " " & impkunderliste(i).sted)
            Me.txtBody.Text = Me.txtBody.Text.Replace("*BESKRIVELSE*", "St√łttemedlemsskap SingelSammen! 2011")
            Me.txtBody.Text = Me.txtBody.Text.Replace("*ANT*", "1")
            Me.txtBody.Text = Me.txtBody.Text.Replace("*PRIS*", "290,-")
            Me.txtBody.Text = Me.txtBody.Text.Replace("*RAB*", "0")
            Me.txtBody.Text = Me.txtBody.Text.Replace("*SUM*", "290,-")
            Me.txtBody.Text = Me.txtBody.Text.Replace("*BILDEFIL*", "http://www.singelnett.no/images/singelsammenfaktura.png")

            Dim sjekkstatus As Boolean = sendeemail(impkunderliste(i).epost, i)

            If sjekkstatus = True Then
                Me.kundermail.Items(0).Selected = True
                Me.kundermail.Items.Remove(Me.kundermail.SelectedItems(0))
            End If

            i = i + 1
            Me.Label10.Text = Trim$(Str(i) & "/" & Str(antlinjerreg - 1))

        Next
    End Sub

Open in new window

0
 
CodeCruiserCommented:
The code currently in sendeALLEemailer needs to be moved to DoWork of backgroundworker.
0
 
Yannick LapierreSoftware DirectorCommented:
Hi,

Sample of how you can implement your code with task aproche

Public Class Form1

    Private _cancelToken As System.Threading.CancellationTokenSource


    Public Sub Run()

        _cancelToken = New System.Threading.CancellationTokenSource

        Dim workTask As New System.Threading.Tasks.Task(AddressOf sendeALLEemailer, _cancelToken.Token)

        workTask.Start()
        
    End Sub

    Private Sub worker_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)

        MsgBox("ALLE EMAILER ER SENDT", MsgBoxStyle.Information)
        Me.Button10.Enabled = True

    End Sub

    Public Sub CancelWork()

        _cancelToken.Cancel()

    End Sub

    'worker.WorkerReportsProgress = True
    'AddHandler worker.DoWork, New DoWorkEventHandler(AddressOf worker_DoWork)
    'AddHandler worker.RunWorkerCompleted, New RunWorkerCompletedEventHandler(AddressOf worker_RunWorkerCompleted)
    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        Me.Button10.Enabled = False

        Run()

    End Sub

    Private Sub sendeALLEemailer()
        Dim i As Integer
        Dim s1 As String

        Dim s11(100) As String
        Dim t1, t2 As Integer

        AddHandler worker.DoWork, New DoWorkEventHandler(AddressOf sendeALLEemailer)
        AddHandler worker.RunWorkerCompleted, New RunWorkerCompletedEventHandler(AddressOf worker_RunWorkerCompleted)

        Me.test.Text = Me.headdummyen.Text

        t1 = 0
        t2 = header.Lines.Length
        For i = 0 To (t2 - 1)
            s11(i) = header.Lines(i)
            t1 = t1 + 1
        Next
        test.Text += Environment.NewLine + "<table>"

        For i = 0 To (t2 - 1)
            test.Text += Environment.NewLine + "<tr><td>" & s11(i) & "</td></tr>"

        Next
        test.Text += Environment.NewLine + "</table>"
        test.Text += Environment.NewLine + "</body>"
        test.Text += Environment.NewLine + "</html>"

        i = 1

        For Each itm As ListViewItem In kundermail.Items

            If _cancelToken.IsCancellationRequested Then
                ' You can add code to clean your system before cancellation
                Exit Sub
            End If


            ' Navn

            Me.txtBody.Text = ""
            Me.txtBody.Text = Me.body2.Text
            Dim s22 As String = impkunderliste(i).navn
            'Me.Label11.Text = (i + 1) & "/" & Me.ListBox3.Items.Count
            Me.txtBody.Text = Me.txtBody.Text.Replace("*NAVN*", impkunderliste(i).navn & " " & impkunderliste(i).etternavn)
            Me.txtBody.Text = Me.txtBody.Text.Replace("*FAKTDATO*", hentfakturanr(impkunderliste(i).kundeid, 2))
            Me.txtBody.Text = Me.txtBody.Text.Replace("*MEDLEMSNR*", impkunderliste(i).medlemsnr)
            Me.txtBody.Text = Me.txtBody.Text.Replace("*FAKNR*", hentfakturanr(impkunderliste(i).kundeid, 1))
            Me.txtBody.Text = Me.txtBody.Text.Replace("*ADR1*", impkunderliste(i).adresse)
            Me.txtBody.Text = Me.txtBody.Text.Replace("*ADR2*", impkunderliste(i).postnr & " " & impkunderliste(i).sted)
            Me.txtBody.Text = Me.txtBody.Text.Replace("*BESKRIVELSE*", "St√łttemedlemsskap SingelSammen! 2011")
            Me.txtBody.Text = Me.txtBody.Text.Replace("*ANT*", "1")
            Me.txtBody.Text = Me.txtBody.Text.Replace("*PRIS*", "290,-")
            Me.txtBody.Text = Me.txtBody.Text.Replace("*RAB*", "0")
            Me.txtBody.Text = Me.txtBody.Text.Replace("*SUM*", "290,-")
            Me.txtBody.Text = Me.txtBody.Text.Replace("*BILDEFIL*", "http://www.singelnett.no/images/singelsammenfaktura.png")

            Dim sjekkstatus As Boolean = sendeemail(impkunderliste(i).epost, i)

            If sjekkstatus = True Then
                Me.kundermail.Items(0).Selected = True
                Me.kundermail.Items.Remove(Me.kundermail.SelectedItems(0))
            End If

            i = i + 1
            Me.Label10.Text = Trim$(Str(i) & "/" & Str(antlinjerreg - 1))

        Next
    End Sub

End Class

Open in new window

0
 
team2005Author Commented:
thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now