Send email with html body using System.net.mail

A

anu b

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

but my code is as below


private bool SendEmail(string 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(new
Guid(Session["Content"].ToString() ));


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


msgBody = page2.AbsoluteLink + "<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(new
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(BlogSettings.Instance.Email);


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


MailAddress ma = new MailAddress(this.txtTo.Text,
this.txtFriendsName.Text);
mail.To.Add(ma);


mail.Subject = page.Title;


//page content includes html


mail.IsBodyHtml = true;
//mail.BodyFormat = System.Web.Mail.MailFormat.Html;


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


//AlternateView htmlView =
AlternateView.CreateAlternateViewFromString("<b>this is bold text,
and
Viewable by those client that support html </b>", null, "text/html");


//mail.AlternateViews.Add(PlainView);


//mail.AlternateViews.Add(htmlView);


mail.Body = msgBody;


//mail.BodyEncoding = System.Text.Encoding.UTF8;


//mail.BodyEncoding.IsBrowserSave = true;


//mail.Body = msgBody;


Utils.SendMailMessage(mail);



}


return true;


}


catch (Exception ex)
{

if (User.Identity.IsAuthenticated)
{


if (ex.InnerException != null)
lblStatus.Text = ex.InnerException.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?
 
S

sloan

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.spaces.live.com/Blog/cns!A68482B9628A842A!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:"+absoluteLink +"</body></html>";
}

private void CollectInfoAndSendEmail()
{

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

}

private EmailSmtpSettings GetSettings()
{
EmailSmtpSettings settings =
((EmailSmtpSettings)System.Configuration.ConfigurationSettings.GetConfig(CONFIG_SECTION_NAME));
return settings;
}




//Change my methods signature:
SendEmailFramework_1_1( SmtpServerSettings serv , string toAddress , string
subjectLine , string htmlBody )




Encapsulate, and less-spaghetti'fy your code.




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








anu b said:
Now i am sending email to my friend using session variable...

but my code is as below


private bool SendEmail(string 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(new
Guid(Session["Content"].ToString() ));


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


msgBody = page2.AbsoluteLink + "<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(new
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(BlogSettings.Instance.Email);


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


MailAddress ma = new MailAddress(this.txtTo.Text,
this.txtFriendsName.Text);
mail.To.Add(ma);


mail.Subject = page.Title;


//page content includes html


mail.IsBodyHtml = true;
//mail.BodyFormat = System.Web.Mail.MailFormat.Html;


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


//AlternateView htmlView =
AlternateView.CreateAlternateViewFromString("<b>this is bold text,
and
Viewable by those client that support html </b>", null, "text/html");


//mail.AlternateViews.Add(PlainView);


//mail.AlternateViews.Add(htmlView);


mail.Body = msgBody;


//mail.BodyEncoding = System.Text.Encoding.UTF8;


//mail.BodyEncoding.IsBrowserSave = true;


//mail.Body = msgBody;


Utils.SendMailMessage(mail);



}


return true;


}


catch (Exception ex)
{

if (User.Identity.IsAuthenticated)
{


if (ex.InnerException != null)
lblStatus.Text = ex.InnerException.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?
 
S

sloan

PS
Utils.SendMailMessage(mail);

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




sloan said:
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.spaces.live.com/Blog/cns!A68482B9628A842A!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:"+absoluteLink +"</body></html>";
}

private void CollectInfoAndSendEmail()
{

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

}

private EmailSmtpSettings GetSettings()
{
EmailSmtpSettings settings =
((EmailSmtpSettings)System.Configuration.ConfigurationSettings.GetConfig(CONFIG_SECTION_NAME));
return settings;
}




//Change my methods signature:
SendEmailFramework_1_1( SmtpServerSettings serv , string toAddress ,
string subjectLine , string htmlBody )




Encapsulate, and less-spaghetti'fy your code.




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








anu b said:
Now i am sending email to my friend using session variable...

but my code is as below


private bool SendEmail(string 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(new
Guid(Session["Content"].ToString() ));


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


msgBody = page2.AbsoluteLink + "<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(new
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(BlogSettings.Instance.Email);


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


MailAddress ma = new MailAddress(this.txtTo.Text,
this.txtFriendsName.Text);
mail.To.Add(ma);


mail.Subject = page.Title;


//page content includes html


mail.IsBodyHtml = true;
//mail.BodyFormat = System.Web.Mail.MailFormat.Html;


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


//AlternateView htmlView =
AlternateView.CreateAlternateViewFromString("<b>this is bold text,
and
Viewable by those client that support html </b>", null, "text/html");


//mail.AlternateViews.Add(PlainView);


//mail.AlternateViews.Add(htmlView);


mail.Body = msgBody;


//mail.BodyEncoding = System.Text.Encoding.UTF8;


//mail.BodyEncoding.IsBrowserSave = true;


//mail.Body = msgBody;


Utils.SendMailMessage(mail);



}


return true;


}


catch (Exception ex)
{

if (User.Identity.IsAuthenticated)
{


if (ex.InnerException != null)
lblStatus.Text = ex.InnerException.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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top