I'm building an application with asp.net. And I need to send emails to selected customers.

What is the best way to send email ? Is it by using System.Net Class or to send it by using MS Sql Server ?

Thank you

5 Years
Discussion Span
Last Post by aryanverma

Mr. Paul,

To send email from the .Net page is very easy.
1.You put design page where you get user id to send newsletter and other details. From here you get every user details of email ID.
2. If you send the email from gmail SMTP them write smtp.gmail.com. then enter user gmail username and password also.
3. Make a method to fetch all emails from SQL-DataBase.

public String fetchmail()
        SqlDataAdapter adp = new SqlDataAdapter("select email from tbmail where status=1", con);
        DataSet ds = new DataSet();
        for (Int32 i = 0; i < ds.Tables[0].Rows.Count; i++)
            d += ds.Tables[0].Rows[i][0].ToString() + ',';
        Int32 j = Convert.ToInt32(d.Length);
        d = d.Substring(0, j - 1);
        return d;

here d is declare for all emails and J-1 is putting "," after every email. Status 1 is showing the emails in active.
4.Make a method for news Description/Other Details.

public String newsdesc()
        SqlDataAdapter adp = new SqlDataAdapter("SELECT TOP 1 discription,title from skuploadnews ORDER BY id DESC", con);
        DataSet ds = new DataSet();
        for (Int32 i = 0; i < ds.Tables[0].Rows.Count; i++)
            a += ds.Tables[0].Rows[0][i].ToString();
             b= ds.Tables[0].Rows[0][i].ToString();
        return a;
        return b;

Here a and b is returning Newsletter and other filed details you can send top 5 or multiple details with single query.

5. Now make a method for email.
But first declare a global string variable in page top.

public partial class adminnewsletter : System.Web.UI.Page
    String d;
    String str;
    String a,b;
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)


Now make a method.

public void sendmail(string str)
        #region Create HTML Table Structure For mail
        str = "<table width='75%' border='0' align='center' cellpadding='0' cellspacing='0' style='border-color:#8d1805; border:solid;'>";
        //str += "<tr><td  bgcolor='#8d1805'><font color='#FFFFFF'><strong> WRITE HEDDER OF EMAIL HERE</strong></font></td></tr>";
        str += "<tr><td height='32px' valign='center' align='center' bgcolor='#0977A6'><font color='#FFFFFF'><strong>  WRITE HEDDER OF EMAIL HERE </strong></font></td></tr>";
        //str += "<tr><td  align='center' bgcolor='#0977A6'><font color='#FFFFFF'>&nbsp;<strong></strong></font></td></tr>";
        //Inner Table Structure//Inner Table Structure
        str += " <tr><td height='10px'></td></tr>";
        str += "<tr><td><table width='100%'><tr><td>&nbsp;</td><td><table width='99%'  align='center'><tr><td width='10%'>&nbsp;</td><td width='73%' ></td></tr>";
        str += "<tr><td ><strong>Title ::</strong></td><td style=width: 100px><strong>" + b + "</strong></td></tr><br><br>";
        str += "<tr><td colspan='2'><strong>--NEWSLETTER--</strong></td></tr>";
        //str += "<tr><td style=width: 100px><b>User E-mail ID::</b></td><td style=width: 100px>" + d + "</td><td style=width: 100px></td></tr><br>";
        str += "<tr><td colspan='2'>" + a + "</td></tr><br>";

        //str += "<tr><td style=width: 100px><b>User Name::</b></td><td style=width: 100px>" + TextBox2.Text + "</td><td style=width: 100px></td></tr><br>";
        //str += "<tr><td style=width: 100px><b>Password::</b></td><td style=width: 100px>" + Label1.Text + "</td><td style=width: 100px></td></tr><br><br>";
        //str += "<tr><td style=width: 100px></td><td style=width: 100px>''</td><td style=width: 100px></td></tr>";
        //str += "<tr><td style=width: 100px><b>Verify the Mail::</b></td><a href=" + url + ">" + key + "</a> <td style=width: 100px></td></tr><br><br><br><br>";
        str += "</table>";

        //str += "  <tr><td align='center' class='services'bgcolor='#0977A6'>&nbsp;</td></tr>";
        //Closing Inner Table
        str += "</table>";
        //Closing Outer Table
        str += "<tr><td align='center'  bgcolor='#0977A6'>&nbsp;</td></tr>";
        str += "<tr><td align='center'  bgcolor='#0977A6'>&nbsp;</td></tr>";
        str += "<tr><td class=inner-left-bottom></td><td class=inner-bottom-border></td><td class=inner-right-bottom></td></tr></table>";
        //Outer Table Structure
        // string attach1 = null;
        System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
        msg.From = new System.Net.Mail.MailAddress("youremail.com");
        if (d.Trim().Length != 0) msg.To.Add(d);
        msg.Subject = "write subject here";
        msg.Body = str;
        msg.Priority = System.Net.Mail.MailPriority.Normal;
        msg.IsBodyHtml = true;
        System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
        //smtp.Host = System.Configuration.ConfigurationManager.AppSettings["write smtp host here.com"];
        smtp.Port = 25;
        smtp.Credentials = new System.Net.NetworkCredential("youremail.com", "write password here");
        smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

Here i write where you fill email of Admin or your and password, SMTP also.
Code of email is small which is after #endregion. And code between #region and #endregion is design of email in table you can give colour also here if you know some HTML.

6. Last point, now you write all methods in a button click.As per your requirement.

protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        Page.ClientScript.RegisterStartupScript(this.GetType(), "ch", "<script>alert('Email has been send !!!! ')</script>");

Here is all the code for you which i write and describe and create today for you.
If you not able to understand then ask me any time.

Edited by __avd: Added [code] tags.


Thank you very much. I went through many blogs to send mail. But this is only post that gave me clear solution.


can u pls tell me what should we use on asp web form to send an email e.g. ( button , textbox etc)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.