i have a strange problem with sending HTML mail in c#.
Basically i am trying to email myself the weather every morning and I begin by downloading the weather in HTML markup from an ftp site.
After obtaining the source file i then read it into a string and create a mailMessage using the following code.
string body = File.ReadAllText(#"C:\Weather.htm");
MailMessage mailMessage = new MailMessage();
SmtpClient mailClient = new SmtpClient("smtp.gmail.com");
mailClient.Credentials = new NetworkCredential(username, password);
mailClient.Port = 587;
mailClient.EnableSsl = true;
mailMessage.From = new MailAddress(emailFrom);
mailMessage.IsBodyHtml = true;
mailMessage.To.Add(emailTo);
mailMessage.Subject = "Test Email";
mailMessage.SubjectEncoding = System.Text.Encoding.Unicode;
mailMessage.Body = body;
mailMessage.BodyEncoding = System.Text.Encoding.Unicode;
mailClient.Send(mailMessage);
The problem is that whilst the email arrives in HTML format, all of the DIV / CSS are not respected and it looks weird. I have tried to email to a different email address & client and it looks the same as well as trying different encodings.
So somewhere along the line something is going wrong.
Does anyone know how to fix this and get a properly formatted email?
Good Image
BadImage
source markup
<html xmlns="http://www.w3.org/1999/xhtml"><head xmlns="">
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Sydney Forecast</title>
<link rel="stylesheet" type="text/css" href="http://www.bom.gov.au/watl/standard/common.css">
<link rel="stylesheet" type="text/css" href="http://www.bom.gov.au/weather-services/styles/text-gfe.css">
</head>
<body>
<div class="product">
<p xmlns="" class="p-id">IDN10064</p>
<p xmlns="" class="source">Australian Government Bureau of Meteorology<br/>New South Wales
<h2 xmlns="">Updated Sydney Forecast</h2>
<p xmlns="" class="date">Issued at 8:11 am EST on Thursday 9 September 2010<br>for the period until midnight EST Wednesday 15 September 2010.</p>
<h3 xmlns="" class="warning">Warning Summary at issue time</h3>
<p xmlns="">Nil.</p>
<p xmlns="" class="p-id">Details of warnings are available on the Bureau's website www.bom.gov.au, by telephone 1300-659-218* or through some TV and radio broadcasts.</p>
<h3 xmlns="" class="day">Forecast for the rest of Thursday</h3>
<p xmlns="" class="sl">Cloud increasing. Areas of rain this afternoon and evening. Winds northerly averaging up to 20 km/h.</p>
<div xmlns="" class="grid">
<div class="line space-b">
<div class="name">City Centre</div>
<div class="wx">A little rain later.</div>
<div class="max_l">Max</div>
<div class="max_v">20</div>
</div>
<div class="line">
<div class="rain_l">Chance of any rainfall:</div>
<div class="rain_prob_v">30%</div>
<div class="rain_l">Chance of no rainfall:</div>
<div class="rain_prob_v">70%</div>
<div class="rain_l">Rainfall:</div>
<div class="rain_v">0 to 1 mm</div>
</div>
<div class="line space-b">
<div class="name">Penrith</div>
<div class="wx">Late rain.</div>
<div class="max_l">Max</div>
<div class="max_v">20</div>
</div>
<div class="line">
<div class="rain_l">Chance of any rainfall:</div>
<div class="rain_prob_v">30%</div>
<div class="rain_l">Chance of no rainfall:</div>
<div class="rain_prob_v">70%</div>
<div class="rain_l">Rainfall:</div>
<div class="rain_v">0 to 1 mm</div>
</div>
<div class="line">
<div>
<h4>Around Sydney</h4>
</div>
</div>
<div class="line">
<div class="name">Liverpool</div>
<div class="max_l">Max</div>
<div class="max_v">20</div>
<div class="name">Parramatta</div>
<div class="max_l">Max</div>
<div class="max_v">20</div>
</div>
<div class="line">
<div class="name">Terrey Hills</div>
<div class="max_l">Max</div>
<div class="max_v">19</div>
<div class="name">Campbelltown</div>
<div class="max_l">Max</div>
<div class="max_v">19</div>
</div>
<div class="line">
<div class="name">Richmond</div>
<div class="max_l">Max</div>
<div class="max_v">20</div>
<div class="name">Bondi</div>
<div class="max_l">Max</div>
<div class="max_v">19</div>
</div>
</div>
<p xmlns="" class="sl">Fire Danger: Low to Moderate [0-11]</p>
<p xmlns="" class="sl">UV Alert from 9:10 am to 2:40 pm, UV Index predicted to reach 6 [High]</p>
<h3 xmlns="" class="day">Forecast for Friday</h3>
<p xmlns="" class="sl">Partly cloudy. Winds west to northwesterly averaging up to 25 km/h tending westerly up to 40 km/h around midday.</p>
<div xmlns="" class="grid">
<div class="line space-b">
<div class="name">City Centre</div>
<div class="wx">Becoming windy.</div>
<div class="min_l">Min</div>
<div class="min_v">13</div>
<div class="max_l">Max</div>
<div class="max_v">22</div>
</div>
<div class="line space-b">
<div class="name">Penrith</div>
<div class="wx">Partly cloudy. Becoming windy.</div>
<div class="min_l">Min</div>
<div class="min_v">11</div>
<div class="max_l">Max</div>
<div class="max_v">23</div>
</div>
</div>
<h3 xmlns="" class="day">Forecast for Saturday</h3>
<p xmlns="" class="sl">Sunny. Winds west to southwesterly averaging up to 25 km/h tending mainly southeast to southwesterly up to 20 km/h around midday.</p>
<div xmlns="" class="grid">
<div class="line space-b">
<div class="name">City Centre</div>
<div class="wx">Sunny.</div>
<div class="min_l">Min</div>
<div class="min_v">12</div>
<div class="max_l">Max</div>
<div class="max_v">21</div>
</div>
<div class="line space-b">
<div class="name">Penrith</div>
<div class="wx">Sunny.</div>
<div class="min_l">Min</div>
<div class="min_v">9</div>
<div class="max_l">Max</div>
<div class="max_v">22</div>
</div>
</div>
<h3 xmlns="" class="day">Forecast for Sunday</h3>
<p xmlns="" class="sl">Mostly sunny. Light winds.</p>
<div xmlns="" class="grid">
<div class="line space-b">
<div class="name">City Centre</div>
<div class="wx">Mostly sunny.</div>
<div class="min_l">Min</div>
<div class="min_v">10</div>
<div class="max_l">Max</div>
<div class="max_v">21</div>
</div>
<div class="line space-b">
<div class="name">Penrith</div>
<div class="wx">Sunny.</div>
<div class="min_l">Min</div>
<div class="min_v">6</div>
<div class="max_l">Max</div>
<div class="max_v">23</div>
</div>
</div>
<h3 xmlns="" class="day">Forecast for Monday</h3>
<p xmlns="" class="sl">Becoming cloudy. Isolated showers later in the day. Winds west to southwesterly averaging up to 25 km/h.</p>
<div xmlns="" class="grid">
<div class="line space-b">
<div class="name">City Centre</div>
<div class="wx">Shower or two developing.</div>
<div class="min_l">Min</div>
<div class="min_v">13</div>
<div class="max_l">Max</div>
<div class="max_v">22</div>
</div>
<div class="line space-b">
<div class="name">Penrith</div>
<div class="wx">Shower or two developing.</div>
<div class="min_l">Min</div>
<div class="min_v">10</div>
<div class="max_l">Max</div>
<div class="max_v">22</div>
</div>
</div>
<h3 xmlns="" class="day">Forecast for Tuesday</h3>
<p xmlns="" class="sl">Sunny. Light winds tending north to northeasterly up to 20 km/h during the evening.</p>
<div xmlns="" class="grid">
<div class="line space-b">
<div class="name">City Centre</div>
<div class="wx">Sunny.</div>
<div class="min_l">Min</div>
<div class="min_v">12</div>
<div class="max_l">Max</div>
<div class="max_v">19</div>
</div>
<div class="line space-b">
<div class="name">Penrith</div>
<div class="wx">Sunny.</div>
<div class="min_l">Min</div>
<div class="min_v">10</div>
<div class="max_l">Max</div>
<div class="max_v">19</div>
</div>
</div>
<h3 xmlns="" class="day">Forecast for Wednesday</h3>
<p xmlns="" class="sl">Isolated showers during the morning. Sunny afternoon. Winds northwesterly averaging up to 25 km/h tending westerly up to 35 km/h during the morning.</p>
<div xmlns="" class="grid">
<div class="line space-b">
<div class="name">City Centre</div>
<div class="wx">Shower or two clearing.</div>
<div class="min_l">Min</div>
<div class="min_v">12</div>
<div class="max_l">Max</div>
<div class="max_v">20</div>
</div>
<div class="line space-b">
<div class="name">Penrith</div>
<div class="wx">Shower or two clearing.</div>
<div class="min_l">Min</div>
<div class="min_v">8</div>
<div class="max_l">Max</div>
<div class="max_v">21</div>
</div>
</div>
<p xmlns="" class="dt">The next routine forecast will be issued at 4:20 pm EST Thursday.</p>
<p xmlns="" class="p-id">* Calls to 1300 numbers cost around 27.5c incl. GST, higher from mobiles or public phones.</p>
</div>
</html>
css settings which are supported in web pages are not 100% supported in email. so it is normal the content rends differently.
Also it is not recommended using divs in email, since table is a safer option for email content.
check the link:
http://css-tricks.com/using-css-in-html-emails-the-real-story/
http://www.campaignmonitor.com/css/
The 'bad' image looks exactly like how the HTML would render if the CSS wasn't being applied. If you're on a webmail, use something like HTTPFox to check if the browser's even requesting the CSS files - they may be getting supressed as part of the anti-spam measure (no remote content loaded automatically). If it's a real mail client (Outlook, Thunderbird, etc...) use a wire sniffer and do the same check.
Most likely you'll have to download the css and embed it inline, preferably somewhere after the <body> tag.
A lot of email clients cut out everything that appears above the body tag (i.e. your CSS links) - what happens if you do all your CSS inline??
This link might offer some useful help
I had to do this type of project recently. Try writing to the lowest common denominator, such as OE 6 and prior. OE 6.0 and prior will not be consistent in its presentation of CSS - even if it is embedded in the header. Same with DIV in the body.
(Retrieved from Yahoo Answers 09-09-2010 1)
Best Answer - Chosen by Voters
It would definitely depend on which
version of outlook you are using. Any
that do, probably only support inline
styles...
<p style=" color:red; ">this is red</p>
... but certainly not linked style
sheets, and probably not stylesheets
in the head.
Suggested Solution: Convert source rows to HTML 2.0 formatted table rows.
From your source, programmatically convert each line to an HTML 2.0 table row, using embedded styles. Then append each table row in the body. Finally, close the table.
This is the most sure way to get the results you want (short of remote loading a pre-formatted image).
If you want to include images in the table, you can remote load them, OR, you can insert them as attachments, and reference the attachment in the table body. I tried using 64-bit image encoding, but it was not consistent in email clients. I opted for the attachment method, since I did not want to rely on a server from which to remote load images.
Related
I'm trying to scrape data from web site with HtmlAgilityPack. One site has this structure:
<footer id="footer-index"><div class="container-fluid bg-dark-blue">
<div class="container bg-dark-blue text-white">
<div class="col-12 col-lg-3">
<p>
<strong>XXXX</strong>
<br>viale XXXXXXX
<br>12345 XXXXXX
<br>CF/P.IVA XXXXXXXXXXXXX
</p>
<p>Tel <a class="text-white" href="tel:+3912345">+39 123456</a></p>
<p class="text-white">Fax +39 123456</p>
</div>
<div class="col-12 col-lg-3 d-print-none">
<p>Informativa su Privacy e Cookie</p>
</div>
<div class="col-12 col-lg-3">
<p>Copyright 2018 XXXXX<br>Tutti i diritti riservati.</p>
</div>
</div>
</div>
</div>
</footer>
I can capture footer element with
pageDocument.DocumentNode.SelectSingleNode("//footer");
but the content of the footer is not captured. There is a way to get the footer content without constraint to know the structure of the footer' content?
Thanks
I am working on a web page in Asp.net Core MVC (dotnet 2.2) with authentication, and I want to replace Bootstrap with SemanticUI. I managed to do it on everything but Identity.
Or is there a way to build in user management but with custom written sites? (with Identity pages disabled).
I have changed layout for Identity, but there are still some bootstrap things and I dont know where are they from.
Page source:
<div class="row"> <!--Why is this here?-->
<div class="col-md-3"> <!--Why is this here?-->
<div class="ui grid">
<div class="four wide column">
<div class="ui vertical tabular menu">
<a class="item teal active" id="profile" href="/Identity/Account/Manage">Profile</a>
<a class="item" id="change-password" href="/Identity/Account/Manage/ChangePassword">Password</a>
<a class="item" id="two-factor" href="/Identity/Account/Manage/TwoFactorAuthentication">Two-factor authentication</a>
<a class="item" id="personal-data" href="/Identity/Account/Manage/PersonalData">Personal data</a>¨
</div>
</div>
</div>
Layout that is set in Identity:
<html>
<head>
<meta name="viewport" content="width=device-width, viewport-fit=cover">
<meta charset="UTF-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/fomantic-ui#2.7.6/dist/semantic.min.css">
<script src="https://cdn.jsdelivr.net/npm/fomantic-ui#2.7.6/dist/semantic.min.js"></script>
<title>Title</title>
<link rel="stylesheet" href="~/style.css">
</head>
<body>
<div class="ui inverted header">
<div class="ui inverted stackable menu">
<div class="ui container">
<div class="item">
<img src="~/spacex2.png" style="width: 15em">
</div>
<a class="item" asp-controller="Home" asp-action="Index">Domů</a>
<a class="item">Projekty</a>
<partial name="_LoginPartial" />
</div>
</div>
<div class="ui container">
<partial name="_CookieConsentPartial" />
#RenderBody()
</div>
<div class="ui inverted vertical footer segment" id="footer">
<div class="ui container">
<div class="ui stackable inverted divided equal height stackable grid">
<div class="three wide column">
<h4 class="ui inverted header">About</h4>
<div class="ui inverted link list">
Sitemap
Contact Us
Religious Ceremonies
Gazebo Plans
</div>
</div>
<div class="three wide column">
<h4 class="ui inverted header">Services</h4>
<div class="ui inverted link list">
Banana Pre-Order
DNA FAQ
How To Access
Favorite X-Men
</div>
</div>
<div class="seven wide column">
<h4 class="ui inverted header">Footer Header</h4>
<p>Extra space for a call to action inside the footer that could help re-engage users.</p>
</div>
</div>
</div>
</div>
#RenderSection("Scripts", required: false)
</body>
</html>
i am trying to send values to tag-text using c# selenium. tried different scenarios but could not. can any one help me?
<div class="list">
<div class="darkTag">
<div class="contents">
<span class="tag-text">Tamil</span>
<div class="tag-remove">x</div>
</div>
<div class="tag-bg" style="background: rgb(255, 87, 51);"></div>
</div>
</div>
I'm trying to extract the date element of the following page chunk:
<div class="row small edr-process-panel__card-row">
<div class="col-md-6">
<label translate="DETAILS_LAST_SEEN" class="ng-scope">Last Seen</label>
</div>
<!-- ngIf: $ctrl.threat -->
<div class="col-md-6 text-right ng-scope" ng-if="$ctrl.threat" style="">
<label class="ng-binding">28 Nov 06:19:07</label>
</div><!-- end ngIf: $ctrl.threat -->
</div>
<div class="row small edr-process-panel__card-row">
<div class="col-md-7">
<label translate="PREVALENCE" class="ng-scope">Prevalence</label>
</div>
<!-- ngIf: $ctrl.threat -->
<div class="col-md-5 text-right ng-scope" ng-if="$ctrl.threat" style="">
<label class="ng-binding">1</label>
</div><!-- end ngIf: $ctrl.threat -->
</div>
This was working before but not anymore:
lastSeen = driver.FindElement(By.XPath("//label[#translate='DETAILS_LAST_SEEN']/following::label[1]")).Text;
Any idea how I can get the date?
Thanks, John.
I think you can use combination of dynamic xpath
//label[#class='ng-scope' and #translate='DETAILS_LAST_SEEN']//following::label[1]
Seems your xpath was not correct. Can you try with :
lastSeen = driver.FindElement(By.XPath("//label[#translate='DETAILS_LAST_SEEN']//following::label[1]")).Text;
Try the following:
driver.FindElement(By.XPath("//label[contains(#class, 'ng-binding')]")).Text;
See the following if you need more help with XPaths: Xpath changing after the page gest loaded every time.
Try the following code:
driver.FindElement(By.XPath("//div/label[#class='ng-binding']").Text
Should do the trick.
I got very simple html page which looks like this:
(index.html):
<html>
<head>
<title>Test</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Test</h1></center>
A
<div class="masterWrapper">
<div class="headerWrapper">
<div class="headerLogo">
</div>
</div>
<div class="clear">
</div>
<div class="sliderWrapper">
</div>
<div class="clear">
</div>
<div class="contentWrapper">
<div class="rightColumn">
<h2>
Test <strong class="green">|</strong><strong>Test2</strong>?</h2>
</div>
</div>
</div>
</body>
</html>
and almost the same subpage:
(a.html):
<html>
<head>
<title>Test2</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Test2</h1></center>
<div class="masterWrapper">
<div class="headerWrapper">
<div class="headerLogo">
</div>
</div>
<div class="clear">
</div>
<div class="sliderWrapper">
</div>
<div class="clear">
</div>
<div class="contentWrapper">
<div class="rightColumn">
<h2>
Test <strong class="green">|</strong><strong>Test2</strong>?</h2>
</div>
</div>
</div>
</body>
</html>
the only difference here is the lack of A in the second page.
I scrape the above (using Abot) and now the weird thing happens, I'm trying to select ALL h2 tags for current page by XPath, like this:
page.HtmlDocument.DocumentNode.SelectNodes("//h2");
If I use the above on "index.html" - it works fine, but if I use the same line to get nodes from "a.html" - it returns NULL. I also tried SelectNodes("h2"), SelectNodes("/h2"), SelectNodes(".//h2") and SelectNodes("./h2") but all return NULL. Inner html return by Abot is the same for both pages (minus the link I mentioned above).
If I use linq instead of XPath, like this:
page.HtmlDocument.DocumentNode.DescendantNodes().Where(x => x.Name == tagNameMinusAttribute)
it returns correct values on both pages - why doesn't it work with XPath? Is this some bug in HtmlAgilityPack, or is my XPath syntax wrong?
Thanks in advance.
Best regards.