Saturday, August 20, 2011

Sending mail to multiple recipient


For sending mail to multiple recipients you can use BCC to send Blind Carbon Copy to each recipient. When you specify recipient in Bcc they will receive message, but the names will not be visible to other recipients.
But instead of using BCC, you should send a separate copy of your message to each recipient that you want to receive blind copy. The problem with BCC is that spam filters frequently block messages that do not have recipient’s email address in the From header. Therefore, if you use BCC, the message will very likely be filtered.
In my previous post I have shown how to send email. Here I would like to rewrite BuildMail() method for sending mail to multiple recipient at a time:
    private MailMessage BuildMail()
    {
        string from, to, bcc, cc, subject, body;
        from = "uniquesaiful@gmail.com";   //Email Address of Sender
        to = "tips.asp.net@gmail.com,saifulondotnet@gmail.com,uniquesaiful@gmail.com";   //Email Address of Receiver
        bcc = "";
        cc = "";
        subject = "This is a test email. I am just checking whether email client is working properly.";
 
        StringBuilder sb = new StringBuilder();
        sb.Append("Hi Scott,<br/>");
        sb.Append("This is a test email. We are testing out email client. Please don't mind.<br/>");
        sb.Append("We are sorry for this unexpected mail to your mail box.<br/>");
        sb.Append("<br/>");
        sb.Append("Thanking you<br/>");
        sb.Append("Tips.Asp.Net");
        
        body =sb.ToString() ;
 
        MailMessage mail = new MailMessage();
        mail.From = new MailAddress(from);
        if (to.Contains(","))
        {
            string[] tos = to.Split(',');
            for (int i = 0; i < tos.Length; i++)
            {
                mail.To.Add(new MailAddress(tos[i]));
            }
        }
        else
        {
            mail.To.Add(new MailAddress(to));
        }
 
        if (!string.IsNullOrEmpty(bcc))
        {
            if (bcc.Contains(","))
            {
                string[] bccs = bcc.Split(',');
                for (int i = 0; i < bccs.Length; i++)
                {
                    mail.Bcc.Add(new MailAddress(bccs[i]));
                }
            }
            else
            {
                mail.Bcc.Add(new MailAddress(bcc));
            }
        }
        if (!string.IsNullOrEmpty(cc))
        {
            if (cc.Contains(","))
            {
                string[] ccs = cc.Split(',');
                for (int i = 0; i < ccs.Length; i++)
                {
                    mail.CC.Add(new MailAddress(ccs[i]));
                }
            }
            else
            {
                mail.CC.Add(new MailAddress(bcc));
            }
            mail.CC.Add(new MailAddress(cc));
        }
 
        mail.Subject = subject;
        mail.Body = body;
        mail.IsBodyHtml = true;
        mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
 
        return mail;
    }

35 comments:

  1. thanks for ur codings dude it works fine gud ...

    ReplyDelete
  2. i need to send mails to at least 40 people.How can send it without typing each and every mail addresses of the reccepients

    ReplyDelete
  3. MailMessage mail = new MailMessage();
    mail.To.Add(TextBox6.Text);//Email ID where email is to be send
    mail.To.Add(TextBox10.Text);
    mail.From = new MailAddress(TextBox3.Text);//YourGmailID@gmail.com


    mail.Subject = TextBox7.Text;

    string Body = TextBox8.Text;
    mail.Body = Body;

    mail.IsBodyHtml = true;
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
    smtp.Credentials = new System.Net.NetworkCredential
    (TextBox3.Text, TextBox9.Text);//YourUserName@gmail.com,YourGmailPassword
    //Or your Smtp Email ID and Password
    smtp.EnableSsl = true;
    smtp.Send(mail);
    mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;





    This is my code....how can i ad multiple recepients without typing each adrresses....


    ReplyDelete
  4. thank you........ its really work

    ReplyDelete