How to send HTML email with multiple Images - c#

i want to send html mail with multiple images. i am using webbrowsercontrol to embedded multiple images but when i send mail HTML email along with Image in HTML then I receive only html mail without Image.!

Try with LinkedResource Class to embed the image
using System.Net.Mail;
string messageHtml= #"<html><body> Your message text
<img src=""cid:12345"" />
<img src=""cid:123456"" /></body></html>";
AlternateView view= AlternateView.CreateAlternateViewFromString(messageHtml, null, MediaTypeNames.Text.Html);
LinkedResource pic= new LinkedResource("pics.jpg", MediaTypeNames.Image.Jpeg);
pic.ContentId = "12345";
LinkedResource pic2= new LinkedResource("pic2.jpg", MediaTypeNames.Image.Jpeg);
pic2.ContentId = "123456";
view.LinkedResources.Add(pic);
view.LinkedResources.Add(pic2);
MailMessage mail = new MailMessage();
mail.AlternateViews.Add(view);
mail.send();

Related

Attachment without filename

When using the System.Net.Mail namespace to send a e-mail with attachment to any Yahoo account the attachment is downloaded with 'untitled' name instead the file name.
In the Yahoo Mail interface the attachment looks with the correct name but when you download it the download name goes to 'untitled' for all attachments. The same e-mail message works fine with Gmail, Outlook.com, Windows Live Mail and other clients.
Looking the raw message it constains a content-type with name but without filename attribute. The Yahoo works fine if the filename attribute is set but C# library don't use this.
That's the header generated by C# for attachments:
Content-Type: application/octet-stream; name=test.pdf
That's the header that works with Yahoo:
Content-Type: application/octet-stream; name=file2; filename=test.pdf
Anyone get this problem so far? Is there a work arround for C# default mail sending?
using (var message = new MailMessage("from#domain", "to#yahoo.com.br", "Test with attachment", "Test with attachment"))
{
var attachment = new Attachment(#"c:\temp\test.pdf"); // Same result using stream instead path to file.
attachment.Name = "test.pdf"; // Same result without this line.
message.Attachments.Add(attachment);
using (var smtp = new SmtpClient("smtp.domain", 587))
{
smtp.Credentials = new NetworkCredential("from#domain", "password");
smtp.Send(message);
}
}
I found a solution:
attachment.ContentDisposition.FileName = "test.pdf";
This add the missing filename attribute in the raw e-mail message and solve the Yahoo limitation.
Have you tried explicitly specifying the content type?
var attachment = new Attachment(... , MediaTypeNames.Application.Octet);

SendGrid - Image not showing up in HTML email

I am using the SendGrid v3 API and C# library (v7) to send an email.
In my email I have a header which is a png. The header is embedded like this:
<img src="cid:emailheader"/>
In the C# code I send the image as an attachment with with the same ContentId
var mail = new Mail(from, subject, to, content);
var headerPath = HttpContext.Current.Server.MapPath("~/Resources/email-header.png");
var attachment = new SendGrid.Helpers.Mail.Attachment();
attachment.ContentId = "emailheader";
attachment.Content = Convert.ToBase64String(File.ReadAllBytes(headerPath));
attachment.Type = "image/png";
attachment.Filename = "email-header.png";
mail.AddAttachment(attachment);
var send = sg.client.mail.send.post(requestBody: mail.Get());
Yet when I open the email it says the source is not found, even though the image is correctly displayed in the attachment
I'm not the expert for Sendgrid, but I found on there blog post
that this suggest to do inline encoding in your html directly. this way you don't need to add an attachment. (I'm use this quite a lot)
<img alt="My Image" src="data:image/jpeg;base64,/9j/4S/+RXhpZgAATU0AKgA...more encoding" />
Maybe this is a work around for you.
As an second alternative:
for sending out emails with pictures I'm using
System.Net.Mail
here I do add an AlternateView with a linked resource.
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(html, null, "text/html");
LinkedResource imageResource = new LinkedResource(Imagepath + "Monitoring.png", "image/png")
{
ContentId = "1",
TransferEncoding = System.Net.Mime.TransferEncoding.Base64
};
htmlView.LinkedResources.Add(imageResource);
message.AlternateViews.Add(htmlView);
the syntax in html is the same as you use
<img src="cid:1">
I hope this help.
Butti
node
//imageData= "data:image/png;base64,ine793nfdsf......."
imageb64 = imageData.replace('data:image/png;base64,' , '');
//remove data:image/png;base64,
const msg = {
to: 'example#gmail.com',
from: 'test#gmail.com',
subject: "image attached",
html :'<img src="cid:myimagecid"/>',
attachments: [
{
filename: "imageattachment.png",
content: imageb64,
content_id: "myimagecid",
}
]
};
sgMail.send(msg);

Display email as html when client support it otherwise display as plain/text

I want to display email as html if client dont support for html it shud show mail as text/plain. I have written code but not sure can anybody check it how to check for multicontent and multi mimetype here.
mailMessage.IsBodyHtml = true;
mailMessage.From = new MailAddress(from);
foreach (string adress in to)
{
mailMessage.To.Add(adress);
}
string path = string.Empty;
var htmlView = AlternateView.CreateAlternateViewFromString(mailMessage.Body, null, "text/html");
ContentType mimeType = new System.Net.Mime.ContentType("text/html");
// Add the alternate body to the message.
AlternateView alternate = AlternateView.CreateAlternateViewFromString(mailMessage.Body, mimeType);
mailMessage.AlternateViews.Add(alternate);

Sending Image in email with text retrieved from DB

Currently we stored email text in db table and when we send an email, we query the db get the email text, do an HTML Encode and send email. But now we need to send Images in the email. What I did was stored the image in project file and stored the image location as tag in email text in db table. But its not working, any ideas on how should i do this. I need to insert image in middle of text. This is how we store html email in db table. As html is being parsed I copied it in comments section.
Pass your body to an html form and allowhtml(true) while sending email like this
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient();
SmtpServer.Host = Host;
mail.IsBodyHtml = true;
mail.From = new MailAddress(FromEmail);
mail.To.Add(ToEmail);
mail.Body = MailBody;
such that MailBody is String.Format("{0} with < img href='{1}' />",email,imagesrc);
{0} will be replaced by email
{1} will be replaced by imagesrc
you have to upload your picture to a webserver and set the src of the image to the absolute path!
<img src="http://www.myuploaded.com/image.jpg" />
if you cannot do that look at this post:
how to embed image in email

SMTP Permission Error

I'm using system.net.mail and have a textbox that users can enter their email address and a file gets attached and sent to them. When I test this in my custom box with Server 2008 I get the following error:
Request for the permission of type 'System.Net.Mail.SmtpPermission....at System.Security.CodeAccessSecurityEngine.Check
Do I have to configure something specifically on the server to allow? Or is it a code error?
string strto = txtTo.Text;
//create the mail message
MailMessage mail = new MailMessage();
//set the addresses
mail.From = new MailAddress("serveremail");
mail.To.Add(strto);
//set the content
mail.Subject = "subject";
//Get some binary data
byte[] byteArray = Encoding.ASCII.GetBytes(result);
//save the data to a memory stream
using (MemoryStream ms = new MemoryStream(byteArray))
//create the attachment from a stream. Be sure to name the data with a file and
//media type that is respective of the data
mail.Attachments.Add(new Attachment(ms, "test.txt", "text/plain"));
//send the message
SmtpClient smtp = new SmtpClient("server");
smtp.Send(mail);
This looks like it could be a permission issue when trying to access the attachment from the steam. What happens if you explicitly state credentials?

Categories