I’m using the System.Net.Mail library
and am attempting to initialize a MailMessage object. I must initalize it with an empty object because I iterate through a few email addresses to add them into the MailMessage To
field.
I’ve tried to initialize the MailMessage object with an email addresses, but that also throws the Object reference not set...
error.
The error occurs on the initialization of MailMessage msg = new MailMessage();
SmtpClient smtp = new SmtpClient();
smtp.Host = "host-address";
smtp.Port = 25;
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
MailMessage msg = new MailMessage();
string[] TOaddresses = emailModel.To.Split(';');
string[] BCCaddresses = emailModel.Bcc.Split(';');
string[] CCaddresses = emailModel.Cc.Split(';');
msg.From = new MailAddress(emailModel.From);
foreach (var emailAddress in TOaddresses)
msg.To.Add(emailAddress);
foreach (var emailAddress in BCCaddresses)
msg.Bcc.Add(emailAddress);
foreach (var emailAddress in CCaddresses)
msg.CC.Add(emailAddress);
msg.Subject = "TEST: " + emailModel.Subject;
msg.Body = emailModel.Body;
msg.IsBodyHtml = true;
smtp.Send(msg);