Send email with html body using System.net.mail

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

    Send email with html body using System.net.mail

    Now i am sending email to my friend using session variable...

    but my code is as below


    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;


    //page content includes html


    mail.IsBodyHtml = true;
    //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(" <b>this is bold text,
    and
    Viewable by those client that support html </b>", null, "text/html");


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


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


    mail.Body = msgBody;


    //mail.BodyEncodi ng = System.Text.Enc oding.UTF8;


    //mail.BodyEncodi ng.IsBrowserSav e = true;


    //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;


    }
    }


    eventhogh i did mail.Isbodyhtml = true....

    my email shows html tags in it i dont need that...how can i get that
    do u hav any idea?


  • sloan

    #2
    Re: Send email with html body using System.net.mail

    First, do NOT MULTIPOST.

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

    .......
    Now your issue.

    You can try getting the source code here:
    http://sholliday.space s.live.com/Blog/cns!A68482B9628 A842A!138.entry

    Download the example. I am able to send html email through that code.

    You should encapsulate your method better.
    You have Session data being pulled from within your method. 8<

    Go get the source code from my blog, and you will have an encapsulated
    method.
    Then use a different function to create the html you want to send.
    Then call the method.

    private string CreateHtml ( string absoluteLink , string msgBody )
    {
    return "<html><body>"+ "<h3>Hi :"+msgBody+" </h3>"+"<br/>You
    visited:"+absol uteLink +"</body></html>";
    }

    private void CollectInfoAndS endEmail()
    {

    string html = CreateHtml( " http: // www . yahoo .com " , "Billy" );
    SendEmailFramew ork_1_1( GetSettings() , "me@ toyou.com" , "Message From
    the System", html);

    }

    private EmailSmtpSettin gs GetSettings()
    {
    EmailSmtpSettin gs settings =
    ((EmailSmtpSett ings)System.Con figuration.Conf igurationSettin gs.GetConfig(CO NFIG_SECTION_NA ME));
    return settings;
    }




    //Change my methods signature:
    SendEmailFramew ork_1_1( SmtpServerSetti ngs serv , string toAddress , string
    subjectLine , string htmlBody )




    Encapsulate, and less-spaghetti'fy your code.




    Note: SmtpServerSetti ngs is my custom object available in the downloadable
    sourcecode.








    "anu b" <anupamabr@gmai l.comwrote in message
    news:f2ef2286-4763-4f10-98fb-674af02019f8@j4 0g2000prh.googl egroups.com...
    Now i am sending email to my friend using session variable...
    >
    but my code is as below
    >
    >
    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;
    >
    >
    //page content includes html
    >
    >
    mail.IsBodyHtml = true;
    //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(" <b>this is bold text,
    and
    Viewable by those client that support html </b>", null, "text/html");
    >
    >
    //mail.AlternateV iews.Add(PlainV iew);
    >
    >
    //mail.AlternateV iews.Add(htmlVi ew);
    >
    >
    mail.Body = msgBody;
    >
    >
    //mail.BodyEncodi ng = System.Text.Enc oding.UTF8;
    >
    >
    //mail.BodyEncodi ng.IsBrowserSav e = true;
    >
    >
    //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;
    >
    >
    }
    }
    >
    >
    eventhogh i did mail.Isbodyhtml = true....
    >
    my email shows html tags in it i dont need that...how can i get that
    do u hav any idea?
    >
    >

    Comment

    • sloan

      #3
      Re: Send email with html body using System.net.mail


      PS
      Utils.SendMailM essage(mail);

      That code isn't available, so we have no idea what is happening in its
      contents.




      "sloan" <sloan@ipass.ne twrote in message
      news:ux79E0GQJH A.1164@TK2MSFTN GP02.phx.gbl...
      First, do NOT MULTIPOST.
      >
      Etiquette is to CrossPost to one (or a few) germane newsgroups (The total
      # of newsgroups should be a frugal number)
      >
      ......
      Now your issue.
      >
      You can try getting the source code here:
      http://sholliday.space s.live.com/Blog/cns!A68482B9628 A842A!138.entry
      >
      Download the example. I am able to send html email through that code.
      >
      You should encapsulate your method better.
      You have Session data being pulled from within your method. 8<
      >
      Go get the source code from my blog, and you will have an encapsulated
      method.
      Then use a different function to create the html you want to send.
      Then call the method.
      >
      private string CreateHtml ( string absoluteLink , string msgBody )
      {
      return "<html><body>"+ "<h3>Hi :"+msgBody+" </h3>"+"<br/>You
      visited:"+absol uteLink +"</body></html>";
      }
      >
      private void CollectInfoAndS endEmail()
      {
      >
      string html = CreateHtml( " http: // www . yahoo .com " , "Billy" );
      SendEmailFramew ork_1_1( GetSettings() , "me@ toyou.com" , "Message From
      the System", html);
      >
      }
      >
      private EmailSmtpSettin gs GetSettings()
      {
      EmailSmtpSettin gs settings =
      ((EmailSmtpSett ings)System.Con figuration.Conf igurationSettin gs.GetConfig(CO NFIG_SECTION_NA ME));
      return settings;
      }
      >
      >
      >
      >
      //Change my methods signature:
      SendEmailFramew ork_1_1( SmtpServerSetti ngs serv , string toAddress ,
      string subjectLine , string htmlBody )
      >
      >
      >
      >
      Encapsulate, and less-spaghetti'fy your code.
      >
      >
      >
      >
      Note: SmtpServerSetti ngs is my custom object available in the
      downloadable sourcecode.
      >
      >
      >
      >
      >
      >
      >
      >
      "anu b" <anupamabr@gmai l.comwrote in message
      news:f2ef2286-4763-4f10-98fb-674af02019f8@j4 0g2000prh.googl egroups.com...
      >Now i am sending email to my friend using session variable...
      >>
      >but my code is as below
      >>
      >>
      >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.Cor e.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.Cor e.Page page = BlogEngine.Core .Page.GetPage(n ew
      >Guid("ff8f06 85-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.txtFriend sName.Text);
      >mail.To.Add(ma );
      >>
      >>
      >mail.Subject = page.Title;
      >>
      >>
      >//page content includes html
      >>
      >>
      >mail.IsBodyHtm l = true;
      >//mail.BodyFormat = System.Web.Mail .MailFormat.Htm l;
      >>
      >>
      >// AlternateView PlainView =
      >AlternateView. CreateAlternate ViewFromString( "This is my Plain text
      >Content, Viewable by those client that dont support html", null,
      >"text/
      >Plain");
      >>
      >>
      >//AlternateView htmlView =
      >AlternateView. CreateAlternate ViewFromString( "<b>this is bold text,
      >and
      >Viewable by those client that support html </b>", null, "text/html");
      >>
      >>
      >//mail.AlternateV iews.Add(PlainV iew);
      >>
      >>
      >//mail.AlternateV iews.Add(htmlVi ew);
      >>
      >>
      >mail.Body = msgBody;
      >>
      >>
      >//mail.BodyEncodi ng = System.Text.Enc oding.UTF8;
      >>
      >>
      >//mail.BodyEncodi ng.IsBrowserSav e = true;
      >>
      >>
      >//mail.Body = msgBody;
      >>
      >>
      >Utils.SendMail Message(mail);
      >>
      >>
      >>
      >}
      >>
      >>
      >return true;
      >>
      >>
      >}
      >>
      >>
      >catch (Exception ex)
      >{
      >>
      >if (User.Identity. IsAuthenticated )
      >{
      >>
      >>
      >if (ex.InnerExcept ion != null)
      >lblStatus.Te xt = ex.InnerExcepti on.Message;
      >>
      >>
      >else
      >>
      >>
      >lblStatus.Te xt = ex.Message;
      >>
      >>
      >>
      >}
      >>
      >>
      >return false;
      >>
      >>
      >}
      >}
      >>
      >>
      >eventhogh i did mail.Isbodyhtml = true....
      >>
      >my email shows html tags in it i dont need that...how can i get that
      >do u hav any idea?
      >>
      >>
      >
      >

      Comment

      Working...