這個問題是從超級用戶遷移過來的,因為可以在 Stack Overflow 上回答。 7 天前遷移 。
我正在嘗試向申請該職位的候選人發送電子郵件,有時我要發送數百甚至數千封郵件,因此代碼會有所幫助。
這是我寫的,但問題是郵件在同一個地址上多次發送(在某些地址上發送一次或兩次,有時甚至更多)。
對以下代碼的一些解釋。代碼讀取活動作業資料庫并選擇尚未聯系的候選人(資料庫中的 FMZ 列,“X”),然后讀取另一個資料庫中的候選人唯一 ID,以獲取將要發送郵件的電子郵件地址。即使SmtpServer.Send(mail)是下面的基本 msgbox也只為每個候選人激活一次,但郵件會發送多次。這是代碼。
'Step 1 - All candidates in DBAP that applied for ClientID/Job ID
For i As Integer = 0 To dsPoslovi.Tables(0).Rows.Count - 1
If dsPoslovi.Tables(0).Rows(i)(1).ToString() = ATS_IDKlijent AndAlso dsPoslovi.Tables(0).Rows(i)(2).ToString() = ATS_IDPosao Then
'Step 2 - Send mails to those not contacted yet (X in FMZ column)
If dsPoslovi.Tables(0).Rows(i)(8).ToString() <> "X" Then
IDKandidataAP = dsPoslovi.Tables(0).Rows(i)(0).ToString()
IDKandidataAP2 = dsPoslovi.Tables(0).Rows(i)(3).ToString()
'Read mail addresses
For x As Integer = 0 To dsCL.Tables(0).Rows.Count - 1
If dsPoslovi.Tables(0).Rows(i)(3).ToString() = dsCL.Tables(0).Rows(x)(1).ToString() Then
MailKandidata = dsCL.Tables(0).Rows(x)(7).ToString()
If MailKandidata.Contains("@") = True Then
SmtpServer.Send(mail)
MsgBox(MailKandidata)
End If
End If
Next
'Update active job database
Dim cmd As New OleDbCommand(query, myCONN)
myCONN.Open()
cmd.Parameters.AddWithValue("FMZ", "X")
cmd.Parameters.AddWithValue("ID", IDKandidataAP)
cmd.ExecuteNonQuery()
myCONN.Close()
End If
End If
Next
uj5u.com熱心網友回復:
由于我不喜歡打字,我DataSet.Tables將 a決議為 aDataTable并回圈遍歷Rows集合。對我來說,它更具可讀性。
我認為問題出在mail.To集合上。每次迭代都會添加一個收件人并將電子郵件發送給每個收件人。我在回圈內構建集合,并且只在回圈外發送一次郵件。郵件將發送到集合中的每個收件人。
我不確定是怎么回事IDKandidataAP2。它似乎沒有在此方法中使用,并且在每次迭代時都會被覆寫。
我更正了資料庫代碼。Using一旦在回圈之外,連接和命令應該在塊中宣告。我在回圈外構建引數集合,只更改回圈內@ID 引數的值。我猜到了更新字串的樣子。該Add方法優于AddWithValue。我猜到了資料型別。檢查您的資料庫以獲取實際型別。
Private ATS_IDKlijent As String = "" '?
Private ATS_IDPosao As String = "" '?
Private dsCL As New DataSet
Private dsPoslovi As New DataSet
Private mail As New MailMessage
Private SmtpServer As New SmtpClient()
Private IDKandidataAP As String
Private IDKandidataAP2 As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim dt As DataTable = dsPoslovi.Tables(0)
'Step 1 - All candidates in DBAP that applied for ClientID/Job ID
Dim query = "Update SomeTable Set FMZ = @FMZ Where ID = @ID;"
Using myCONN As New OleDbConnection("Your connection string"),
cmd As New OleDbCommand(query, myCONN)
cmd.Parameters.Add("@FMZ", OleDbType.VarChar).Value = "X"
cmd.Parameters.Add("@ID", OleDbType.VarChar)
myCONN.Open()
For Each row As DataRow In dt.Rows
If row(1).ToString() = ATS_IDKlijent AndAlso row(2).ToString() = ATS_IDPosao Then
'Step 2 - Send mails to those not contacted yet (X in FMZ column)
If row(8).ToString() <> "X" Then
IDKandidataAP = row(0).ToString()
IDKandidataAP2 = row(3).ToString()
'Read mail addresses
Dim dt2 As DataTable = dsCL.Tables(0)
For Each row2 As DataRow In dt2.Rows
If row(3).ToString() = row2(1).ToString() Then
Dim MailKandidata = row2(7).ToString()
If MailKandidata.Contains("@") = True Then
Dim ToAddress As New MailAddress(MailKandidata)
mail.To.Add(ToAddress)
MsgBox(MailKandidata)
End If
End If
Next
'Update active job database
cmd.Parameters("@ID").Value = IDKandidataAP
cmd.ExecuteNonQuery()
End If
End If
Next
End Using 'Closes and disposes the connection and disposes the command.
SmtpServer.Send(mail)
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/378928.html
