I have a WinForms application where a user can configure email templates that will later get mail merged with client data from the database and then sent via SMTP.
I have everything working fine, but the issue comes when the user saves images inside the template body. I'm using a DevExpress RichEdit control to allow the users to create this email body, and that control converts it to HTML that I use to send. When images are there, it uses data-uri to embed the image directly into the HTML.
The problem now comes when I send the email in this way using data-uri images, not all clients render it properly. I just tested it in Outlook, and it doesn't seem to work. I know some web based email clients will work, but I need this to work all the time, at least for the widely used email clients.
I'm wondering if others have solved this problem and what might be the best solution here? I suppose I could send the images as attachments and then reference the attachment, although I'm not sure if this is a best practice. The other alternative though is to host each image on a server somewhere, then create a service which would have to store the images (like in a DB), and provide a way to query this image over the web using an ID. Then my emails would use an img src=site/getImg?imgID=xxx type thing. However, this seems like a lot of work to do what I want to accomplish and I'm hoping I could avoid it with something easier.
Thanks in advance for any advice!!
Adding images as attachments will work, but has the downside of a larger payload and higher bandwidth utilization both for you and for your recipients. The code for adding attachments in .net is pretty simple:
var message = new MailMessage();
foreach (string path in attachmentPaths)
{
if (File.Exists(path))
{
message.Attachments.Add(new Attachment(path));
}
}
On the other hand, referencing images stored on the web somewhere reduces the size of your emails and also opens up the possibility of gathering tracking data on clients that request images. Of course, most email clients will first ask the user whether they want to download images, so your tracking will only be rough.
you should be try save the images in the local disk , and the next include the who Embedded images.
// Add image attachment from local disk
Attachment oAttachment = oMail.AddAttachment( "d:\\test.gif" );
// Specifies the attachment as an embedded image
// contentid can be any string.
string contentID = "test001#host";
oAttachment.ContentID = contentID;
oMail.HtmlBody = "<html><body>this is a <img src=\"cid:"
+ contentID + "\"> embedded image.</body></html>";
Sorry for not explain, I speak only a little in english
Related
I'm currently sending reply emails via SMTP using SendGrid to emails I receive. I save the emails I get as .msg and load them as MimeMessage. I add some text as a reply but if the emails have images, the images don't seem to get passed through and I'm not sure exactly where they are. My goal is to send the reply emails with images being retained as currently they show as blank boxes in Outlook saying 'The linked image cannot be displayed. The file may have been moved, renamed, or deleted'.
From my research, I am seeing that images are passed in through attachments and the body of the email show the image as cid:image001.png... . I can't find where the actual images are stored though. When I look into the MimeMessage, attachment is null (unless I attach a file to the email, that file will show but not images within a body of email)
Does anyone have any tips for where to look for the image(s) of emails?
Update: I found the image stored in Body Parts. Will figure out how to pass that one to the reply next.
I managed to export multiple charts as pdf and download it on client side. Is there a way to email that pdf to email that user types. I was not able to find anything. I am using .net framework on server side.
Is it possible to send pdf to server using ajax and to send it to email from server that way?
Assuming that you've got the PDF content and the email address to send it to from the client side, you can send email via .NET from your back-end server. This used to be done via the built-in SmtpClient class but Microsoft now recommends people to use other options like MailKit instead.
You're also going to need an email server to connect to which will handle the delivery of those emails. This could be Gmail if you already have an account or an alternative like SendGrid
As far as getting the PDF content of the chart, Highcharts appears to be taking the SVG content of the chart and using svg2pdf to produce the PDF when not using an export server.
Since there appears to be no (documented) way of exporting to anything other than a file, you're likely going to need to mimic this process yourself by taking the SVG content via chart.getSVG() and then using svg2pdf to get your PDF content to send to your server.
Highcharts is a client html2print generator and like jsPDF or other client based solutions the PDF generation is per user selection of PDF printout options. (The client has control over PDF so no need for them to email it to self or others, unless they have to.)
Per link above
However, for situations where you may be generating a report for a client who has just seen the screen (and you don't need to change the chart in anyway) - why would you want to render the chart server side? when the client has already done the work.
To build server side automatically using a browser the server needs generally to process it, just like a user, with say chrome --headless options for html2pdf (and chrome can normally do that in one line, but with limited user layout options).
At that point you could have a PDF for attachment by any conventional mime application/pdf means (e.g. convert to base64.txt).
However due to the more dynamic drawing of svg (rather than a static insert) I could not run an Out.PDF --headless just by CLI, so it would need a driven solution such as selenium/puppeteer. THUS you may need to follow the suggestions in the lee-m above answer to build and modify the svg output.
for me I was able to run printEdge.vbs (with my current landscape defaults)
DIM shell
SET shell = WScript.CreateObject("WScript.Shell")
Shell.Run "cmd /c start msedge.exe file:///C:/Users/WDAGUtilityAccount/Documents/highchart.htm"
WScript.Sleep(4000)
Shell.SendKeys "^p"
WScript.Sleep(3000)
Shell.SendKeys "{enter}"
WScript.Sleep(1000)
Shell.SendKeys "{enter}"
WScript.Sleep(1000)
Shell.SendKeys "y"
WScript.Sleep(1000)
Shell.SendKeys "{esc}"
WScript.Sleep(1000)
Shell.SendKeys "%{f4}"
I am able to create an email draft using the gmail api. I'm also able to create a draft with an embedded image if the image is a real file.
The next step I want to reach is to create a draft with an embedded image where the image is just a screenshot on my clipboard. By creating the draft my html img tag looks something like
" img width="593" height="363" src="data:image/jpg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAMCAgMCAgMDAwM......"
but in gmail this part of my draft is empty.
Does someone know any solutions for this ?
Thanks a lot !
I have an Outlook addin that handles attachments by sending them to our server and then embedding a link into the body of the email containing the URL to the uploaded attachment.
This works but it is not 100% satisfactory because the URLs can get broken depending in the email client and the sender will not see their attachments listed at the top of there email where they are use to seeing them.
Ideally what I would like to do is exactly what is being done for OneDrive.
Currently if I add an attachment and provide a URL as the source it will download the file and send it as a normal attachment which is the exact opposite of what I want. I have noticed that if it fails to download the file it will then just send the URL as an attachment which is what I want. But sending broken URLs is not very useful.
Using
Outlook.MailItem.Attachments.Add("https://myserver.com/somefile", 7, 1, "MyAttachment")
If I set the attachment type to '7' which is what is used for OneDrive attachments I can get it to send the URL but it will first complain about not being able to set some access rights and if the sender clicks OK to continue anyway the attachment will appear to the sender and the receiver as residing on OneDrive which it is not, but clicking the URL will download the file from my server as I want.
My question is: Is it possible to send a none OneDrive URL as an attachment?
(I expect the answer is no, but I am hoping some Outlook guru out there may know some trick to do this.)
My other alternative it to wrap my URLs in an small html file that is then sent as a normal attachment and when opened will provide the link to the actual file.
I am currently using a string to embed a base64 image in email via EWS but the embeded images is not showing up in my outlook client when i receive the email.
If i save the email source it is viewable in my browser.
static string str = #"<head><meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8"" /></head><body><img src=""data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="" /></body></html>";
Is it possible to embed images into a HTML email message in this method?
Best Regards
Chris
Data URI scheme may be not supported by the receiving party:
What is Data URI support like in major email client software?
Consider using MIME related (cid protocol) to send html email with embedded images. I believe it is far better supported. I'm not sure if it can be easily done with SmtpMail.