Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8163513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:04:41+00:00 2026-06-06T19:04:41+00:00

I am working on an asp.net application for sending emails. I want to make

  • 0

I am working on an asp.net application for sending emails. I want to make sure that if something goes wrong, then it logs errors and move to next record. I am using SmtpClient class to send emails. but right now, If some email address is wrong, it just crashes. How to force it to move to next record ?

My code is like this:

 var EmailsQueue = context.WC_EmailToolQueue.Where(t => t.EmailDate == null).ToList();

            foreach (var email in EmailsQueue)
            {
                MailMessage message = new MailMessage();
                message.From = new MailAddress(email.WC_EmailToolTemplates.SenderEmail);
                message.To.Add(new MailAddress(email.Email));
                message.Body = body;
                message.IsBodyHtml = true;
                message.Subject = subject;

                using (SmtpClient smtp = new SmtpClient
                {
                    Host = email.WC_EmailToolTemplates.Host, 
                    Port = email.WC_EmailToolTemplates.Port,  
                    Credentials = new NetworkCredential(email.WC_EmailToolTemplates.SMTPUser, email.WC_EmailToolTemplates.SMTPPass), 
                    EnableSsl = email.WC_EmailToolTemplates.EnableSSL 
                })
                   try
                {
                    smtp.Send(message);
                }
                catch (Exception ex) {

                }
                }

Regards,
Asif Hameed

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:04:42+00:00Added an answer on June 6, 2026 at 7:04 pm

    You may use a try catch block. If the address is not valid format, the Add function will throw an error. If needed you may log it also

       try
        {
             message.To.Add(new MailAddress(email.Email));
        }
        catch (FormatException ex)
        {
            //If you want you may log it here
        }
    

    EDIT : As per the OP’s question edit.

    the below code will skip sending email to those items having a problem (bad address etc..) But executes the remaining items in loop

    foreach (var email in EmailsQueue)
    {
        MailMessage message = new MailMessage();
        try
        {
            message.From = new MailAddress(email.WC_EmailToolTemplates.SenderEmail);
            message.To.Add(new MailAddress(email.Email));            
            message.Body = body;
            message.IsBodyHtml = true;
            message.Subject = subject;
    
            using (SmtpClient smtp = new SmtpClient
            {
                Host = email.WC_EmailToolTemplates.Host,
                Port = email.WC_EmailToolTemplates.Port,
                Credentials = new NetworkCredential(email.WC_EmailToolTemplates.SMTPUser, email.WC_EmailToolTemplates.SMTPPass),
                EnableSsl = email.WC_EmailToolTemplates.EnableSSL
            })
            smtp.Send(message);
        }
        catch (Exception ex)
        {
            //error in sending email to one item in collection
            //Log it ig you want .Foreach will continue with remaining items    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a ASP.NET application. I want to prevent the user from
I am working on a ASP.NET application that has a class that inherits a
I'm working on an ASP.NET application containing WCF services that is meant to act
I am currently working on an asp.net application that has sql server 2008 as
I am working on an ASP.NET application that uses the AspNetSqlMembershipProvider for the authentication
I'm working on an ASP.NET application that uses VB. I'm using a SQLReader/SQLCommand/SQLConnection within
I am working on ASP.net application with a MS Access database.I want to only
I'm currently working on a web application in ASP.NET that displays reports (created with
I am working on asp.net application and I have created a query. I want
We are working on an ASP.NET application at work that uses the just-in-time collection

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.