Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i work in c# and asp.net
i want to sent mail to some people
and i use class and method like below code:

C#
using System.Net.Mail;

public void sendmail(string Subject, string ToEmail, string Body)
{
     SmtpClient MyMail = new SmtpClient();
     MailMessage MyMsg = new MailMessage();
     MyMail.Host = "webmail.tahasoft.net";

     MyMsg.To = ToEmail;

     MyMsg.Subject = Subject;
     MyMsg.SubjectEncoding = Encoding.UTF8;
     MyMsg.IsBodyHtml = true;
     MyMsg.From = new MailAddress("info@mysite.net", "mysite");
     MyMsg.BodyEncoding = Encoding.UTF8;
     MyMsg.Body = Body;
     MyMail.UseDefaultCredentials = false;
     NetworkCredential MyCredentials = new NetworkCredential("info@mysite.net", "mysite");
     MyMail.Credentials = MyCredentials;
     MyMail.Send(MyMsg);
}


when i sent one email to one person it works fine
but when i sent ToEmail like to:
aaa@yahoo.com;sss@yahoo.com;ddd@yahoo.com

i get error:
error cannot implicitly convert type 'string' to System.Net.Mail.MailAddressCollection
Posted
Updated 13-Feb-13 0:41am
v4

You need to call the Add method for each address in your list, and you need to convert them to MailAddress objects:
C#
mail.To.Add(new MailAddress(to));

In your case, you probably want to split the names into separate strings, and add them:
C#
string[] tos = ToEmail.Split(';');
foreach (string to in tos)
   {
   mail.To.Add(new MailAddress(to));
   }
 
Share this answer
 
Comments
jiji2663 14-Feb-13 3:42am    
thanks
it's work
OriginalGriff 14-Feb-13 3:51am    
You're welcome!
Split the string on the char ';'
C#
string[] Addresses = s.Split(';');

and then:
C#
MailMessage msg = new MailMessage();
foreach (string address in Addresses)
   msg.To.Add(address);


Cheers,
Edo
 
Share this answer
 
User following code to send email to multiple recipients,
C#
MyMsg.To.Add(new System.Net.Mail.MailAddress(ToEmail));
 
Share this answer
 
Comments
jiji2663 14-Feb-13 3:30am    
thnx for your help
i try your solutions
and see this error:
An invalid character was found in the mail header: ';'
Mukund Thakker 14-Feb-13 23:41pm    
You can pass multiple email by comma separated string.
I think you need to create a MailAddressCollection and add the email addresses to that collection with new MailAddress("emailadres@domain.extension", "mysite"); Then you assign the collection to the To property of the MailMessage object. It could be you need to split the string on the ";" character and add them to the collection with a for loop.

Basically do the same as this line:
MyMsg.From = new MailAddress("info@mysite.net", "mysite");
but then with the MailAddressCollection.

Hope this helps.
 
Share this answer
 
This code below will work fine for you:

public static void SendBulkMail(Shooter sender, List<string> receiversList, string mailtext)
        {
            MailMessage email = new MailMessage();
            email.From = new MailAddress(sender.Mailaddress, string.Format("{0}, {1}", sender.Lastname, sender.Surname));
            foreach (string receiver in receiversList)
            {
                email.Bcc.Add(new MailAddress(receiver));
            }
            email.Subject = string.Format("SMGT BulkMail von {0} {1}", sender.Lastname, sender.Surname);
            email.IsBodyHtml = false;
            email.Body = string.Format("{0}\n\n\n--> Diese Email-Nachricht wurde als BulkMail vom SMGT Web-Portal gesendet. Absender ist {1} {2} ({3} - {4})\n\nDa es sich um ein BulkMail handelt sind alle Empfänger im BCC-Feld aufgeführt",
                                        mailtext, sender.Lastname, sender.Surname, sender.HomeShootingClub.Name, sender.HomeShootingClub.City);
            SmtpClient client = ConnectSmtpClient();
            client.Send(email);
        }

        private static SmtpClient ConnectSmtpClient()
        {
            SmtpClient client = new SmtpClient();
            client.Port = 587;
            client.EnableSsl = true;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential("someone@gmail.com", "password");
            client.Host = "smtp.gmail.com";
            return client;
        }



Shooter is a class to represent a person.

cheers,
Marco Bertschi
 
Share this answer
 
 
Share this answer
 
Try below...

C#
using System.Net.Mail;
using System.Text;
using System.Net;

C#
SmtpClient MyMail = new SmtpClient();
MailMessage MyMsg = new MailMessage();
MyMail.Host = "webmail.tahasoft.net";
MyMsg.To.Add(ToEmail);
MyMsg.Subject = Subject;
MyMsg.SubjectEncoding = Encoding.UTF8;
MyMsg.IsBodyHtml = true;
MyMsg.From = new MailAddress("info@mysite.net");
MyMsg.BodyEncoding = Encoding.UTF8;
MyMsg.Body = Body;
MyMail.UseDefaultCredentials = false;
NetworkCredential MyCredentials = new NetworkCredential("info@mysite.net", "mysite");
MyMail.Credentials = MyCredentials;
MyMail.Send(MyMsg);


first try to send mail to one email id, if it successful then send to multiple email id's..
......
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900