문제

I am using System.Net.Mail.MailMessage to capture and pass it over the info related to email.

I am getting "Value Cannot be null" exception.

My requirement is anyone of the To,Cc,Bcc can be empty but at least one of those fields should have an email (either To or Cc or Bcc).

Here is my code :

public object InsertMailSendQueue([FromBody] EmailReport mailMsg ) 
        {            

            mailMessage.To.Add(mailMsg.To);
            mailMessage.CC.Add(mailMsg.Cc);
            mailMessage.Bcc.Add(mailMsg.Bcc);

            mailMessage.From = new System.Net.Mail.MailAddress(mailMsg.From);
            mailMessage.Subject = mailMsg.Subject;
            mailMessage.Body = mailMsg.Body;

            var confirmation = EmailService.InsertMailSendQueue(mailMsg.AgentId, mailMsg.Notes, null, mailMessage, true);
            return confirmation;

        }    
도움이 되었습니까?

해결책

As Hans inferred in his comment, something like

bool send = false;

if (!String.IsNullOrEmpty(mailMsg.To))
{
     mailMessage.CC.Add(mailMsg.To);
     send = true;
}
if (!String.IsNullOrEmpty(mailMsg.Cc))
{
     mailMessage.CC.Add(mailMsg.Cc);
     send = true;
}
if (!String.IsNullOrEmpty(mailMsg.Bcc))
{
     mailMessage.CC.Add(mailMsg.Bcc);
     send = true;
}
if (!send)
{
     // what to do if none are set...
}

etc. should do the job.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top