Monday, 4 July 2011

Newsletter in asp.net


Introduction: Hello everybody, in this article i will explain that how we can create and send “newsletter” in asp.net. This is very helpful article for every .net developer

Implementation: Create a page namaed newletter.aspx. Place a fileuploader named attach_file, label named lbl_msg and a button named send. Below I am giving complete .aspx code and .aspx.cs page’s code.
Code for .aspx page:
<head id="Head1" runat="server">
    <title>Newslatter</title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center" style="margin-top:100px">
         
        <asp:FileUpload ID="attach_file" runat="server" />
         
<asp:label ID="lbl_msg" runat="server" text=""></asp:label>
        <br />
        <br />
        <asp:Button ID="send" runat="server" onclick="send_Click" Text="send" />
        </div>
    </form>
</body>
Code for aspx.cs page:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Net.Mail;

public partial class newsletter : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {

        }
    }
    // here am creating a function named snd_mail()
    public void snd_mail()
    {

        try
        {
            // here m using sbodyas StringBuilder class
            StringBuilder sbody = new StringBuilder();
            // For sending the newsletter in the asp.net we need the complete structure of a newsletter in html.here i am using a  sample structute
            // of a newsletter in html.
            // here i m filling the stringbuilder class sbody with the complete html. You can also send image through the newsletter. To send image there is
            // need for the complte server's address of the image. like see below
         
            sbody.Append("<img src=http://a1.twimg.com/profile_images/1427057726/asp_image.jpg/>");
            sbody.Append("<table  align=center border=2 cellspacing=6 cellpadding=9 width=640 <tr> <td bgcolor=cornsilk> <table bgcolor=gold width=90% align=center><tr><td align=center><img src=http://a1.twimg.com/profile_images/1427057726/asp_image.jpg/></td><tr/><tr><td align=center> ");
            sbody.Append("<b><font size=5 color=brown> Employee Portal</font></br>");
            sbody.Append("<font size=2 color=black>&nbsp; &nbsp; &nbsp; &nbsp;" + DateTime.Now + " &nbsp; &nbsp;  </font>");
            sbody.Append("</b> </td></tr></table> </br>  &nbsp; &nbsp; &nbsp; &nbsp;<br>");
            sbody.Append("<table  bgcolor=gold cellpadding=0 cellspacing=0><tr><td><font size=4 color=brown>Deatil of Your registration for  Glotera Portal</font></td></tr></table>");
            sbody.Append("</br> Feature Article &nbsp; &nbsp; &nbsp; Related Articles <br> What's New</br> Guest Article</br>Classifieds</br> Feedback </br></br></br> ");
            sbody.Append("<table bgcolor=gold cellpadding=0 cellspacing=0> <tr><td><font size=4 color=brown>Feature Article</font>");
            sbody.Append("</td></tr></table> </br>  blah blah goes here - usually more than one ?");
            sbody.Append("</br></br></br> <table bgcolor=gold cellpadding=0 cellspacing=0> <tr><td> <font size=4 color=brown>What's New</font>");
            sbody.Append("</td></tr></table> </br> blank if no new news - but it could also contain a link to the current meeting minutes - depends on the type of web site.</br></br></br>");
            sbody.Append("<table bgcolor=gold cellpadding=0 cellspacing=0> <tr><td><font size=4 color=brown>Guest Article</font>");
            sbody.Append("</td></tr></table> </br> blank if no article for this period </br></br></br> <table bgcolor=gold cellpadding=0 cellspacing=0>");
            sbody.Append("<tr><td> <font size=4 color=brown>Classified (or Sponsors)</font> </td></tr></table></br> Forum Link (to the onsite forum)</br> Forum would contain Classified Section (and Sponsors Information for some types of web sites). <br> i.e. this section could be a link to an online Forum - or the info could be included in this section of the newsletter.");
            sbody.Append("</br></br></br> <table bgcolor=gold cellpadding=0 cellspacing=0> <tr><td> <font size=4 color=brown>Feedback Form</font>");
            sbody.Append("</td></tr></table></br> A link to the Feedback Forum Section.</br> i.e. this section could be a link to an online Forum - or a form could be embedded in the online page - or it could just be an email link (mailto).");
            sbody.Append("</br></br></br> <table bgcolor=gold cellpadding=0 cellspacing=0> <tr><td><font size=4 color=brown>Subscription and Archive</font> </td></tr></table> </br> How to Subscribe (could be link to 'tell-a-friend' form)</br>");
            sbody.Append("How to Unsubscribe</br> Link to Newsletter Archives </br></br></br> <table bgcolor=gold cellpadding=0 cellspacing=0> <tr><td>");
            sbody.Append("<font size=4 color=brown>About:</font> 'Newsletter Site Name Here' </td></tr></table> </br> Your Signature File or Name</br>");
            sbody.Append("Your Email Address</br> Your City, State Zip</br>Your Phone </br> Your Newsletter URL</br> </td></tr> </table>");
            //now in the stringbuilder class sbody there will be complete html code afer execution of the code.
            // now i am explainning that how we send the email as with newsletter
            //pass four parameters inside the below line. See next line here am explainning about all the parameters
            System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage("write the complete email address of sender", "write the complete email address of receiever", "Info or here you can also write the heading of a mail", sbody.ToString());
            // you can also attach somthing with this mail. below am writting a code of the attachment of a file
            if (attach_file.HasFile)
            {
                mail.Attachments.Add(new Attachment(attach_file.PostedFile.InputStream, attach_file.FileName));
            }

            mail.CC.Add("bharti222000@yahoo.com");
           // mail.CC.Add(" here u can write another email address as cc");
            System.Net.NetworkCredential mailAuthenticaion = new System.Net.NetworkCredential("here write the email address of the sender", "here write the password of your email address");
            // in the below line i am writting the smtp address of gmail and port number
            System.Net.Mail.SmtpClient mailclient = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
            mailclient.EnableSsl = true;
            mailclient.Credentials = mailAuthenticaion;
            // in below line i am writting IsBodyHtml = true; because am sending html code with this mail.
            mail.IsBodyHtml = true;
            mailclient.Send(mail);
            //Label1.Visible = True
            lbl_msg.Text = ("your e-mail has been sent");
        }
        catch (Exception ex)
        {

            lbl_msg.Text = ex.Message;
        }
        finally
        {

        }
    }
    protected void send_Click(object sender, EventArgs e)
    {
        // here i am calling the function named snd_mail() at the click of the button
        snd_mail();
    }
}
See output in this image:




Conclusion
Through this article, you have learned how to create and send newsletter in asp.net.



7 comments:

  1. hi sir how is receiever Mail id run time given . plz send me .

    ReplyDelete
  2. thanks dude for this info..............

    ReplyDelete
    Replies
    1. Hi!! mohit moudgil Thankx you sir. Please feel free to get in touch with my blog and you can also give us your expensive suggestions to improve this blog.

      Delete
  3. Command not implemented. The server response was: 5.5.1 Unrecognized command. pn9sm29761957pbb.22 - gsmtp
    how to resolve this error.???

    ReplyDelete
    Replies
    1. Hi!! amitesh. On the my this code is working fine. There is no this type of error on my end. Which email id u r using to execute this code? Only use gmail id. And port no: 587.

      Thanks
      Using asp.net

      Delete
  4. The only thing I was able to do with this code is send an attachment.Do you know of any code on newsletter sign-ups?

    ReplyDelete