Sending Multiple Recepients in a single mail: Consequences

173 Views Asked by At

While sending email via System.Net.Mail to multiple recipients (one single email with multiple recipients), if one recipients address fail, will the rest reach the email?

There might be duplicates of the same question, but I'm asking something different in contrast.

For prompting such question, I was taken into consideration of Email clients available like Outlook where such does not happen even if one single address failed. Perhaps, System.Net.Mail is a lightweight client and does not allow such functionality? Or is it the way I'm writing my code incorrectly.

I was asked as if why this happens, hope I can get an explanation so that I can pass it by.

I was running such a scenario from my code below:

public void sendBusinessEmail(communication.email.business.config.BusinessEmailList[] aEmailList)
        {
            System.Net.Mail.MailMessage objMsg = null;
            string[] aEmail = null;
            System.Net.Mail.SmtpClient objSmtpClient = null;
            int iRetries = 0;

            if (aEmailList == null)
            {
                return;    
            }

            try
            {
                if (Properties.Settings.Default.IS_SMTP_TLS)
                {
                    objSmtpClient = getSMTP_TLS_Client();
                    ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
                }
                else
                {
                    objSmtpClient = getSMTP_Default_Client();
                }

                foreach (communication.email.business.config.BusinessEmailList obj in aEmailList)
                {
                    try
                    {
                        objMsg = new System.Net.Mail.MailMessage();
                        objMsg.From = new System.Net.Mail.MailAddress("[email protected]", "JKSB Business Services");
                        aEmail = obj.Emails;

                        if (Properties.Settings.Default.TO_TEST_EMAIL)
                        {
                            objMsg.To.Add(Properties.Settings.Default.TO_TEST_EMAIL_ADDRESS.ToString());

                            for (int i = 0; i < aEmail.Length; i++)
                            {
                                objMsg.Body += aEmail[i] + Environment.NewLine;
                            }
                        }
                        else
                        {
                            for (int i = 0; i < aEmail.Length; i++)
                            {
                                objMsg.To.Add(new System.Net.Mail.MailAddress(aEmail[i]));
                            }
                            objMsg.Body = obj.EmailBody;
                        }

                        objMsg.Subject = checkAllReadySent(obj.EmailSubject);
                        objMsg.Attachments.Add(new System.Net.Mail.Attachment(obj.AttachmentPath, (System.IO.Path.GetExtension(obj.AttachmentPath) == ".pdf" ? System.Net.Mime.MediaTypeNames.Application.Pdf : System.Net.Mime.MediaTypeNames.Application.Octet)));

                        //sending emails
                        //send for 5 times if error persists, unless otherwise success
                        for (int i = 0; i < 5; i++)
                        {
                            try
                            {
                                objSmtpClient.Send(objMsg);
                                obj.updateLog("OK", (i+1));
                                break;
                            }
                            catch (Exception e)
                            {
                                if (i == 4)
                                {
                                    obj.updateLog(e.Message, (i+1));    
                                }
                            }
                        }

                        objMsg = null;
                    }
                    catch (System.Net.Mail.SmtpFailedRecipientsException e0)
                    {
                        obj.updateLog("Invalid Recipient(s)",0);
                    }
                    catch (Exception e1)
                    {
                        obj.updateLog("Error",0);
                    }
                }
                objSmtpClient = null;

            }
            catch (Exception e2)
            {
                MessageBox.Show(e2.Message);
                objMsg = null;
                objSmtpClient = null;
            }
        }

References for the above code:

public abstract class BusinessEmailList
    {
        private string _accountid = string.Empty;
        private string _attachmentPath = string.Empty;
        private string _emailsubject = string.Empty;
        private string _agentid = string.Empty;
        private string _response = string.Empty;
        private string _ccTo = string.Empty;
        private string _bccTo = string.Empty;
        protected string _additionalBody = string.Empty;

        private string[] _aEmail = null;

        public string AccountID
        {
            get { return _accountid; }
            set { _accountid = value; }
        }

        public string AgentID
        {
            get { return _agentid; }
            set { _agentid = value; }
        }

        public string Response
        {
            get { return _response; }
            set { _response = value; }
        }

        public string AttachmentPath
        {
            get { return _attachmentPath; }
            set
            {
                if (System.IO.File.Exists(value))
                {
                    _attachmentPath = value;
                }
                else { throw (new Exception("Attachment Not Found")); }
            }
        }

        public string EmailSubject
        {
            get { return _emailsubject; }
            set { _emailsubject = value; }
        }

        public string[] Emails
        {
            get { return getEmail(); }
        }

        public string EmailBody
        {
            get { return getMsgBody(); }
            set { _additionalBody = value; }
        }

        public string ccTo
        {
            get { return _ccTo; }
            set { _ccTo = value; }
        }

        public string bccTo
        {
            get { return _bccTo; }
            set { _bccTo = value; }
        }

        public virtual string[] getEmail()
        {
            return null;
        }

        public virtual string getMsgBody()
        {
            if (System.IO.Path.GetExtension(this.AttachmentPath) == ".pdf")
            {
                return "Dear Sir/Madam, " +
                                    Environment.NewLine +
                                    Environment.NewLine +
                                    "With kind Reference to the above, details as per attachment." +
                                    Environment.NewLine +
                                    Environment.NewLine +
                                    "To view the attached PDF files you need Adobe Acrobat Reader installed in your computer. Download Adobe Reader from http://get.adobe.com/reader/ " +
                                    Environment.NewLine +
                                    Environment.NewLine +
                                    "Thank you," +
                                    Environment.NewLine +
                                    "John Keells Stock Brokers (Pvt) Ltd.";
            }
            else 
            {
                return "Dear Sir/Madam, " +
                                    Environment.NewLine +
                                    Environment.NewLine +
                                    "With kind Reference to the above, details as per attachment." +
                                    Environment.NewLine +
                                    Environment.NewLine +
                                    "Thank you," +
                                    Environment.NewLine +
                                    "John Keells Stock Brokers (Pvt) Ltd.";
            }

        }

        public void updateLog(string status, int retries)
        {
            try
            {
                using (OracleConnection oracleConn = new OracleConnection(EquityBroker32.Properties.Settings.Default.JKSB_CONN_ORA.ToString()))
                {
                    OracleCommand cmd = new OracleCommand();
                    cmd.Connection = oracleConn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "JKSBSCHEMA.EMAILLOG_ADD_PROC";

                    string[] aEmail = this.Emails;
                    OracleParameter p = null;

                    foreach (string  s in aEmail)
                    {
                        cmd.Parameters.Clear();

                        p = new OracleParameter("pemail", OracleType.VarChar);
                        p.Value = s;
                        cmd.Parameters.Add(p);

                        p = new OracleParameter("psubject", OracleType.VarChar);
                        p.Value = this.EmailSubject;
                        cmd.Parameters.Add(p);

                        p = new OracleParameter("pattachement", OracleType.VarChar);
                        p.Value = this.AttachmentPath;
                        cmd.Parameters.Add(p);

                        p = new OracleParameter("presponse", OracleType.VarChar);
                        p.Value = status;
                        cmd.Parameters.Add(p);

                        p = new OracleParameter("pseqno", OracleType.Number);
                        p.Direction = ParameterDirection.InputOutput;
                        p.Value = "0";
                        cmd.Parameters.Add(p);

                        p = new OracleParameter("pretries", OracleType.Number);
                        p.Value = retries;
                        cmd.Parameters.Add(p);

                        oracleConn.Open();
                        cmd.ExecuteNonQuery();
                        oracleConn.Close();
                    }
                }
            }
            catch (Exception er)
            {
                this.Response = er.Message;
            }
        }
    }

    public class BusinessClientEmailList : BusinessEmailList
    {
        public override string[] getEmail()
        {
            string[] aEmail;

            //if (Properties.Settings.Default.TO_TEST_EMAIL == false)
            //{
                try
                {
                    using (OracleConnection oracleConn = new OracleConnection(EquityBroker32.Properties.Settings.Default.JKSB_CONN_ORA.ToString()))
                    {
                        string sql = "SELECT EMAIL " +
                                        "FROM " +
                                        "(" +
                                            "SELECT A.EMAIL AS EMAIL " +
                                            "FROM JKSBSCHEMA.AGENTEMAIL A " +
                                            "WHERE A.AGENTID = '" + this.AgentID + "' " +
                                            "AND A.AGENTID != 'JKSB' "+
                                            "AND A.ISACTIVE = 1 " +
                                            "UNION " +
                                            "SELECT B.EMAILID AS EMAIL " +
                                            "FROM JKSBSCHEMA.CLIENTACCOUNTEMAIL B " +
                                            "WHERE B.CLIENTACCOUNTID = '" + this.AccountID + "' " +
                                            "AND B.IS_CONFIRMATION = 1 " +
                                        ") " +
                                        "GROUP BY EMAIL";

                        int i = 0;
                        DataTable tbl = new DataTable();

                        OracleCommand cmd = new OracleCommand(sql, oracleConn);
                        cmd.CommandType = CommandType.Text;
                        OracleDataAdapter da = new OracleDataAdapter(cmd);

                        da.Fill(tbl);
                        aEmail = new string[tbl.Rows.Count];
                        foreach (DataRow rw in tbl.Rows)
                        {
                            aEmail[i] = rw[0].ToString();
                            i++;
                        }
                    }
                }
                catch (Exception)
                {
                    aEmail = null;
                }
            //}
            //else
            //{
            //    aEmail = new string[1];
            //    aEmail[0] = Properties.Settings.Default.TO_TEST_EMAIL_ADDRESS.ToString();
            //}

            return aEmail;
        }

        public override string getMsgBody()
        {
            return base.getMsgBody();
        }
    }
0

There are 0 best solutions below