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
| 1484 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.
view sourceprint?01.public DataTable All_Topik()02.   {03.       SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ASPNETDBConnectionString1"].ConnectionString);04.       SqlCommand cmd = new SqlCommand();05.       cmd.Connection = con;06.       cmd.CommandText = "SELECT top(10) Message,Subject from B_Topics order by TopicID desc";07.       con.Open();08.       SqlDataAdapter da = new SqlDataAdapter(cmd);09.       DataTable dt = new DataTable();10.       da.Fill(dt);11.       con.Close();12.       return dt;13.   }14.public DataTable All_Email()15.   {16.       SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ASPNETDBConnectionString1"].ConnectionString);17.       SqlCommand cmd = new SqlCommand();18.       cmd.Connection = con;19.       cmd.CommandText = "SELECT email from [email]";20.       con.Open();21.       SqlDataAdapter da = new SqlDataAdapter(cmd);22.       DataTable dt = new DataTable();23.       da.Fill(dt);24.       con.Close();25.       return dt;26.   }27.public static void MessageScript(string function, Page thispage)28.   {29.       thispage.ClientScript.RegisterStartupScript(thispage.GetType(), "msg", function, true);30.   }31. protected void Btnsend_Click(object sender, EventArgs e)32.   {33.       try34.       {35.           DataTable dt = All_Topik();36.           DataTable dt2 = All_Email();37.           int ct = All_Topik().Rows.Count;38.           int ce = All_Email().Rows.Count;39.           for (int i = 0; i < ct; i++)40.           {41.               string msg = dt.Rows[i]["Message"].ToString();42.               string sub = dt.Rows[i]["Subject"].ToString();43.               MailMessage mail = new MailMessage("nava.mojtaba@yahoo.com", dt2.Rows[0]["email"].ToString());44.               mail.Body = msg;45.               mail.Subject = sub;46.               mail.IsBodyHtml = true;47.               mail.Priority = MailPriority.High;48.               mail.BodyEncoding = Encoding.UTF8;49.               mail.SubjectEncoding = Encoding.UTF8;50.               mail.HeadersEncoding = Encoding.UTF8;51.               mail.Headers.Add("Company", "FAKHRAVARY.SOMEE");52.               mail.Headers.Add("Location", "Iran/Bousher");53.               mail.Headers.Add("Tell", "09173700916");54.               for (int b = 1; b < ce; b++)55.               {56.                   mail.Bcc.Add(dt2.Rows[b]["email"].ToString());57.               }58.               SmtpClient c = new SmtpClient("mail.zarcheckan.com", 25);59.               c.Send(mail);60.           }61.       }62.       catch63.       {64.           MessageScript("ERROR", this);65.       }66.   } 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="support@zarcheckan.com">          <network host="mail.a-velayat.com" password="nava" userName="nava.mojtaba@yahoo.com" />        </smtp>      </mailSettings>    </system.net>


thanks.

Good luck.

    Tags: