2012-06-08 87 views
0

我試圖發送電子郵件邀請到未知的號碼。的人,我希望我可以使用線程來改善速度方面的過程。任何人都可以請建議,如果我做了什麼將會有所幫助或完全不重要。謝謝。.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 

回答

2

不,這不會幫助,因爲你沒有做任何並行的工作,你只是把在後臺線程的工作。這將有助於保持UI的暢通,但不會加快執行速度。

正確的方法是將線程產生在sendEmail子中的For循環中。但是,請不要這樣做。這真的不是線程的好選擇。

+0

感謝您的解釋。 – k80sg