I'm trying to send emails using gmail's username and password in a Windows application. However, the following code is sending the mail to only the first email address when I collect multiple email address in my StringBuilder instance.
var fromAddress = new MailAddress(username, DefaultSender);
var toAddress = new MailAddress(builder.ToString());//builder reference having multiple email address
string subject = txtSubject.Text;
string body = txtBody.Text; ;
var smtp = new SmtpClient
{
Host = HostName,
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(username, password),
//Timeout = 1000000
};
var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body,
IsBodyHtml = chkHtmlBody.Checked
};
if (System.IO.File.Exists(txtAttechments.Text))
{
System.Net.Mail.Attachment attechment = new Attachment(txtAttechments.Text);
message.Attachments.Add(attechment);
}
if(this.Enabled)
this.Enabled = false;
smtp.Send(message);
What am I doing wrong, and how can I sort out my problem?
In the preceding class file I used a foreach loop to add the multiple recipient's Email Ids but before that we are using the comma (,) separated input email ids from user in one string "Tomail" then we are splitting the input sting with (,) commas and added to one string array named "Multi" then using a foreach loop we ...
In short, to send to multiple recipients you should set the header to be a string of comma delimited email addresses. The sendmail() parameter to_addrs however should be a list of email addresses.
Best bet is to message.To.Add()
each of your MailAddress
es individually. I think early versions of .Net were happier to parse apart comma or semicolon separated email addresses than the more recent runtime versions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With