0

System.Net.Mail 経由で複数の受信者 (複数の受信者を持つ 1 つの電子メール) に電子メールを送信しているときに、1 つの受信者のアドレスが失敗した場合、残りは電子メールに届きますか?

同じ質問の重複があるかもしれませんが、対照的に別の質問をしています。

そのような質問を促すために、1 つのアドレスが失敗してもそのようなことが起こらない Outlook のような利用可能な電子メール クライアントを考慮しました。おそらく、System.Net.Mail は軽量のクライアントであり、そのような機能を許可していないのでしょうか? それとも、コードの書き方が間違っているのでしょうか。

どうしてこうなったのかと聞かれましたが、説明を受けて通り過ぎることができればいいのにと思います。

以下のコードからそのようなシナリオを実行していました。

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("jkstock@keells.com", "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;
            }
        }

上記のコードの参照:

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();
        }
    }
4

0 に答える 0