Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET mailing
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:
 
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 13-Feb-13 0:25am
jiji26631.7K
Edited 13-Feb-13 0:41am
joe_j3.9K
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You need to call the Add method for each address in your list, and you need to convert them to MailAddress objects:
mail.To.Add(new MailAddress(to));
In your case, you probably want to split the names into separate strings, and add them:
string[] tos = ToEmail.Split(';');
foreach (string to in tos)
   {
   mail.To.Add(new MailAddress(to));
   }
  Permalink  
Comments
jiji2663 at 14-Feb-13 3:42am
   
thanks
it's work
OriginalGriff at 14-Feb-13 3:51am
   
You're welcome!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Split the string on the char ';'
string[] Addresses = s.Split(';');
and then:
MailMessage msg = new MailMessage();
foreach (string address in Addresses)
   msg.To.Add(address);
 
Cheers,
Edo
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

User following code to send email to multiple recipients,
MyMsg.To.Add(new System.Net.Mail.MailAddress(ToEmail));
  Permalink  
Comments
jiji2663 at 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 at 14-Feb-13 23:41pm
   
You can pass multiple email by comma separated string.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

Try below...
 
using System.Net.Mail;
using System.Text;
using System.Net;
  
        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..
......
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 383
1 ChauhanAjay 186
2 Richard Deeming 180
3 Sergey Alexandrovich Kryukov 171
4 Yogesh Kumar Tyagi 165
0 Sergey Alexandrovich Kryukov 6,252
1 OriginalGriff 5,498
2 CPallini 2,473
3 Richard MacCutchan 1,627
4 Abhinav S 1,530


Advertise | Privacy | Mobile
Web04 | 2.8.140821.2 | Last Updated 13 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100