I have a string that I am sending to an e-mail Server to be the body of an e-mail. I'm trying to control what font it will end up in the e-mail. Here is the string:
#"Dear " + txtName.Text + ",<br/><br/>Thank you for submitting. <br/><br/>In the event of any changes, please submit again here" + link + ". <br><br>The following is a summary for your records.<br/><br/>"
How would I make the font, say, Times New Roman?
It seems that all the information out there about font changing always involves using an ASP control but that's out of the picture in this case.
To change the font-family, you must use CSS.
string mail = #"Dear " + txtName.Text + ",<br/><br/>Thank you for submitting. <br/><br/>In the event of any changes, please submit again here" + link + ". <br><br>The following is a summary for your records.<br/><br/>"
for example with Time New Roman:
mail = "<p><span style=\"font-family: Time New Roman;\">" + mail + </span></p>"
You should know that you can use different font, if the client did not have the first one :
font-family: Times, "Times New Roman", Georgia, serif;
If you want more tips about mail formatting :
Using CSS in HTML Emails: The Real Story by Chris Coyier
How to Create Great HTML Emails with CSS by Christian MacAuley
Related
I am trying redirect to another page using below code
divIRSPayment.InnerHtml = divIRSPayment.InnerHtml
+ "Your Tax amount is $ "+lblBalance.Text
+ "<br />Click here to enter <a href='IRSEFWPayment.aspx'>IRS Payment Details</a>";
I have written above code in back-end. The amount is displaying successfully and text is also displaying successfully. But when i click on IRS Payment Details link, it is not redirecting to another page instead page is getting reloaded and displaying same page.
Probably you need to use absolute URL of the file rather than just the file name. Something like below using Page.ResolveUrl() method
".....<a href='" + Page.ResolveUrl("~/IRSEFWPayment.aspx") + "'>"
I have an empty label when i try to display image from my computer to this label
this is the code :
Label1.Text += " " + "<img src='C:/Users/AA/Documents/Bureau/car/img/image3.jpg'>";
Include image in your project for example if you include in images folder. Following code will for you.
Label1.Text += "<img src='images/image3.jpg'></img>";
If you want to open it from same path try this
Label1.Text += " " + "<img src='file://C:/Users/AA/Documents/Bureau/car/img/image3.jpg'>";
Note: file://C:/Users/AA/Documents/Bureau/car/img/image3.jpg will be loaded in IE but not in Chrome or Firefox because loading files form direct path is considered security thread in Chrome and Firefox.
Considering all this i will suggest you to put images in Image and access them from there is simplest way to do it.
This is working example in asp .net c#.
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
Label1.Text = "<img alt='' src='images/download-img.jpg' />";
Sorry, I missed that it's ASP.net
You'll probably want to use Server.MapPath("~/image.jpg") stuff to be relative to deployment location. And close the tag.
I have a simple web form in asp.net that has some validation on the form fields. I also have an image whose visibility is set to false. In my validation if statement I want code that will make that image visible if the validation has passed. Below is what I have but the image is not displaying. Thanks!
if (!Page.IsValid) return;
//Order is valid. Process it.
lblOrderDetails.Text = "<h1>Success!</h1>" +
"<b>Email: </b> " + tbEmail.Text + "<br />" +
"<b>Model: </b> " + dlModel.SelectedItem.Text + "<br />" +
"<b>Discounts: </b> ";
imgSnowboard.Visible = true;
<asp:Image Visible="false" runat="server" ImageUrl="~/SnowBoard.jpg" ID="imgSnowboard"/>
Two things, first unrelated to the question, I would start using string.Format as opposed to concatenating like that. It's just cleaner, and easier to manage. Ex.
lblOrderDetails.Text = string.Format("<h1>Success!</h1>
+ "<b>Email: </b>{0}<br />"
+ "<b>Model: </b>{1}<br />"
+ "<b>Discounts: </b> ", tbEmail.Text, dlModel.SelectedItem.Text);
Instead of the mess of + signs, this simplifies it a little more.
As to your question, I would say remove the Visible="false" property from the declaration, and instead, call out the image on PageLoad and set the visible property to false server side. Visible"false" can be kind of funky when you have a control declared with it. It almost acts like it doesn't create the control, and the C# has trouble accessing it, so it may be trying to show that image, but it literally cannot find it.
Hope this helps!
I am facing a situation where I need to process an incoming email which is in plain text format and then display the email as virtual plain text(Font Size =10.5 and Font type=Consolas) to the user.
My code till now
string Text = "<html><body><basefont size=3 font face= consolas>" + mailItem.HTMLBody + "</font></body></html>";
mailItem.HTMLBody = Text;
//Process the email and display
This works good, but during display this shows virtual plain text of font size 10.0 instead of needed 10.5. I tried changing the basefont size to 4,5 etc. this does not change the display in any way.
other options tried:
1.Adding a CSS stylesheet. This will not work good as most email clients don't support it.
2.Adding a div tag to the code like this
string Text = "<html><div style=font-size:10.5px; font-face:consolas;>" + mailItem.HTMLBody + "</font></body></div></html>"
this also does not work.
My main objective is to display a virtual plain text email with Font Size 10.5 and font type Consolas to the user after the processing has been done.
Your second approach seems to be allright - you just have a </font> and a </body> too much + you should change "font-face" to "font-family".
Also throw in double quotes - that will output one quote:
string Text = "<html><div style=""font-size:10.5px; font-family:Consolas;"">" + mailItem.HTMLBody + "</div></html>"
I'm trying to create a nice looking email for when a user registers on my website however I am not able to display the text over an image the usual way. Instead, the texts are displayed below the image.
I know I can't use StyleSheets, and that I should stick to CSS 1.4..
I'm having a background image which i'm stretching (by setting its width and height) and I would like to put some text on top of this image. In normal html, I can use css methods such as float: left; position: absolute for the div to be floating over the image. But this doesn't work within the C# code because it gets truncated in most email clients.
The image will only display if it is written in the following way:
<div style='width: 580px; font-family: Calibri; font-size:13px;'>
<img style='height: 45px; width: inherit;' src='http://www.somedomain.com/mail/background.png' />
Test
Test2
Test3
</div>
I tried to embed the code above within a style and it simply doesn't work. For example:
<div style='background:url(images/background.png); position: absolute; top:45px; width:550px; padding: 15px;'> SOME TEXT HERE </div>
OR
<div style='background-image:url(images/background.png); position: absolute; top:45px; width:550px; padding: 15px;'> SOME TEXT HERE </div>
OR
<table>
<tr>
<td style="background-image: url(background.png);">Testing Page</td>
</tr>
</table>
If you change "background-image: url(background.png);" to "background:red;" it works and this is confusing!
How do I make this work properly in most email clients as well? (Gmail, Hotmail, Outlook, Thunderbird, Windows Mail, etc.)
Thanks for any help you can provide :)
You should probably check out all of your emails in Outlook 2007. Microsoft really broke the capabilities for HTML in Emails by using the rendering engine of MS Word. To that end background-image is not recognized by Outlook 2007. Here is the MSDN on supported HTML in Outlook. Use outlook as your base line as it's support is the most basic.
Iv had similar issues with images in html and images inside an email. I solved the Image issue by using an inline attachment. The trick is to use a content id to set the image.
Hopefully this code will help:
EDIT: Width and Height settings on the actual image don't seem to work though . . . :/
string contentID = "Image1.gif".Replace(".", "") + "#test";
// create the INLINE attachment
string attachmentPath = Server.MapPath("Images/Image1.gif");
inline = new Attachment(attachmentPath);
inline.ContentDisposition.Inline = true;
inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
inline.ContentId = contentID;
inline.ContentType.MediaType = "image/gif";
inline.ContentType.Name = Path.GetFileName(attachmentPath);
//then add in the message body
//stringbuilder to construct the message
sb = new StringBuilder();
sb.Append("<div style=\"font-family:Arial\"> Hello World!<br /><br /><img src=\"##IMAGE##\" alt=\"\" Width=\"250px\" Height=\"250px\"><br /><br /></div>");
//creating the message with from and to and the smpt server connections
mail = new MailMessage("SendersEmail#Address.com", "RecieversEmail#Address.com");
SmtpServer = new SmtpClient("smtp.gmail.com"); //Add SMTP settings into the Web.Config --> ConfigurationManager.AppSettings["MyCustomId"]
mail.Subject = "Testing Emails";
mail.IsBodyHtml = true;
mail.Body = sb.ToString();
mail.Attachments.Add(inline);
// replace the tag with the correct content ID
mail.Body = mail.Body.Replace("##IMAGE##", "cid:" + contentID);
You can't use relative urls (for example "url(background.png)" or "url(images/background.png)" because it's relative to what? the email message? The email message has no folders, file system, etc. as does a web server.
You have two choices: use the complete url syntax as you did in the first example or create a MimeHtml message (MHTML). In MHTML, you can bundle an image in with your email and may be able to reference it in the way you're hoping.
I hate being the bearer of bad news, but you can't cross-platformly (is that a word?) have a background image and/or stacked elements in HTML emails.
You're trying to use CSS features that are not supported by all major email viewers. Try taking a look here to see what's supported and what's not:
http://www.campaignmonitor.com/css/
There's also a sitepoint book which you might be interested in:
http://www.sitepoint.com/books/htmlemail1/