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

ASP.NETMicrosoft IIS Web ServerExchange

Avatar of undefined
Last Comment
Bardobrave

8/22/2022 - Mon
Bardobrave

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?
ASKER
pothios

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
Bardobrave

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck