C# + Append DataTable row to html email template - c#

I have a DataTable of records which I need to append as information to my Email template htm. Currently what I am doing here works fine but that is if I only have 1 row of record. How can I go about appending the htm template such that I can have multiple postings in the email
e.g Sample Email Screen (Assuming my DataTable returns 3 rows of record):
Dear Sir, your daily car posting results:
Image
Toyota
Cambry
$10000
Image
Honda
GT
$10000
Image
Nissan
Sunny
$10000
Loop DataTable row:
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
primaryImage = dr["PrimaryImage"].ToString();
email = dr["Email"].ToString();
postTitle = dr["Model"].ToString();
model = dr["Model"].ToString();
askingPrice = dr["AskingPrice"].ToString();
var mail = new Email();
mail.IsBodyHtml = true;
mail.MailAddresses = email;
mail.MailSubject = "Test";
mail.HtmFileName = "Email.htm";
var dict = new Dictionary<string, string>
{
{"<%PrimaryImage%>", primaryImage },
{"<%PostTitle%>", postTitle},
{"<%Model%>", model},
{"<%AskingPrice%", askingPrice}
};
mail.Dict = dict;
MailMessage mailMessage;
mailMessage = mail.CreateMailMessage();
Email.Send(mailMessage, 3, 3000, true);
}
}
Create Mail Message:
public MailMessage CreateMailMessage()
{
MailMessage mail = new MailMessage();
mail.IsBodyHtml = IsBodyHtml;
mail.From = new MailAddress("xxx#yahoo.com", "xxx");
mail.Bcc.Add(MailAddresses);
mail.Subject = MailSubject;
string body = "";
string filePath =
HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings["TEMPLATES"] + "/");
if (File.Exists(filePath + HtmFileName))
{
FileStream f = new FileStream(filePath + HtmFileName, FileMode.Open);
StreamReader sr = new StreamReader(f);
body = sr.ReadToEnd();
foreach (var pair in Dict)
{
body = body.Replace(pair.Key, pair.Value);
}
f.Close();
}
mail.Body = body;
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess |
DeliveryNotificationOptions.OnFailure;
return mail;
}
Portion of the Email.htm template:
<body>
<form id="form1" runat="server">
<div style="border: thin solid #E1E1E1; background-color: #F0F0F0; margin-bottom: 10px; padding-top: 3px; padding-bottom: 3px; padding-left:5px;">
Postings
</div>
<div>
<a><img src="<%PrimaryImage%>"/></a>
<br/><br/>
Sell Post Title: <%PostTitle%>
<br/><br/>
Model: <%Model%>
<br/><br/>
Asking Price: <%AskingPrice%>
</div>
</form>
</body>

#M.S : There must be some condition on the basis of which you will decide which attribute goes to which td. You can wrap up this logic in some method and generate a class name. Below is a way how you generate a classname on the basis of rownum.
var className="" ;
var rowNum=0;
foreach (var entry in dataTable)
{
className=GetClassName(rowNum)
innerHtml += "<tr>";
innerHtml += "<td class='"+ className +"'>" + entry.PrimaryImage + "</td> ";
innerHtml += "</tr>";
rowNum++;
}
public static string GetClassName(int rowCount)
{
switch (rowCount)
{
case 1:
return "class1";
case 2:
return "class2";
case 3:
return "class3";
default:
return "unassignedClass";
}
}

I have always used HtmlAgilityPack to prepare htmlcontent and achieve things like this.
private string PrepareHtmlContent(List<DataRow> dataTable)
{
var htmlDocument = new HtmlDocument();
var html = EmailTemplates.GetTemplate("yourTemplate");
htmlDocument.LoadHtml(html);
var recordsContainerNode = htmlDocument.GetElementbyId("dataTable");
if (recordsContainerNode != null)
{
var innerHtml = "";
foreach (var entry in dataTable)
{
innerHtml += "<tr>";
innerHtml += "<td>" + entry.PrimaryImage + "</td> ";
innerHtml += "<td>" + entry.Model + "</td> ";
innerHtml += "<td>" + entry.AskingPrice + "</td> ";
innerHtml += "</tr>";
}
recordsContainerNode.InnerHtml = innerHtml;
}
using (var stringWriter = new StringWriter())
{
htmlDocument.Save(stringWriter);
return stringWriter.GetStringBuilder().ToString();
}
}
And your template should be sth like this
<body>
<form id="form1" runat="server">
<div style="border: thin solid #E1E1E1; background-color: #F0F0F0; margin-bottom: 10px; padding-top: 3px; padding-bottom: 3px; padding-left:5px;">
Postings
</div>
<table>
<thead> </thead>
<tbody id="dataTable">
</tbody>
</table>
</form>
</body>

Related

How to apped in table format using String builder

I am trying to make a table format in the body of email.
This is how I do the appending
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<table border='1' cellpadding='0' cellspacing='0'>");
sb.Append("<tr>");
sb.Append("<td>ID</td>");
sb.Append("<td>STATUS</td>");
sb.Append("</tr>");
//sb.Append("</table>");
//sb.Append(" < table border = '1' cellpadding = '0' cellspacing = '0' width = '100%' >");
foreach (var item in mylist)
{
sb.Append("<tr>");
sb.Append("<td>" + item.message + "</td>");
sb.Append("<td>" + item.transactionid + "</td>");
sb.Append("</tr>");
}
sb.Append("</table>");
var sendmail = new MailAddress("", "Akhil");
var receiver = new MailAddress("", "Buddy");
var subject = "Request for Asset!!";
var body = sb.ToString();
If you using System.Web.Mail then put messageg.BodyFormat = MailFormat.Html;
If you using System.Net.Mail then put message.IsBodyHtml = true; .
IsBodyHtml & MailFormat.Html states that your message is HTML formatted.
> This is not a true way, first you should add the HTML file, then after create a table inside the file.
Now attach a file with StringBuilder.
## Here is my send email with attachment code ##
StringBuilder sbMailBody = new StringBuilder();
sbMailBody.Append(Server.MapPath("~/Content/fielname.html"));
var sendmail = new MailAddress("", "Akhil");
var receiver = new MailAddress("", "Buddy");
var subject = "Request for Asset!!";
var body = sbMailBody.ToString();

Create HTML Table in C# with foreach loop

I want to create an HTML table with my c# program.
I've tried this:
internal string CreateMailBody(mailObj dataObj)
{
string body = string.Empty;
body +=
"Dear all, there is my new table:" +
"TableNo: " + dataObj.Obj.Table.TableNumber+ "<br /> <br />";
body += "<table border='1'><tr><th>Line No</th><th>Table</th><th>Description</th><th>Count</th><th>Met</th><th>something</th></tr>";
foreach (var item in dataObj.Table.TableLineCollection)
{
body += "<tr><td>" + item.LineNumber +"</td>";
body += "<tr><td>" + item.Table+"</td>";
body += "<tr><td>" + item.Description+"</td></tr>";
}
return body;
}
Will it give a better solution for this ?
You may mix StringBuilder using the method AppendFormat and string literals (# in from of a string, so you can write multiple lines).
StringBuilder body = new StringBuilder();
body.AppendFormat (
#"Dear all, there is my new table:
TableNo: {0} <br /> <br />
<table border='1'><tr><th>Line No</th><th>Table</th><th>Description</th><th>Count</th><th>Met</th><th>something</th></tr>"
, dataObj.Obj.Table.TableNumber);
foreach (var item in dataObj.Table.TableLineCollection)
{
body.AppendFormat(
#"<tr><td> {0}</td>
<tr><td> {1}</td>
<tr><td> {2}</td></tr>",
item.LineNumber, item.Table, item.Description);
}

Export aspx web page to pdf using htm

I've got a htm page to arrange the position for me to export the aspx web page as pdf. Here is the code:
<form id="form1" runat="server">
<div>
<h1>Packaging Slip</h1>
<table>
<tr>
<td>Packing Date</td>
<td>[PACKINGDATE]</td>
</tr>
<tr>
<td>Delivery Date</td>
<td>[DELIVERYDATE]</td>
</tr>
<tr>
<td>Beneficiary Name</td>
<td>[BENEFICIARYNAME]</td>
</tr>
<tr>
<td colspan ="2">[ADDRESS]</td>
</tr>
<tr>
<td>Mobile</td>
<td>[MOBILE]</td>
</tr>
<tr>
<td>Contact(Work)</td>
<td>[CONTACTWORK]</td>
</tr>
<tr>
<td>Fax</td>
<td>[FAX]</td>
</tr>
</table>
[ITEMS]
</div>
</form>
The [ITEM] I should replace with grid view later on. And here is the code for me to export as pdf:
// Create a Document object
var document = new Document(PageSize.A4, 50, 50, 25, 25);
// Create a new PdfWriter object, specifying the output stream
var output = new MemoryStream();
var writer = PdfWriter.GetInstance(document, output);
// Open the Document for writing
document.Open();
string contents = File.ReadAllText(Server.MapPath("~/FoodBankSystem/Distributions/Packing/PackagingSlip.htm"));
// Replace the placeholders with the user-specified text
contents = contents.Replace("[PACKINGDATE]", lblPackDate.Text);
contents = contents.Replace("[DELIVERYDATE]", lblDeliveryDate.Text);
contents = contents.Replace("[BENEFICIARYNAME]", lblBeneficiaryName.Text);
contents = contents.Replace("[ADDRESS]", lblAddress.Text);
contents = contents.Replace("[MOBILE]", lblContactMobile.Text);
contents = contents.Replace("[CONTACTWORK]", lblWorkContact.Text);
contents = contents.Replace("[FAX]", lblFax.Text);
// Step 4: Parse the HTML string into a collection of elements...
var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), null);
// Enumerate the elements, adding each one to the Document...
foreach (var htmlElement in parsedHtmlElements)
document.Add(htmlElement as IElement);
document.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "inline;filename=UserDetails.pdf");
Response.BinaryWrite(output.ToArray());
However, for the [ITEM] part, I have no idea how to loop it. Can somebody posts some example? Thanks in advance.
For [Items] You need to generate HTML Table for Code Behind based on Item data and Columns and Replace it this TAG
DataTable dt = new DataTable();
dt.Columns.Add("Item");
dt.Columns.Add("Description");
dt.Columns.Add("QTY");
for(int i=0;i<10;i++)
{
DataRow dr = dt.NewRow();
dr["Item"] = "Item" + i.ToString();
dr["Description"] = "Test" + i.ToString();
dr["QTY"] = i.ToString();
dt.Rows.Add(dr);
}
string data = "<table border=1>";
data += "<TR>";
data += "<TD>Item</TD>";
data += "<TD>Description</TD>";
data += "<TD>QTY</TD>";
data += "</TR>";
foreach(DataRow dr1 in dt.Rows)
{
data += "<TR>";
data += "<TD>" + dr1["Item"].ToString() + "</TD>";
data += "<TD>" + dr1["Description"].ToString() + "</TD>";
data += "<TD>" + dr1["QTY"].ToString() + "</TD>";
data += "</TR>";
}
data += "</table>";
contents = contents.Replace("[ITEMS]", data);
You just need to replace "data" variable with [ITEMS] Tag.

Trouble with attaching gridview to email in DotNetNuke

I was looking around and found this. It is quite similar to what I want to accomplish the only problem is that when I run the script nothing happens unless I remove the { attachment } line. However, if I remove that line I do not get my attachment.
private void SendEmail()
{
StringBuilder message;
Attachment attachment;
string strOrderType;
if (lblRequestType.Text == "Order Request")
{
strOrderType = "Order Request";
}
else
{
strOrderType = "Return Request";
}
string strName = txtFname.Text + " " + txtLname.Text;
string strOrderNum = lblOrderNum.Text;
string strSubject = "Request:" + strOrderNum + " - " + strName + " " + strOrderType;
StringWriter writer = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
GridView gridView = new GridView();
gridView.DataSource = sdsOrders;
gridView.AutoGenerateColumns = true;
gridView.DataBind();
gridView.HeaderRow.Style.Add("background-color", "#003c74");
gridView.HeaderRow.Style.Add("color", "#ffffff");
for (int i = 0; i < gridView.Rows.Count; i++)
{
GridViewRow row = gridView.Rows[i];
//Change Color back to white
row.BackColor = System.Drawing.Color.White;
//Apply text style to each Row
row.Attributes.Add("class", "textmode");
//Apply style to Individual Cells of Alternating Row
if (i % 2 != 0)
{
row.BackColor = System.Drawing.Color.AliceBlue;
}
}
gridView.RenderControl(htmlWriter);
htmlWriter.Close();
System.Text.Encoding theEncoding = System.Text.Encoding.ASCII;
byte[] theByteArray = theEncoding.GetBytes(writer.ToString());
MemoryStream theMemoryStream = new MemoryStream(theByteArray, false);
//Visitor Message
if (txtEmail.Text.Length > 0)
{
message = new StringBuilder();
message.AppendLine("<h4 style=\"font-family: verdana, arial;\">Company Name</h4>");
message.AppendLine("<p style=\"font-size: small; font-family: verdana, arial;\">Dear " + txtFname.Text + ",</p>");
message.AppendLine("<p style=\"font-size: small; font-family: verdana, arial;\">Company Message.</p>");
message.AppendLine("Your Confirmation number is: " + strOrderNum);
attachment = new Attachment(theMemoryStream, strSubject);
Mail.SendMail("Address1#none.net", txtEmail.Text, "", "Address1#none.net",
"Address1#none.net", MailPriority.Normal, "Company Message", MailFormat.Html, Encoding.UTF8, message.ToString(),
new List<Attachment>() { attachment }, null, null, null, null, Host.EnableSMTPSSL);
}
//Internal Message
message = new StringBuilder();
message.AppendLine("<p style=\"font-size: small; font-family: verdana, arial;\">YAY it worked.</p>");
// need to create new reference to attachment, cannot use same reference to send email twice
attachment = new Attachment(theMemoryStream, strSubject);
Mail.SendMail("Address1#none.net", "Address1#none.net", "", "Address1#none.net",
"", MailPriority.Normal, strSubject, MailFormat.Html, Encoding.UTF8, message.ToString(),
new List<Attachment>() { attachment }, null, null, null, null, Host.EnableSMTPSSL);
}
Please tell me where I am going wrong here

Send inline image in email

Having an issue sending an image via email as an embedded image in the body.
The image file shows as an attachment which is ok but the inline image portion just shows as a red x.
Here is what I have so far
LinkedResource inline = new LinkedResource(filePath);
inline.ContentId = Guid.NewGuid().ToString();
MailMessage mail = new MailMessage();
Attachment att = new Attachment(filePath);
att.ContentDisposition.Inline = true;
mail.From = from_email;
mail.To.Add(data.email);
mail.Subject = "Client: " + data.client_id + " Has Sent You A Screenshot";
mail.Body = String.Format(
"<h3>Client: " + data.client_id + " Has Sent You A Screenshot</h3>" +
#"<img src=""cid:{0}"" />", inline.ContentId);
mail.IsBodyHtml = true;
mail.Attachments.Add(att);
Some minimal c# code to embed an image, can be:
MailMessage mailWithImg = GetMailWithImg();
MySMTPClient.Send(mailWithImg); //* Set up your SMTPClient before!
private MailMessage GetMailWithImg() {
MailMessage mail = new MailMessage();
mail.IsBodyHtml = true;
mail.AlternateViews.Add(GetEmbeddedImage("c:/image.png"));
mail.From = new MailAddress("yourAddress#yourDomain");
mail.To.Add("recipient#hisDomain");
mail.Subject = "yourSubject";
return mail;
}
private AlternateView GetEmbeddedImage(String filePath) {
LinkedResource res = new LinkedResource(filePath);
res.ContentId = Guid.NewGuid().ToString();
string htmlBody = #"<img src='cid:" + res.ContentId + #"'/>";
AlternateView alternateView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
alternateView.LinkedResources.Add(res);
return alternateView;
}
Try this
string htmlBody = "<html><body><h1>Picture</h1><br><img src=\"cid:filename\"></body></html>";
AlternateView avHtml = AlternateView.CreateAlternateViewFromString
(htmlBody, null, MediaTypeNames.Text.Html);
LinkedResource inline = new LinkedResource("filename.jpg", MediaTypeNames.Image.Jpeg);
inline.ContentId = Guid.NewGuid().ToString();
avHtml.LinkedResources.Add(inline);
MailMessage mail = new MailMessage();
mail.AlternateViews.Add(avHtml);
Attachment att = new Attachment(filePath);
att.ContentDisposition.Inline = true;
mail.From = from_email;
mail.To.Add(data.email);
mail.Subject = "Client: " + data.client_id + " Has Sent You A Screenshot";
mail.Body = String.Format(
"<h3>Client: " + data.client_id + " Has Sent You A Screenshot</h3>" +
#"<img src=""cid:{0}"" />", att.ContentId);
mail.IsBodyHtml = true;
mail.Attachments.Add(att);
protected void Page_Load(object sender, EventArgs e)
{
string Themessage = #"<html>
<body>
<table width=""100%"">
<tr>
<td style=""font-style:arial; color:maroon; font-weight:bold"">
Hi! <br>
<img src=cid:myImageID>
</td>
</tr>
</table>
</body>
</html>";
sendHtmlEmail("from#gmail.com", "tomailaccount", Themessage, "Scoutfoto", "Test HTML Email", "smtp.gmail.com", 25);
}
protected void sendHtmlEmail(string from_Email, string to_Email, string body, string from_Name, string Subject, string SMTP_IP, Int32 SMTP_Server_Port)
{
//create an instance of new mail message
MailMessage mail = new MailMessage();
//set the HTML format to true
mail.IsBodyHtml = true;
//create Alrternative HTML view
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
//Add Image
LinkedResource theEmailImage = new LinkedResource("E:\\IMG_3332.jpg");
theEmailImage.ContentId = "myImageID";
//Add the Image to the Alternate view
htmlView.LinkedResources.Add(theEmailImage);
//Add view to the Email Message
mail.AlternateViews.Add(htmlView);
//set the "from email" address and specify a friendly 'from' name
mail.From = new MailAddress(from_Email, from_Name);
//set the "to" email address
mail.To.Add(to_Email);
//set the Email subject
mail.Subject = Subject;
//set the SMTP info
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("fromEmail#gmail.com", "fromEmail password");
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = cred;
//send the email
smtp.Send(mail);
}
In addition to the comments above, I have the following additional comments:
Do not mix Attachments and AlternativeView, use one or the other. If you mix them, the inline attachments will be rendered as unknown downloads.
While Outlook and Google allow standard HTML-style "cid:att-001" this does NOT work on iPhone (late 2016 patch level), rather use pure alpha numeric "cid:att-001" -> "cid:att001"
As an aside: Outlook (even Office 2015) rendering (still the clear majority for business users) requires the use of TABLE TR TD style HTML, as it does not fully support the HTML box model.
An even more minimalistic example:
var linkedResource = new LinkedResource(#"C:\Image.jpg", MediaTypeNames.Image.Jpeg);
// My mail provider would not accept an email with only an image, adding hello so that the content looks less suspicious.
var htmlBody = $"hello<img src=\"cid:{linkedResource.ContentId}\"/>";
var alternateView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
alternateView.LinkedResources.Add(linkedResource);
var mailMessage = new MailMessage
{
From = new MailAddress("youremail#host.com"),
To = { "recipient#host.com" },
Subject = "yourSubject",
AlternateViews = { alternateView }
};
var smtpClient = new SmtpClient();
smtpClient.Send(mailMessage);
Try This.
protected void Page_Load(object sender, EventArgs e)
{
string Themessage = #"<html>
<body>
<table width=""100%"">
<tr>
<td style=""font-style:arial; color:maroon; font-weight:bold"">
Hi! <br>
<img src=cid:myImageID>
</td>
</tr>
</table>
</body>
</html>";
sendHtmlEmail("from#gmail.com", "tomailaccount", Themessage, "Scoutfoto", "Test HTML Email", "smtp.gmail.com", 25);
}
protected void sendHtmlEmail(string from_Email, string to_Email, string body, string from_Name, string Subject, string SMTP_IP, Int32 SMTP_Server_Port)
{
//create an instance of new mail message
MailMessage mail = new MailMessage();
//set the HTML format to true
mail.IsBodyHtml = true;
//create Alrternative HTML view
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
//Add Image
LinkedResource theEmailImage = new LinkedResource("E:\\IMG_3332.jpg");
theEmailImage.ContentId = "myImageID";
//Add the Image to the Alternate view
htmlView.LinkedResources.Add(theEmailImage);
//Add view to the Email Message
mail.AlternateViews.Add(htmlView);
//set the "from email" address and specify a friendly 'from' name
mail.From = new MailAddress(from_Email, from_Name);
//set the "to" email address
mail.To.Add(to_Email);
//set the Email subject
mail.Subject = Subject;
//set the SMTP info
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("fromEmail#gmail.com", "fromEmail password");
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = cred;
//send the email
smtp.Send(mail);
}
The other solution is attaching the image as attachment and then referencing it html code using cid.
HTML Code:
<html>
<head>
</head>
<body>
<img width=100 height=100 id=""1"" src=""cid:Logo.jpg"">
</body>
</html>
C# Code:
EmailMessage email = new EmailMessage(service);
email.Subject = "Email with Image";
email.Body = new MessageBody(BodyType.HTML, html);
email.ToRecipients.Add("abc#xyz.com");
string file = #"C:\Users\acv\Pictures\Logo.jpg";
email.Attachments.AddFileAttachment("Logo.jpg", file);
email.Attachments[0].IsInline = true;
email.Attachments[0].ContentId = "Logo.jpg";
email.SendAndSaveCopy();
Example on how to embed multiple images into an HTML email. Note, if you try to use newer HTML elements such as flexbox you will have trouble. If you use a simple table it will work. When I tried with flexbox the styling wasn't recognized.
public static void Main(string[] args)
{
var html = File.ReadAllText("c:\test.html");
var altView = GetAlternateView(html);
SendEmail(new List<MailRecipients> {new MailAddress(someone#email.com)}, "Hello!", "Images in Email Test!", true, altView);
}
public static bool SendEmail(List<MailAddress> recipients, string body, string subject, bool html, AlternateView view, MailPriority priority = MailPriority.Normal)
{
try
{
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
message.From = new MailAddress(ConfigLookup.GetStringValue(ConfigNames.EmailFrom));
recipients.ForEach(r => message.To.Add(new MailAddress(r.Address)));
message.Subject = subject;
message.IsBodyHtml = html;
message.AlternateViews.Add(view);
message.Priority = priority;
smtp.Port = 25;
smtp.Host = youremailhost;
smtp.EnableSsl = yes;
smtp.UseDefaultCredentials = false;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
return true;
}
catch (Exception e)
{
return false;
}
}
private AlternateView GetAlternateView(string html)
{
var logoPath = ConfigLookup.GetStringValue(ConfigNames.LogoPath);
LinkedResource logoResource = new LinkedResource(logoPath);
// ContentID must match the 'cid:contentId' in the html
logoResource.ContentId = "logo";
// Without a Mime type the images will show up as attached .bin files
logoResource.ContentType = new ContentType("image/png");
logoResource.TransferEncoding = TransferEncoding.Base64;
var heroPath = ConfigLookup.GetStringValue(ConfigNames.HeroPath);
LinkedResource heroResource = new LinkedResource(heroPath);
// ContentID must match the 'cid:contentId' in the html
heroResource.ContentId = "hero";
// Without a Mime type the images will show up as attached .bin files
heroResource.ContentType = new ContentType("image/png");
heroResource.TransferEncoding = TransferEncoding.Base64;
AlternateView alternateView = AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html);
alternateView.LinkedResources.Add(logoResource);
alternateView.LinkedResources.Add(heroResource);
return alternateView;
}
In your Html, the main thing that links up the images to the LinkedResource is the 'cid:contentId' must match:
<a href="https://somesite.com" target="_blank" style="color:#033254;">
<img src="cid:logo" width="218" alt="Company logo" style="max-width:100%;height:auto;" />
</a>
<a href="https://somesite.com" target="_blank" style="color:#033254;">
<img src="cid:hero" width="218" alt="Company hero" style="max-width:100%;height:auto;" />
</a>
Full HTML:
<!DOCTYPE html>
<html lang="en" xmlns="https://www.w3.org/1999/xhtml" xmlns:o="urn:schemas-microsoft-com:office:office">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="x-apple-disable-message-reformatting">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Shipping Confirmation from Products Company</title>
</head>
<body style="margin:0;padding:0;word-spacing:normal;">
<div role="article" aria-roledescription="email" lang="en" style="-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%;background-color:#ffffff;">
<table width="100%" align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" >
<tr>
<td align="center">
<div class="outer" style="width:96%;max-width:660px;margin:20px auto;background-color:#ffffff;">
<table role="presentation" width="100%" border="0" cellpadding="0" cellspacing="0">
<tr style="background-color:rgb(35, 68, 107);">
<td align="center" style="padding:20px 0;">
<a href="https://yahoo.com" target="_blank" style="color:#033254;">
<img src="cid:logo" width="218" alt="Products Company logo" style="max-width:100%;height:auto;" />
</a>
</td>
</tr>
<tr style="background-color:#ffffff;;">
<td align="center" style="padding:20px 0;">
<a href="https://yahoo.com" target="_blank" style="color:#033254;">
<img src="cid:hero" width="400" alt="Products Company hero" style="max-width:100%;height:auto;" />
</a>
</td>
</tr>
<tr style="background-color:rgb(35, 68, 107);">
<td style="padding:32px; text-align:center;">
<h1 style="margin-top:0;margin-bottom:16px;font-family:Arial,sans-serif;font-size:24px;line-height:30px;letter-spacing:1px;font-weight:bold;color:#ffffff;">
Shipping Confirmation
</h1>
<hr style="border:0;background-color:rgb(35, 68, 107);border-bottom:.5px solid #ffffff;width:66%;"/>
<h3 style="margin:16px 0;font-family:Arial,sans-serif;font-size:18px;line-height:24px;letter-spacing:1px;font-weight:bold;color:#ffffff;">Order# {0}</h3>
<h3 style="margin:16px 0;font-family:Arial,sans-serif;font-size:18px;line-height:24px;letter-spacing:1px;font-weight:bold;color:#ffffff;">{1}</h3>
<p style="margin:24px 0 8px;font-family:Arial,sans-serif;font-size:16px;line-height:22px;letter-spacing:.5px;color:#ffffff;">Thank you for your order!</p>
<p style="margin:8px 0 8px;font-family:Arial,sans-serif;font-size:16px;line-height:22px;letter-spacing:.5px;color:#ffffff;">Good news! It's on its way.</p>
<p style="margin:8px 0 8px;font-family:Arial,sans-serif;font-size:16px;line-height:22px;letter-spacing:.5px;color:#ffffff;">You can find the shipping details below.</p>
<h3 style="margin:16px 0;font-family:Arial,sans-serif;font-size:18px;line-height:24px;letter-spacing:1px;font-weight:bold;color:#ffffff;">
<a style="color:#ffffff;" href="https://www.ups.com/mobile/track?trackingNumber={2}">
[Track my package!]
</a>
</h3>
<p style="margin:32px 0 8px;font-family:Arial,sans-serif;font-size:16px;line-height:22px;letter-spacing:.5px;font-weight:bold;color:#ffffff;">Please contact Customer Service if you have any questions.</p>
<p style="margin:8px 0 8px;font-family:Arial,sans-serif;font-size:16px;line-height:22px;letter-spacing:.5px;color:#ffffff;">Customer Service:
<a style="color:#ffffff;" href="tel:18003665412">1-800-888-8888</a> ext. 8888
</p>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</div>
</body>
</html>
We all have our preferred coding styles. This is what I did:
var pictures = new[]
{
new { id = Guid.NewGuid(), type = "image/jpeg", tag = "justme", path = #"C:\Pictures\JustMe.jpg" },
new { id = Guid.NewGuid(), type = "image/jpeg", tag = "justme-bw", path = #"C:\Pictures\JustMe-BW.jpg" }
}.ToList();
var content = $#"
<style type=""text/css"">
body {{ font-family: Arial; font-size: 10pt; }}
</style>
<body>
<h4>{DateTime.Now:dddd, MMMM d, yyyy h:mm:ss tt}</h4>
<p>Some pictures</p>
<div>
<p>Color Picture</p>
<img src=cid:{{justme}} />
</div>
<div>
<p>Black and White Picture</p>
<img src=cid:{{justme-bw}} />
</div>
<div>
<p>Color Picture repeated</p>
<img src=cid:{{justme}} />
</div>
</body>
";
// Update content with picture guid
pictures.ForEach(p => content = content.Replace($"{{{p.tag}}}", $"{p.id}"));
// Create Alternate View
var view = AlternateView.CreateAlternateViewFromString(content, Encoding.UTF8, MediaTypeNames.Text.Html);
// Add the resources
pictures.ForEach(p => view.LinkedResources.Add(new LinkedResource(p.path, p.type) { ContentId = p.id.ToString() }));
using (var client = new SmtpClient()) // Set properties as needed or use config file
using (MailMessage message = new MailMessage()
{
IsBodyHtml = true,
BodyEncoding = Encoding.UTF8,
Subject = "Picture Email",
SubjectEncoding = Encoding.UTF8,
})
{
message.AlternateViews.Add(view);
message.From = new MailAddress("me#me.com");
message.To.Add(new MailAddress("you#you.com"));
client.Send(message);
}
You need to add the LinkedResource into an AlternateView
AlternateView alternateView = AlternateView.CreateAlternateViewFromString("<h3>Client: " + data.client_id + " Has Sent You A Screenshot</h3>" +
#"<img src=""cid:{0}"" />", null, "text/html");
alternateView.LinkedResources.Add(inline);
mail.AlternateViews.Add(alternateView);
MailMessage mail = new MailMessage();
//set the addresses
mail.From = new MailAddress("userid#gmail.com");
mail.To.Add("userid#gmail.com");
//set the content
mail.Subject = "Sucessfully Sent the HTML and Content of mail";
//first we create the Plain Text part
string plainText = "Non-HTML Plain Text Message for Non-HTML enable mode";
AlternateView plainView = AlternateView.CreateAlternateViewFromString(plainText, null, "text/plain");
XmlTextReader reader = new XmlTextReader(#"E:\HTMLPage.htm");
string[] address = new string[30];
string finalHtml = "";
var i = -1;
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{ // The node is an element.
if (reader.AttributeCount <= 1)
{
if (reader.Name == "img")
{
finalHtml += "<" + reader.Name;
while (reader.MoveToNextAttribute())
{
if (reader.Name == "src")
{
i++;
address[i] = reader.Value;
address[i] = address[i].Remove(0, 8);
finalHtml += " " + reader.Name + "=" + "cid:chartlogo" + i.ToString();
}
else
{
finalHtml += " " + reader.Name + "='" + reader.Value + "'";
}
}
finalHtml += ">";
}
else
{
finalHtml += "<" + reader.Name;
while (reader.MoveToNextAttribute())
{
finalHtml += " " + reader.Name + "='" + reader.Value + "'";
}
finalHtml += ">";
}
}
}
else if (reader.NodeType == XmlNodeType.Text)
{ //Display the text in each element.
finalHtml += reader.Value;
}
else if (reader.NodeType == XmlNodeType.EndElement)
{
//Display the end of the element.
finalHtml += "</" + reader.Name;
finalHtml += ">";
}
}
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(finalHtml, null, "text/html");
LinkedResource[] logo = new LinkedResource[i + 1];
for (int j = 0; j <= i; j++)
{
logo[j] = new LinkedResource(address[j]);
logo[j].ContentId = "chartlogo" + j;
htmlView.LinkedResources.Add(logo[j]);
}
mail.AlternateViews.Add(plainView);
mail.AlternateViews.Add(htmlView);
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new NetworkCredential(
"userid#gmail.com", "Password");
smtp.EnableSsl = true;
Console.WriteLine();
smtp.Send(mail);
}
I added the complete code below to display images in Gmail,Thunderbird and other email clients :
MailMessage mailWithImg = getMailWithImg();
MySMTPClient.Send(mailWithImg); //* Set up your SMTPClient before!
private MailMessage getMailWithImg()
{
MailMessage mail = new MailMessage();
mail.IsBodyHtml = true;
mail.AlternateViews.Add(getEmbeddedImage("c:/image.png"));
mail.From = new MailAddress("yourAddress#yourDomain");
mail.To.Add("recipient#hisDomain");
mail.Subject = "yourSubject";
return mail;
}
private AlternateView getEmbeddedImage(String filePath)
{
// below line was corrected to include the mediatype so it displays in all
// mail clients. previous solution only displays in Gmail the inline images
LinkedResource res = new LinkedResource(filePath, MediaTypeNames.Image.Jpeg);
res.ContentId = Guid.NewGuid().ToString();
string htmlBody = #"<img src='cid:" + res.ContentId + #"'/>";
AlternateView alternateView = AlternateView.CreateAlternateViewFromString(htmlBody,
null, MediaTypeNames.Text.Html);
alternateView.LinkedResources.Add(res);
return alternateView;
}
sending 2 images vb.net code convert for C# online converter.
Public Function SendEmail(Optional ByVal p_AsHTML As Boolean = False, Optional ByVal p_themEmail As String = "") As Boolean
Dim client As SmtpClient = New SmtpClient ''("FMSERVER.FMINNOVATIONS.COM.AU")
'Dim fromAddress As MailAddress = New MailAddress(Me.FromEmail, "WSMenterprise")
'Dim toAddress As MailAddress
Try
Dim aMessage As New MailMessage()
'(New MailAddress(Me.FromEmail, "WSMenterprise"), New MailAddress(anAdd))
If _fromAddress IsNot Nothing Then
If _fromName IsNot Nothing Then
aMessage.From = New MailAddress(_fromAddress, _fromName)
Else
aMessage.From = New MailAddress(_fromAddress)
End If
End If
For Each anAdd As String In _To
aMessage.To.Add(New MailAddress(anAdd))
Next
For Each cc As String In _CC
aMessage.CC.Add(New MailAddress(cc))
Next
For Each bcc As String In _BCC
aMessage.Bcc.Add(New MailAddress(bcc))
Next
aMessage.Subject = _Subject
aMessage.IsBodyHtml = p_AsHTML
If _EmailLogo Is Nothing Then
aMessage.Body = _Body
Else
If p_themEmail.ToString().ToLower.Contains("dexus") Then
Dim htmlView = AlternateView.CreateAlternateViewFromString(_Body.ToString(), Nothing, "text/html")
Dim logo As New LinkedResource(_EmailLogo)
logo.ContentId = "Dexuslogo1"
Dim logo1 As New LinkedResource(_EmailLogo1)
logo1.ContentId = "Dexuslogo2"
htmlView.LinkedResources.Add(logo)
htmlView.LinkedResources.Add(logo1)
aMessage.AlternateViews.Add(htmlView)
Else
Dim htmlView = AlternateView.CreateAlternateViewFromString(_Body.ToString(), Nothing, "text/html")
Dim logo As New LinkedResource(_EmailLogo)
logo.ContentId = "companylogo"
htmlView.LinkedResources.Add(logo)
aMessage.AlternateViews.Add(htmlView)
End If
End If
For Each anAttach As Attachment In _Attachments
aMessage.Attachments.Add(anAttach)
Next
If _ReplyTo IsNot Nothing Then aMessage.ReplyToList.Add(New MailAddress(_ReplyTo))
client.Host = "smtpi.cbre.com.au"
client.UseDefaultCredentials = True
client.Send(aMessage)
Catch exRecipUnk As SmtpFailedRecipientException
Return False
Catch exSmtp As SmtpException
''exSmtp.StatusCode
Return False
Catch ex As Exception
Return False
End Try
Return True
End Function
If p_Gmap_code = "DE" Then
Dim p_Theme As New Theme("Dexus")
Dim passwordlink As String = ""
Dim DexuslogoImage1 As String = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images\Dexus_Notice_Logo.png")
Dim DexuslogoImage2 As String = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images\DexusTenantNotice.png")
passwordlink = "<a href='" + p_Theme.TenantLoginPage + "?accesstype=email&te=" + a.Encrypt(p_TenantEmail) + "' target='_blank'>here.</a><br/>"
bodys += "<div align='Center'><table border='0' cellpadding='0' cellspacing='0'><tr style='height:50px;'><td width='623px' ></td><td valign='top' width='180'><p align='right'><a href='http://www.dexus.com/'><img border='0' height='50' src=cid:Dexuslogo1 width='174' alt=''/></a></p></td></tr><tr><td colspan='2' width='803' style='height:25px;'></td></tr> <tr><td width='623px'><p align='left' style='font-family:Arial;font-size:14pt;'><strong> Your Dexus Response Password is about to expire</strong></p></td>"
bodys += " <td width='180'><p align='right' style='font-family:Arial;font-size:10pt;'>" + DateTime.Now.ToString("dd/MM/yyyy") + " </p>"
bodys += "</td></tr><tr><td colspan='2' width='803' style='height:30px;'> </td></tr> <tr> <td colspan='2' width='803' style='font-family:Arial;font-size:10pt;'>"
bodys += "<p>" + wishes + " " + p_TenantName.Trim().ToString() + "</p>"
bodys += "</td></tr><tr><td colspan='2' width='803' style='height:25px;'></td> </tr><tr><td colspan='2' width='803' style='font-family:Arial;font-size:10pt;'>"
bodys += "Your Dexus Response password is about to expire in " + p_remaindays.ToString() + " days.<br /><br /> To reset your password and update your details, please click " + passwordlink.ToString() + "<br /><br />Please note that if you do not update your password by " + p_date + ",then your account will be set to inactive and you will not be able to access Dexus Response.</br></br>Please contact Dexus Response if you require assistance in accessing the portal.</p></td>" 'edit
bodys += " </tr><tr><td colspan='2' width='803' style='height:30px;'></td></tr><tr><td colspan='2' width='803'><table align='left' border='0' cellpadding='0' cellspacing='0'><tr><td width='802' style='font-family:Arial;font-size:10pt;'><p><strong>Dexus Response</strong></p></td></tr><tr><td width='802' style='font-family:Arial;font-size:10pt;'><p><a href='mailto:property.services#dexusfm.com'>property.services#dexusfm.com</a> <strong>|</strong> 1300 339 870 <strong>|</strong> <a href='https://response.dexus.com/'>response.dexus.com</a></p></td></tr></table></td></tr><tr><td colspan='2' width='803' style='height:15px;'></td></tr><tr> <td colspan='2' width='803'><p> </p><p><a href='https://response.dexus.com/' border='0' target='_blank'><img border='0' height='133'"
bodys += "src=cid:Dexuslogo2 alt='' width='800' /></a></p></td></tr><tr><td colspan='2' width='803' style='height:10px;'></td></tr><tr><td colspan='2' width='803' style='font-family:Arial;font-size:10pt;'><p><a href='http://www.dexus.com/who-we-are/terms-and-conditions' style=' color:#000000;'>Terms and Conditions</a><strong> | </strong><a href='http://www.dexus.com/who-we-are/privacy-policy' style=' color:#000000;'> Privacy Policy</a></p></td></tr><tr><td colspan='2' width='803' style='height:40px;'></td></tr><tr><td colspan='2' width='803'><p></p></td></tr><tr><td colspan='2' width='803' style='height:10px;'></td></tr><tr></tr><tr><td colspan='2' width='803' style='height:20px;'></td></tr></table></div>"
email = New Common.Email(emailHeading, bodys, p_Theme.EmailFrom, DexuslogoImage1, DexuslogoImage2)
email.ToEmail = p_TenantEmail
email.SendEmail(True, p_Theme.EmailFrom)

Categories

Resources