Link to home
Start Free TrialLog in
Avatar of pothios
pothios

asked on

Sending Email - Urgent Help Needed

Dear experts,

I have written an application which will send out email notifications to my users.

I noticed that sometimes when i trigger a event which will sends out email, my application will load for a very long time to complete but sometimes, it will send almost immediately.

My exchange server is seperate server from my web server, but both of them is within the same network.

I tried to add asynchronous mailing but the random long loading time still persist.

I have also thought about putting the mail into a pickup directory so my web application will not load that long, but i am not sure how to configure it.

Can anyone please kindly advice? Please help....

Thanks.

public static void sendMail(List<User> users, string subject, string body)
        {
            try
            {
                MailMessage message = new MailMessage();
                message.From = new MailAddress(from);

                for (int i = 0; i < users.Count(); i++)
                {
                    message.To.Add(users[i].Email);
                }

                message.Subject = subject;
                message.Body = body;
                message.IsBodyHtml = true;

                SmtpClient emailClient = new SmtpClient();
                emailClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

                 
                Object userState = message;

                try
                {
                    //Send the email asynchronously
                    emailClient.SendAsync(message, userState);
                }
                catch (SmtpException smtpEx)
                {
                    MailMessage errorMailAlert = new MailMessage();
                    errorMailAlert.From = new MailAddress(from);
                    errorMailAlert.To.Add(new MailAddress("randomise@scri.edu.sg"));

                    errorMailAlert.Subject = "Send Mail Failure";
                    errorMailAlert.Body = "system has failed to a mail. Details of the error is stated below and can be viewed" +
                        " in the MailLog under the rand log folder.<br/><br/>Exception details:<br/>" + smtpEx.ToString();
                    errorMailAlert.IsBodyHtml = true;

                    //try sending message to the admin
                    try
                    {
                        //Send the email asynchronously
                        emailClient.SendAsync(errorMailAlert, userState);
                    }
                    catch (SmtpException smtpEx2)
                    {
                        //Error handling here
                        ExceptionUtility.LogMailException(smtpEx2);
                    }
                    //Error handling here
                    ExceptionUtility.LogMailException(smtpEx);

                }
                catch (Exception ex)
                {
                    //Error handling here
                    ExceptionUtility.LogMailException(ex);
                }
            }
            catch
            {
                throw;
            }
        }

Open in new window

Avatar of Bardobrave
Bardobrave
Flag of Spain image

I tried to add asynchronous mailing but the random long loading time still persist.

This makes me think that your bottleneck isn't at the mailing process. Maybe it's located at address retrieval?

Have you noted if the lag is always on the same event or if it produces on different events?
Avatar of pothios
pothios

ASKER

Hi Bardobrave,

 all event that sends mail will randomly cause a long wait time loading the browser. sometimes it's fast, sometimes its very slow. and usually as im using EXT.NET, it starts to prompt timeout error since the process takes longer then the defined timeout period
ASKER CERTIFIED SOLUTION
Avatar of Bardobrave
Bardobrave
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial