Pregunta

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;

        }    
¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top