我正在开发邮件系统,逐个发送相同的电子邮件,但我不想使用密件抄送,因为发送的信息具有从数据库查询的不同信息。我查询数据库并将所有电子邮件地址添加到一个数组中,然后使用数组我调用一个函数来发送发送地址的电子邮件和html页面引用作为参数。我的问题是,邮件的重复次数与电子邮件收件人的次数一样多。例如,对于3个收件人,每个收件人都会收到三次邮件。我一直使用msg.to.clear()来清除收件人,但程序仍然会调用该函数三次。
受保护的子btnSend_Click(发件人为对象,e为EventArgs)处理 btnSend.Click SendHTMLMail()End Sub
Public Sub SendHTMLMail()
Dim toaddress As String = "" Dim red As String = (Server.MapPath("~/HTMLPage.htm")) Dim ta As New DataSet1TableAdapters.pruebasTableAdapter Dim dt As DataSet1.pruebasDataTable Dim r As DataSet1.pruebasRow Dim x As Integer = ta.conteo() Dim s(x - 1) As String Dim y = x Dim z = 0 dt = ta.GetDataBy1() Dim mystring = "" For Each r In dt s(z) = r.email z = z + 1 Next For i As Integer = 0 To (y - 1) sendEmailtoaddress(s(i), red) MsgBox("sent" + s(i).ToString) mystring += s(i).ToString + ", " Next Label1.Text = mystring 'Label1.Text = "Enviando mensaje!" 'Label1.Text += "Mensaje enviado exitosamente!"
End Sub
共享函数sendEmailtoaddress(ByVal toaddress As String,ByVal serverpath As String)As Integer
Dim x As Integer Dim reader As New StreamReader(serverpath) Dim readFile As String = reader.ReadToEnd() Dim myString As String = "" myString = readFile Dim Msg As New MailMessage() 'Dim fromMail As New MailAddress("info@gc.hn") Dim fromMail As New MailAddress("ramonhasbun@gmail.com") ' Sender e-mail address. Msg.From = fromMail ' Recipient e-mail address. Dim emailad As New MailAddress(toaddress) Msg.To.Clear() Msg.To.Add(emailad) ' Subject of e-mail Msg.Subject = "Bufete González Corrales" Msg.Body = myString.ToString() Msg.IsBodyHtml = True Dim sSmtpServer As String = "" 'sSmtpServer = "smtp.live.com" sSmtpServer = "smtp.gmail.com" Dim a As New SmtpClient() a.Host = sSmtpServer a.Port = 587 ' a.Credentials = New System.Net.NetworkCredential("info@gc.hn", "PASSWORD") a.Credentials = New System.Net.NetworkCredential("ramonhasbun@gmail.com", "PASSWORD") a.EnableSsl = True a.Send(Msg) reader.Dispose() Return x
我在邮件发送功能的末尾添加了a.dispose()函数,现在我只获得了两个邮件副本。我认为For i As Integer = 0 To(y - 1)...被调用两次。
程序发送前3封电子邮件,然后重新发送。
答案 0 :(得分:0)
mystring += s(i).ToString + ", "
您只是在循环中附加了下一条消息,而没有清除旧消息。
答案 1 :(得分:0)
问题解决后,将代码重写为visual studio中的新项目,使用数组逐个存储和调用电子邮件地址。
填充数组......
For Each r In dt
emails(j) = r.email
j = j + 1
Next
阅读数组并逐个发送电子邮件......
For i As Integer = 0 To (x - 1)
Mail.Subject = "Subject"
Mail.To.Clear()
Mail.To.Add(emails(i).ToString)
Mail.From = New MailAddress("EMAIL ADDRESS")
Mail.Body = myString.ToString()
Mail.IsBodyHtml = True
Dim SMTP As New SmtpClient("SMTP SERVER")
SMTP.EnableSsl = True
SMTP.Credentials = New System.Net.NetworkCredential(TextBox1.Text,TextBox2.Text)
SMTP.Port = "PORT NUMBER"
SMTP.Send(Mail)
MsgBox("Message sent to: " + emails(i).ToString())
Next