DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Mojtaba has posted 2 posts at DZone. View Full User Profile

Send email in asp.net

03.22.2013
| 1732 views |
  • submit to reddit
hi friends.
In this section of the code that I'm going to tell you that you can email.
The first step is to create a new web application. Following method to the page, write the subject, body, desire, send, sending you specify.

 public DataTable All_Topik()    {        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ASPNETDBConnectionString1"].ConnectionString);        SqlCommand cmd = new SqlCommand();        cmd.Connection = con;        cmd.CommandText = "SELECT top(10) Message,Subject from B_Topics order by TopicID desc";        con.Open();        SqlDataAdapter da = new SqlDataAdapter(cmd);        DataTable dt = new DataTable();        da.Fill(dt);        con.Close();        return dt;    }
 public DataTable All_Email()    {        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ASPNETDBConnectionString1"].ConnectionString);        SqlCommand cmd = new SqlCommand();        cmd.Connection = con;        cmd.CommandText = "SELECT email from [email]";        con.Open();        SqlDataAdapter da = new SqlDataAdapter(cmd);        DataTable dt = new DataTable();        da.Fill(dt);        con.Close();        return dt;    }
 public static void MessageScript(string function, Page thispage)    {        thispage.ClientScript.RegisterStartupScript(thispage.GetType(), "msg", function, true);    }
  protected void Btnsend_Click(object sender, EventArgs e)    {        try        {            DataTable dt = All_Topik();            DataTable dt2 = All_Email();            int ct = All_Topik().Rows.Count;            int ce = All_Email().Rows.Count;            for (int i = 0; i < ct; i++)            {                string msg = dt.Rows[i]["Message"].ToString();                string sub = dt.Rows[i]["Subject"].ToString();                MailMessage mail = new MailMessage("nava.mojtaba@yahoo.com", dt2.Rows[0]["email"].ToString());                mail.Body = msg;                mail.Subject = sub;                mail.IsBodyHtml = true;                mail.Priority = MailPriority.High;                mail.BodyEncoding = Encoding.UTF8;                mail.SubjectEncoding = Encoding.UTF8;                mail.HeadersEncoding = Encoding.UTF8;                mail.Headers.Add("Company", "FAKHRAVARY.SOMEE");                mail.Headers.Add("Location", "Iran/Bousher");                mail.Headers.Add("Tell", "09173700916");                for (int b = 1; b < ce; b++)                {                    mail.Bcc.Add(dt2.Rows[b]["email"].ToString());                }                SmtpClient c = new SmtpClient("mail.zarcheckan.com", 25);                c.Send(mail);            }        }        catch        {            MessageScript("ERROR", this);        }    }HTML page(Default.aspx):<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title></head><body>    <form id="form1" runat="server">    <asp:ScriptManager ID="ScriptManager1" runat="server">    </asp:ScriptManager>    <asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1">        <ProgressTemplate>      please wait....        </ProgressTemplate>    </asp:UpdateProgress>    <asp:UpdatePanel ID="UpdatePanel1" runat="server">        <ContentTemplate>            <asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="Send" />        </ContentTemplate>    </asp:UpdatePanel>    </form></body></html>The last step in the Web server configuration is set to work. As follows:
 <system.net>      <mailSettings>        <smtp from="fakhr@a-velayat.com">          <network host="mail.a-velayat.com" password="fakhravary" userName="fakhr@a-velayat.com" />        </smtp>      </mailSettings>    </system.net>

    Tags: