Send Email with html body

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • anu b

    Send Email with html body

    Hii

    I am using System.net.mail for sending email...

    i need to send a webpage with its html content as my email
    body .....for that i used mail.Isbodyhtml as true...but it is not
    working for me

    so pls help me....

    my code is as below

    using System;

    using System.Collecti ons.Generic;

    //using System.Linq;

    using System.Web;

    using System.Web.UI;

    using System.Web.UI.W ebControls;

    using BlogEngine.Core ;

    using BlogEngine.Core .Web.Controls;

    using System.Net.Mail ;

    using System.Text.Reg ularExpressions ;

    using System.Text;


    public partial class EmailTemplate : BlogBasePage

    {

    private void Page_Load(objec t sender, System.EventArg s e)
    {

    // Put user code to initialize the page here

    }

    override protected void OnInit(EventArg s e)
    {

    //

    // CODEGEN: This call is required by the ASP.NET Web Form Designer.

    //

    InitializeCompo nent();

    base.OnInit(e);
    }

    private void InitializeCompo nent()
    {

    ////this.cmdSend.Cl ick += new System.EventHan dler(this.cmdSe nd_Click);

    this.btnSend.Cl ick += new EventHandler(bt nSend_Click);
    this.Load += new System.EventHan dler(this.Page_ Load);

    }

    void btnSend_Click(o bject sender, EventArgs e)
    {

    //throw new NotImplementedE xception();

    bool success = SendEmail(txtTo .Text);
    lblStatus.Visib le = !success;

    divThank.Visibl e = success;

    }

    private bool SendEmail(strin g email)
    {

    try

    {

    using (MailMessage mail = new MailMessage())
    {

    string msgBody="";
    string message = "";

    //Load the template page for the email template.

    BlogEngine.Core .Page page2 = BlogEngine.Core .Page.GetPage(n ew
    Guid(Session["Content"].ToString() ));


    //Take the link for the main content page and add it to the content.
    Use HTML breaks.

    msgBody = page2.AbsoluteL ink + "<br/>";msgBody += page2.Content + "<br/
    >";



    //Take the content for the email heading intro message. This is hard
    coded guid for the virtual page in the admin we created for sending
    the email

    BlogEngine.Core .Page page = BlogEngine.Core .Page.GetPage(n ew
    Guid("ff8f0685-9f6a-4758-855d-6018d9542963")) ;
    message = page.Content;



    // Add the 911 intro

    msgBody = message + "<br/>" + msgBody;
    //Email is from 911

    mail.From = new MailAddress(Blo gSettings.Insta nce.Email);


    //This should add fields from the form for the user's name and thier
    email

    MailAddress ma = new MailAddress(thi s.txtTo.Text,
    this.txtFriends Name.Text);
    mail.To.Add(ma) ;

    mail.Subject = page.Title;

    //mail.BodyEncodi ng = Encoding.GetEnc oding("iso-8859-1");





    //mail.BodyFormat = System.Web.Mail .MailFormat.Htm l;

    // AlternateView PlainView =
    AlternateView.C reateAlternateV iewFromString(" This is my Plain text
    Content, Viewable by those client that dont support html", null, "text/
    Plain");

    AlternateView htmlView =
    AlternateView.C reateAlternateV iewFromString(m sgBody, Encoding.ASCII,
    "text/html");
    htmlView.Transf erEncoding = System.Net.Mime .TransferEncodi ng.SevenBit;

    //mail.BodyEncodi ng = Encoding.GetEnc oding("GB2312") ;

    mail.BodyEncodi ng = System.Text.Enc oding.ASCII;
    //page content includes html

    mail.Body = msgBody;

    mail.BodyEncodi ng = System.Text.Enc oding.ASCII;mai l.IsBodyHtml =
    true;


    //mail.AlternateV iews.Add(PlainV iew);

    //mail.AlternateV iews.Add(htmlVi ew);








    //mail.BodyEncodi ng = System.Text.Enc oding.GetEncodi ng("GB2312");



    //mail.Body = msgBody;









    Utils.SendMailM essage(mail);
    }

    return true;
    }

    catch (Exception ex)
    {

    if (User.Identity. IsAuthenticated )
    {

    if (ex.InnerExcept ion != null)
    lblStatus.Text = ex.InnerExcepti on.Message;

    else

    lblStatus.Text = ex.Message;

    }

    return false;
    }

    }



    #region ICallbackEventH andler Members
    ////private string _Callback;

    //public string GetCallbackResu lt()

    //{

    // //return _Callback;

    //}

    public void RaiseCallbackEv ent(string eventArgument)
    {

    }

    #endregion

    }



    Select Tags... SaveCancel


    [Edit Tags]

  • sloan

    #2
    Re: Send Email with html body

    Please DO NOT MULTI POST ........

    Etiquette is to CrossPost to one (or a few) germane newsgroups (The total #
    of newsgroups should be a frugal number)


    I answered (one of) your multiposts in the csharp group.
    microsoft.publi c.dotnet.langua ges.csharp





    "anu b" <anupamabr@gmai l.comwrote in message
    news:91aebe6d-db6a-4201-bef2-7df00253eaad@j4 0g2000prh.googl egroups.com...
    Hii
    >
    I am using System.net.mail for sending email...
    >
    i need to send a webpage with its html content as my email
    body .....for that i used mail.Isbodyhtml as true...but it is not
    working for me
    >
    so pls help me....
    >
    my code is as below
    >
    using System;
    >
    using System.Collecti ons.Generic;
    >
    //using System.Linq;
    >
    using System.Web;
    >
    using System.Web.UI;
    >
    using System.Web.UI.W ebControls;
    >
    using BlogEngine.Core ;
    >
    using BlogEngine.Core .Web.Controls;
    >
    using System.Net.Mail ;
    >
    using System.Text.Reg ularExpressions ;
    >
    using System.Text;
    >
    >
    public partial class EmailTemplate : BlogBasePage
    >
    {
    >
    private void Page_Load(objec t sender, System.EventArg s e)
    {
    >
    // Put user code to initialize the page here
    >
    }
    >
    override protected void OnInit(EventArg s e)
    {
    >
    //
    >
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    >
    //
    >
    InitializeCompo nent();
    >
    base.OnInit(e);
    }
    >
    private void InitializeCompo nent()
    {
    >
    ////this.cmdSend.Cl ick += new System.EventHan dler(this.cmdSe nd_Click);
    >
    this.btnSend.Cl ick += new EventHandler(bt nSend_Click);
    this.Load += new System.EventHan dler(this.Page_ Load);
    >
    }
    >
    void btnSend_Click(o bject sender, EventArgs e)
    {
    >
    //throw new NotImplementedE xception();
    >
    bool success = SendEmail(txtTo .Text);
    lblStatus.Visib le = !success;
    >
    divThank.Visibl e = success;
    >
    }
    >
    private bool SendEmail(strin g email)
    {
    >
    try
    >
    {
    >
    using (MailMessage mail = new MailMessage())
    {
    >
    string msgBody="";
    string message = "";
    >
    //Load the template page for the email template.
    >
    BlogEngine.Core .Page page2 = BlogEngine.Core .Page.GetPage(n ew
    Guid(Session["Content"].ToString() ));
    >
    >
    //Take the link for the main content page and add it to the content.
    Use HTML breaks.
    >
    msgBody = page2.AbsoluteL ink + "<br/>";msgBody += page2.Content + "<br/
    >>";
    >
    >
    >
    >
    //Take the content for the email heading intro message. This is hard
    coded guid for the virtual page in the admin we created for sending
    the email
    >
    BlogEngine.Core .Page page = BlogEngine.Core .Page.GetPage(n ew
    Guid("ff8f0685-9f6a-4758-855d-6018d9542963")) ;
    message = page.Content;
    >
    >
    >
    // Add the 911 intro
    >
    msgBody = message + "<br/>" + msgBody;
    //Email is from 911
    >
    mail.From = new MailAddress(Blo gSettings.Insta nce.Email);
    >
    >
    //This should add fields from the form for the user's name and thier
    email
    >
    MailAddress ma = new MailAddress(thi s.txtTo.Text,
    this.txtFriends Name.Text);
    mail.To.Add(ma) ;
    >
    mail.Subject = page.Title;
    >
    //mail.BodyEncodi ng = Encoding.GetEnc oding("iso-8859-1");
    >
    >
    >
    >
    >
    //mail.BodyFormat = System.Web.Mail .MailFormat.Htm l;
    >
    // AlternateView PlainView =
    AlternateView.C reateAlternateV iewFromString(" This is my Plain text
    Content, Viewable by those client that dont support html", null, "text/
    Plain");
    >
    AlternateView htmlView =
    AlternateView.C reateAlternateV iewFromString(m sgBody, Encoding.ASCII,
    "text/html");
    htmlView.Transf erEncoding = System.Net.Mime .TransferEncodi ng.SevenBit;
    >
    //mail.BodyEncodi ng = Encoding.GetEnc oding("GB2312") ;
    >
    mail.BodyEncodi ng = System.Text.Enc oding.ASCII;
    //page content includes html
    >
    mail.Body = msgBody;
    >
    mail.BodyEncodi ng = System.Text.Enc oding.ASCII;mai l.IsBodyHtml =
    true;
    >
    >
    //mail.AlternateV iews.Add(PlainV iew);
    >
    //mail.AlternateV iews.Add(htmlVi ew);
    >
    >
    >
    >
    >
    >
    >
    >
    //mail.BodyEncodi ng = System.Text.Enc oding.GetEncodi ng("GB2312");
    >
    >
    >
    //mail.Body = msgBody;
    >
    >
    >
    >
    >
    >
    >
    >
    >
    Utils.SendMailM essage(mail);
    }
    >
    return true;
    }
    >
    catch (Exception ex)
    {
    >
    if (User.Identity. IsAuthenticated )
    {
    >
    if (ex.InnerExcept ion != null)
    lblStatus.Text = ex.InnerExcepti on.Message;
    >
    else
    >
    lblStatus.Text = ex.Message;
    >
    }
    >
    return false;
    }
    >
    }
    >
    >
    >
    #region ICallbackEventH andler Members
    ////private string _Callback;
    >
    //public string GetCallbackResu lt()
    >
    //{
    >
    // //return _Callback;
    >
    //}
    >
    public void RaiseCallbackEv ent(string eventArgument)
    {
    >
    }
    >
    #endregion
    >
    }
    >
    >
    >
    Select Tags... SaveCancel
    >
    >
    [Edit Tags]
    >

    Comment

    • anu b

      #3
      Re: Send Email with html body

      I didnt do multi posting....can u send me the link in which u answered
      the question...i can not access it

      Comment

      • sloan

        #4
        Re: Send Email with html body


        If you didn't multi post, then how can send you a link where the question is
        answered?

        .........

        Here is the multi post, FYI...the one where I responded...... .........




        Here are the other multi posts that I saw:






        Here are some definitions:

        Multipost - Many messages, all with the same content, each posted to a
        different newsgroup.



        "anu b" <anupamabr@gmai l.comwrote in message
        news:67607b01-def7-42ae-96d1-39d38e6add32@d4 2g2000prb.googl egroups.com...
        >I didnt do multi posting....can u send me the link in which u answered
        the question...i can not access it

        Comment

        Working...