By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,635 Members | 1,707 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,635 IT Pros & Developers. It's quick & easy.

Send email with html body using System.net.mail

P: n/a
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>th is 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?
Nov 6 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.Configura tionSettings.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" <an*******@gmail.comwrote in message
news:f2**********************************@j40g2000 prh.googlegroups.com...
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>th is 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?


Nov 7 '08 #2

P: n/a

PS
Utils.SendMailMessage(mail);

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


"sloan" <sl***@ipass.netwrote in message
news:ux**************@TK2MSFTNGP02.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.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.Configura tionSettings.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" <an*******@gmail.comwrote in message
news:f2**********************************@j40g2000 prh.googlegroups.com...
>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("Th is is my Plain text
Content, Viewable by those client that dont support html", null,
"text/
Plain");
//AlternateView htmlView =
AlternateView.CreateAlternateViewFromString("<b>t his 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?



Nov 7 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.