I want to write a little generalized function to send email;

Can any one give me good general example.  To include, how to send email to more than one person

public static void sendmail(string strFrom, string strTo, string strSubject, string strBody, string toMailAddress, string txtName)
            MailMessage EmailMsg = new MailMessage();
            EmailMsg.From = new MailAddress(strFrom);
            //    EmailMsg.From = new MailAddress("CustomerService@Aceyourplace.com", "CustomerService@Aceyourplace.com");
            EmailMsg.To.Add(new MailAddress(toMailAddress, txtName));
            EmailMsg.Subject = strSubject;
            EmailMsg.Body = strBody;
            EmailMsg.IsBodyHtml = true;
            // EmailMsg.Priority = MailPriority.Normal;

            SmtpClient MailClient = new SmtpClient();

            // HttpContext.Current.Response.Redirect("mailsent.aspx");

Who is Participating?
Jitendra PatilConnect With a Mentor Sr.Software EngineerCommented:
Hi there are two ways you can go with.
1st way
MailMessage msg = new MailMessage();
msg.Body = ....;

SmtpClient smtp = new SmtpClient();
2nd Way using database
using System.Collections; //ArrayList
using System.Data.SqlClient; //SQL
using System.Net.Mail; //email

private void btnSend_Email_Click(object sender, EventArgs e)
            ArrayList list_emails = new ArrayList();
            int i = 0, email = 0;
            sqlConnection1.Open(); //connection to the database.
            SqlCommand cmd_Email = new SqlCommand("Select Email from Email_Table", sqlConnection1);
            SqlDataReader read_Email = cmd_Email.ExecuteReader();
            while (read_Email.Read())
                email = read_Email.GetValue(i).ToString();
                list_emails.Add(email); //Add email to a arraylist
                i = i + 1 - 1; //increment or ++i
            sqlConnection1.Close(); //Close connection
            foreach (string email_to in list_emails)
                MailMessage mail = new MailMessage();
                mail.Subject = "Welcome to C#";
                mail.From = new MailAddress(hold_Account);
                mail.Body = "Test";
                SmtpClient smtp = new SmtpClient("SMTP Server");


Open in new window

hope this helps;
the 'to' is simply a string so if you build this string with the email addresses separated by semi-colons then you can pass this string to your function.
goodkAuthor Commented:
string strTo="first name  last name" <asdfasdfasdf@yahoo.com>; "nameLname" <jjjllkjljljlkjlj@gmail.com>;

If the above variable is received in my generalized function how would I send this email to the above people?

 EmailMsg.To.Add(new MailAddress(???????));
goodkAuthor Commented:
not sure if i found the answer i was looking for
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.