从我的应用程序发送邮件时,我遇到了这个不寻常的问题。起初它不起作用(无法传递错误废话),但我添加了正确的身份验证并且它起作用了。我现在的问题是,如果我尝试发送大约 300 封电子邮件(每封电子邮件带有 500k 附件),则应用程序开始在整个过程中挂起大约 95%。
这是我的一些代码,用于发送每封邮件
Using mail As New MailMessage()
With mail
.From = New MailAddress(My.Resources.EmailFrom)
For Each contact As Contact In Contacts
.To.Add(contact.Email)
Next
.Subject = "Accounting"
.Body = My.Resources.EmailBody
'Back the stream up to the beginning orelse the attachment
'will be sent as a zero (0) byte file.
attachment.Seek(0, SeekOrigin.Begin)
.Attachments.Add(New Attachment(attachment, String.Concat(Item.Year, Item.AttachmentType.Extension)))
End With
Dim smtp As New SmtpClient("192.168.1.2")
With smtp
.DeliveryMethod = SmtpDeliveryMethod.Network
.UseDefaultCredentials = False
.Credentials = New NetworkCredential("username", "password")
.Send(mail)
End With
End Using
With item
.SentStatus = True
.DateSent = DateTime.Now.Date
.Save()
End With
Return
我在想,我可以只准备所有邮件并将它们添加到一个集合中,然后打开一个 SMTP 连接并迭代集合,像这样调用发送
Using mail As New MailMessage()
...
MailCollection.Add(mail)
End Using
...
Dim smtp As New SmtpClient("192.168.1.2")
With smtp
.DeliveryMethod = SmtpDeliveryMethod.Network
.UseDefaultCredentials = False
.Credentials = New NetworkCredential("username", "password")
For Each mail in MainCollection
.Send(mail)
Next
End With