views:

87

answers:

2

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
                .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
+1  A: 

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.

M.A. Hanin
I was thinking the same thing (spam-abuse).
Saif Khan
If you're completely local you should be able to control the throttle rate on the Exchange server somewhere. Also, if you're only sending once a month then just put a delay in between each send, maybe 5 or 10 seconds or so. A third option, which most people don't like, you could send TO no one (or yourself) and then batch BCC to people of the same domain. The same domain part is important because it only creates a single SMTP message.
Chris Haas
Chris, your posts are a sheer pleasure. BCC is actually a great option, and maybe the OP should use it (maybe the recipients do not want to be exposed).
M.A. Hanin
A: 

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.

drachenstern
Yes, the server can handle the load. And the amount of mails to be send will not exceed 700 and the process will be once a month...can I drop to the exchange server SMTP accessible folder? Where can I find info on this.
Saif Khan
http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.pickupdirectorylocation.aspx ~ and while this is older, it gives you a start in the right direction (and you wanna be able to handle this in any IIS environment yeah?) ~ http://support.microsoft.com/kb/247958 ~ Also, this is the time when you have to get your sysadmin involved. You're going to be working with Exchange now, and so that has to be configured as well (but boy is it worth it).
drachenstern