.Net +电子邮件线程

时间:2012-06-08 02:31:42

标签: vb.net

我正在尝试向未知的号码发送电子邮件邀请。人和我希望我可以使用线程来提高速度方面的过程。任何人都可以建议,如果我所做的将是任何帮助或完全无关紧要。感谢。

   Public Shared Function Emailing_FriendInvitation(ByVal memberID As String _
                                   , ByVal referrerCode As String _
                                   , ByVal enquiryCode As String _
                                   , ByVal outletCode As String _
                                   , ByVal templateName As String _
                                   , ByVal emailAddressLists As List(Of String) _
                                   , ByVal dbtrans As DbTransaction _
                                   , ByVal dbConnection As DbConnection _
                                   , ByVal addedBy As String _
                                   , ByVal customerCode As String) As Boolean
        Dim dbCommand As DbCommand = Nothing
        Dim isSuccess As Boolean = False
        Try
            Dim senderEmailName As String = String.Empty
            Dim senderEmail As String = String.Empty
            Dim receipientEmail As String = String.Empty

            Dim receipientName As String = "Dear Sir/Madam"
            Dim emailSubject As String = String.Empty
            Dim emailBody As String = String.Empty

            Dim ds As DataSet = Nothing

            If dbConnection.State <> ConnectionState.Open Then
                dbConnection.Open()
            End If
            dbCommand = GetStoredProcedureCommand("FriendsInvitation")
            dbCommand.Connection = dbConnection
            If Not dbtrans Is Nothing Then
                dbCommand.Transaction = dbtrans
            End If

            Dim emailAddresses As String
            For Each email In emailAddressLists
                emailAddresses += email + ","
            Next

            AddInParameter(dbCommand, "@MemberID", DbType.String, memberID)
            AddInParameter(dbCommand, "@EmailAddresses", DbType.String, emailAddresses)
            AddInParameter(dbCommand, "@TemplateName", DbType.String, 256, templateName)
            AddInParameter(dbCommand, "@EnquiryCode", DbType.String, 50, enquiryCode)
            AddInParameter(dbCommand, "@OutletCode", DbType.String, 50, outletCode)
            AddInParameter(dbCommand, "@ReferrerCode", DbType.String, 50, referrerCode)
            AddInParameter(dbCommand, "@AddedBy", DbType.String, 50, addedBy)

            ds = ExecuteDataSet(dbCommand)
            senderEmailName = Convert.ToString(IIf(Not IsDBNull(ds.Tables(0).Rows(0)("AutoEmailDisplayName")), ds.Tables(0).Rows(0)("AutoEmailDisplayName"), String.Empty))
            senderEmail = Convert.ToString(IIf(Not IsDBNull(ds.Tables(0).Rows(0)("AutoEmailSender")), ds.Tables(0).Rows(0)("AutoEmailSender"), String.Empty))
            emailSubject = templateName
            emailBody = Convert.ToString(IIf(Not IsDBNull(ds.Tables(0).Rows(0)("AutoEmailContent")), ds.Tables(0).Rows(0)("AutoEmailContent"), String.Empty))
            'emailBlastRecipientAutoID = Convert.ToString(ds.Tables(1).Rows(0)("EmailBlastRecipientAutoID"))
            ' End If
            If Not (String.IsNullOrEmpty(senderEmail) AndAlso String.IsNullOrEmpty(receipientEmail) AndAlso _
              String.IsNullOrEmpty(emailSubject) AndAlso String.IsNullOrEmpty(emailBody)) Then

      emailBody = emailBody.Replace("[REFERRERCODE]", "REFERRERCODE")

      '----Thread-----

      Dim Th = New Thread(Sub() sendEmail(emailAddressLists, senderEmailName, senderEmail,     emailSubject, emailBody))
                Th.Start()
      End If

      '---------------

        Catch ex As Exception
            isSuccess = False
            Throw New DALException(ex, dbCommand, customerCode, "Emailing_FriendInvitation")
        Finally
            If Not dbCommand Is Nothing Then
                dbCommand.Dispose()
            End If
        End Try
        Return isSuccess
    End Function

    Private Shared Sub sendEmail(ByVal emailLst As List(Of String), ByVal senderEmailName As String, ByVal senderEmail As String, ByVal emailSubject As String, ByVal emailBody As String)
        Dim successfulReceipents As String = String.Empty

        For Each emailAdd In emailLst
            Try
                Email.Send(senderEmailName, senderEmail, emailAdd, emailSubject, emailBody, Nothing)
                successfulReceipents += emailAdd + ","
            Catch ex As Exception

            End Try
        Next
    End Sub

1 个答案:

答案 0 :(得分:2)

不,这没有用,因为你没有并行工作,你只是把工作放在后台线程上。这有助于保持UI畅通,但不会加快执行速度。

正确的方法是让线程在sendEmail sub中的For循环中生成。但是,请不要这样做。这确实不适合线程化。