every thing if working fine when i am sending mail to single address..but i m not been able to find out a way to send mail 2 multiple addresses....plz help me.........

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SendMail("smtp.gmail.com",
               465,
               "contactbook2010@gmail.com",
               "xxxxxxx",
               "Deeptaksh Dargan",
               "deeptakshd@gmail.com",
               "Satish Kumar",
               "satish.k.dargan@gmail.com",
               "Test",
               "Hw r u!",
               true); 
    }
    public static void SendMail(string sHost, int nPort, string sUserName, string sPassword, string sFromName, string sFromEmail,
       string sToName, string sToEmail, string sHeader, string sMessage, bool fSSL)
    {
        if (sToName.Length == 0)
            sToName = sToEmail;
        if (sFromName.Length == 0)
            sFromName = sFromEmail;

        System.Web.Mail.MailMessage Mail = new System.Web.Mail.MailMessage();
        Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = sHost;
        Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;

        Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = nPort.ToString();
        if (fSSL)
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"] = "true";

        if (sUserName.Length == 0)
        {
            //Ingen auth 
        }
        else
        {
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = sUserName;
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = sPassword;
        }

        Mail.To = sToEmail;
        Mail.From = sFromEmail;
        Mail.Subject = sHeader;
        Mail.Body = sMessage;
        Mail.BodyFormat = System.Web.Mail.MailFormat.Html;

        System.Web.Mail.SmtpMail.SmtpServer = sHost;
        System.Web.Mail.SmtpMail.Send(Mail);
    } 
 
}

Recommended Answers

All 2 Replies

Isn't the MailMessage.To property a MailAddressCollection though? can't you use Mail.To.Add(sToEmail)?

Which .Net version are you using, I get a compiler error just from trying to assign anything to To.

this code is working properly............i m using .net 2.0
the error might be occuring due to d the port no issue.......port no 465 work 4 gmail,n generally 25 is used 4 yahoo

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.