开发者

Performance problem with System.Net.Mail

I have this unusual problem with mailing from my app. At first it wasn't working (getting unable to relay error crap) anyways I added the proper authentication and it works. My problem now is, if I try to send around 300 emails (each with a 500k attachment) the app starts hanging around 95% thru the process.

Here is some of my code which is called for each mail to be sent

 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
       开发者_如何学Python         .Credentials = New NetworkCredential("username", "password")
                .Send(mail)
            End With
        End Using
        With item
            .SentStatus = True
            .DateSent = DateTime.Now.Date
            .Save()
        End With
        Return

I was thinking, can I just prepare all the mails and add them to a collection then open one SMTP conenction and just iterate the collection, calling the send like this

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


The limitations you encounter are prolly enforced by the SMTP server, not your code. SMTP servers are very prone to spam-abuse, and therefore have mechanisms to prevent such abuse.

Sending each email individually isn't always going to work, you're competing with other - more sophisticated - mechanisms.

But technically, yes, you can write a code that sends them individually.


For the size and number you're talking about, my advice is to drop them on an SMTP accessible folder and let the SMTP server deliver from that folder. That'll be faster and saner.

500K attachments in memory are kinda resource intensive, and allocating RAM for 300 at a pop is roughly 200MB RAM (overhead for holding onto resources, creating a new message each time, opening libraries, etc). So a second question is can your server handle this? Just an observation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜