set image in email body using html c# - c#

I know that thats not a new question but i want to know that when i set image in my Email Body using the below C# code, why my image not show on mail
SmtpClient client = new SmtpClient();
MailMessage myMessage = new MailMessage();
String Body = "<img src=\"images/logo2.png\" style=\"width:75px; height:75px;\" />";
myMessage.To.Add(new MailAddress(txtemail.Text));
myMessage.Subject = "Subject";
myMessage.Body = Body;
myMessage.IsBodyHtml = true;
try
{
client.Send(myMessage);
}
catch (Exception ex)
{
Response.Write("Unable to send Email" + ex);
}
I am using asp.net c#.

Email will be opened in an email client and doesn't know which web-application to access the image. So your image src shouldn't be relative to the application. Change the src to include the full url:
<img src=\"http://www.somedomain.nl/images/logo2.png\"
Test the url in a browser by taking the src value and try browsing it. If it doesn't work, the src value isn't retrievable.

Related

System.Net.Mail.Attachment file url contains a # [duplicate]

In my view, users can search for a document and once they get the result, they can click on its id and they can download the document from specific url based on id: http://test.com/a.ashx?format=pdf&id={0}
For example, if the id is 10, then url to download document will be: http://test.com/a.ashx?format=pdf&id=10, and when user click on it they are able to download the document.
This is how it looks like in my view:
foreach (var item in Model)
{
<td>
<a href=#string.Format("http://test.com/a.ashx?format=pdf&id={0}",item.id)>
#Html.DisplayFor(modelItem => item.id)
</a>
</td>
}
And below is my controller action for SendEmail.
I am able to send email to the user. But i am having problem with sending attachments.
My question is: how can i attach the document that comes with the url to the email?
public static bool SendEmail(string SentTo, string Text, string cc)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("test#test.com");
msg.To.Add(SentTo);
msg.CC.Add(cc);
msg.Subject = "test";
msg.Body = Text;
msg.IsBodyHtml = true;
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(???);
msg.Attachments.Add(attachment);
SmtpClient client = new SmtpClient("mysmtp.test.com", 25);
client.UseDefaultCredentials = false;
client.EnableSsl = false;
client.Credentials = new NetworkCredential("test", "test");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
//client.EnableSsl = true;
try
{
client.Send(msg);
}
catch (Exception)
{
return false;
}
return true;
}
If the PDF file is generated on an external site, you will need to download it in some way, for this you can use WebClient:
var client = new WebClient();
// Download the PDF file from external site (pdfUrl)
// to your local file system (pdfLocalFileName)
client.DownloadFile(pdfUrl, pdfLocalFileName);
Then you can use it on Attachment:
attachment = new Attachment(pdfLocalFileName, MediaTypeNames.Application.Pdf);
msg.Attachments.Add(attachment)
Consider using Postal It's open source libriary for ASP.Net MVC allowing you to send emails very easy. For example to attach file you can use such code:
dynamic email = new Email("Example");
email.Attach(new Attachment("c:\\attachment.txt"));
email.Send();
Also you may want to use HangFire to send email in background, please take a look at the Sending Mail in Background with ASP.NET MVC
Update: In order to get the PDF file path you can use Server.MapPath method to convert virtual path to the corresponding physical directory on the server.

Email with image via external path C#

Here is a situation.
I am sending an email that has image as html body
<img src="http://www.google.com/images/srpr/logo11w.png" alt="click me" />
After receiving email image src is changed to
<img src="https://bay179.mail.live.com/Handlers/ImageProxy.mvc?bicild=&canary=ei12UmVJE9u9hgMk5TdV12Y1X%2b9Vc365IL%2bmULwd%2bfk%3d0&url=http%3a%2f%2fwww.google.com%2fimages%2fsrpr%2flogo11w.pngf" alt="click me">
Hence does not render image. Following is the code used to send email
SmtpClient sc = new SmtpClient("smtp.live.com");
sc.Port = 587;
sc.UseDefaultCredentials = false;
sc.Credentials = new NetworkCredential("someemail#hotmail.com", "password");
sc.EnableSsl = true;
MailMessage m = new MailMessage();
m.From = new MailAddress("someemail#hotmail.com");
m.Subject = "test subject";
m.IsBodyHtml = true;
m.Body = "<img src=\"http://www.google.com/images/srpr/logo11w.png\" alt=\"click me\" /><img src=\"https://campaign-uploads.s3.amazonaws.com/newsletter/2015/19Apr_wk17/EN/images/logo.jpg\" />";
m.To.Add(new MailAddress("someemail#hotmail.com"));
sc.Send(m);
First image url gets changed and second image's url remains same. So whats the science?
I don't want to embed image.
Once you send the e-mail you have yielded control to the receiving system. Most MTAs are going to do some kind of reformatting either due to anti-virus, phishing, etc. scans. Many will rework the img source to use their own proxies. You have no way to stop them. You just get to work with them.

Send email with attachment from a specific url in C#

In my view, users can search for a document and once they get the result, they can click on its id and they can download the document from specific url based on id: http://test.com/a.ashx?format=pdf&id={0}
For example, if the id is 10, then url to download document will be: http://test.com/a.ashx?format=pdf&id=10, and when user click on it they are able to download the document.
This is how it looks like in my view:
foreach (var item in Model)
{
<td>
<a href=#string.Format("http://test.com/a.ashx?format=pdf&id={0}",item.id)>
#Html.DisplayFor(modelItem => item.id)
</a>
</td>
}
And below is my controller action for SendEmail.
I am able to send email to the user. But i am having problem with sending attachments.
My question is: how can i attach the document that comes with the url to the email?
public static bool SendEmail(string SentTo, string Text, string cc)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("test#test.com");
msg.To.Add(SentTo);
msg.CC.Add(cc);
msg.Subject = "test";
msg.Body = Text;
msg.IsBodyHtml = true;
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(???);
msg.Attachments.Add(attachment);
SmtpClient client = new SmtpClient("mysmtp.test.com", 25);
client.UseDefaultCredentials = false;
client.EnableSsl = false;
client.Credentials = new NetworkCredential("test", "test");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
//client.EnableSsl = true;
try
{
client.Send(msg);
}
catch (Exception)
{
return false;
}
return true;
}
If the PDF file is generated on an external site, you will need to download it in some way, for this you can use WebClient:
var client = new WebClient();
// Download the PDF file from external site (pdfUrl)
// to your local file system (pdfLocalFileName)
client.DownloadFile(pdfUrl, pdfLocalFileName);
Then you can use it on Attachment:
attachment = new Attachment(pdfLocalFileName, MediaTypeNames.Application.Pdf);
msg.Attachments.Add(attachment)
Consider using Postal It's open source libriary for ASP.Net MVC allowing you to send emails very easy. For example to attach file you can use such code:
dynamic email = new Email("Example");
email.Attach(new Attachment("c:\\attachment.txt"));
email.Send();
Also you may want to use HangFire to send email in background, please take a look at the Sending Mail in Background with ASP.NET MVC
Update: In order to get the PDF file path you can use Server.MapPath method to convert virtual path to the corresponding physical directory on the server.

How to place a self created bitmap in the html body of a mail

to specify the question:
I'm creating a bitmap object, which I want to send with an email. I don't want to save it before or upload it to a webserver. Just attach it and then link the attachement in the html body of the mail.
I've searched quite a time now and all I can find are answers in which the picture is stored in the file system or on a server.
So is there any way to do this whithout saving the image before?
Thanks
Edit:
I've tried around a bit and finally came to this solution:
MailMessage mail = new MailMessage();
mail.To.Add(new MailAddress("xxx#yyy.de"));
mail.From = new MailAddress("xxx#yyy.de");
SmtpClient sender = new SmtpClient
{
Host = "smtp.client",
Port = 25
};
mail.Subject = "test";
body= "blablabla<br><img alt=\"\" hspace=0 src=\"cid:ImagedId\" align=baseline border=0 ><br>blablabla";
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
using (System.IO.MemoryStream image = new System.IO.MemoryStream())
{
Bitmap diagram = new Bitmap("C:\\qwer.bmp");
diagram.Save(image, System.Drawing.Imaging.ImageFormat.Jpeg);
LinkedResource resource = new LinkedResource(image, "image/jpeg");
resource.ContentId = "ImageId";
resource.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
htmlView.LinkedResources.Add(resource);
mail.AlternateViews.Add(htmlView);
sender.Send(mail);
}
But now my MailClient (Lotus Notes) doesnt open the mail with the error: "no mime data".
Any Idea how to solve this?
Try creating it in Word (either just the image or whole email) then drag over it and copy and paste into Outlook. I think that auto attaches images as well as embeds them.

How to send email in richtext format to Outlook?

It works great to send emails (to Outlook) in HTML format by assigning the text/html content type string like so:
using (MailMessage message = new MailMessage())
{
message.From = new MailAddress("--#---.com");
message.ReplyTo = new MailAddress("--#---.com");
message.To.Add(new MailAddress("---#---.com"));
message.Subject = "This subject";
message.Body = "This content is in plain text";
message.IsBodyHtml = false;
string bodyHtml = "<p>This is the HTML <strong>content</strong>.</p>";
using (AlternateView altView = AlternateView.CreateAlternateViewFromString(bodyHtml,
new ContentType(MediaTypeNames.Text.Html)))
{
message.AlternateViews.Add(altView);
SmtpClient smtp = new SmtpClient(smtpAddress);
smtp.Send(message);
}
}
The email is correctly recognized as HTML in Outlook (2003).
But if I try rich text:
MediaTypeNames.RichText;
Outlook doesn't detect this, it falls back to plain text.
How do I send email in rich text format?
The bottom line is, you can't do this easily using System.Net.Mail.
The rich text in Outlook is sent as a winmail.dat file in the SMTP world (outside of Exchange).
The winmail.dat file is a TNEF message. So, you would need to create your richtext inside of the winmail.dat file (formatted to TNEF rules).
However, that's not all. Outlook uses a special version of compressed RTF, so, you would also need to compress your RTF down, before it's added to the winmail.dat file.
The bottom line, is this is difficult to do, and unless the client really, really needs this functionality, I would rethink it.
This isn't something you can do with a few lines of code in .NET.
You can also achieve this by adding another alternate view before your calendar view as below:
var body = AlternateView.CreateAlternateViewFromString(bodyHtml, new System.Net.Mime.ContentType("text/html"));
mailMessage.AlternateViews.Add(body);
This worked for me..
public void sendUsersMail(string recipientMailId, string ccMailList, string body, string subject)
{
try
{
MailMessage Msg = new MailMessage();
Msg.From = new MailAddress("norepl#xyz.com", "Tracker Tool");
Msg.To.Add(recipientMailId);
if (ccMailList != "")
Msg.CC.Add(ccMailList);
Msg.Subject = subject;
var AltBody = AlternateView.CreateAlternateViewFromString(body, new System.Net.Mime.ContentType("text/html"));
Msg.AlternateViews.Add(AltBody);
Msg.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("mail.xyz.com");
smtp.Send(Msg);
smtp.Dispose();
}
catch (Exception ex)
{
}
}

Categories

Resources