I want to send an email with some attached images. I also need to show these images in Email. How can I get the link of these attached email so that I can show them in email.
Note: I can also use remote images, but email servers like yahoo, gmail and hotmail will put them in spam.
Is this is possible in ASP.NET or ASP.NET MVC?
Take a look at this question with a solution using the AlternateView class:
sending mail along with embedded image using asp.net
string html = #"<html><body><img src=""cid:YourPictureId""></body></html>";
AlternateView altView = AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html);
LinkedResource yourPictureRes = new LinkedResource("yourPicture.jpg", MediaTypeNames.Image.Jpeg);
yourPictureRes.ContentId = "YourPictureId";
altView.LinkedResources.Add(yourPicture);
MailMessage mail = new MailMessage();
mail.AlternateViews.Add(altView);
Related
I am working on windows application for email sending.
For text formatting i used tinymce editor for email body.
Used tinymce insert image functionality for adding image in email body but when email is sent to user. Images does not appear in user email body.
Then i tried to add base64 image manually as below:
<img src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOYAAABLCAYAAABk6PuLAAAACXBIWXMAASdHAAEnRwEEDs /'>
Which is failed to load images.
Can we use linked resources and alternate view in tiny mce?
How to load images in email body?
Tiny MCE is just an HTML editor and not a tool which can be used for creating alternate views for email.
Moreover, all email clients don't support inline images (with data URL).
Alternate view is the only way to ensure that all email clients will be able to show your content in the intended manner.
Create a dictionary of linked resources:
Dictionary<string, byte[]> linkedResources = new Dictionary<string, byte[]>();
linkedResources.Add("img1", byte[]);
Create a common method to send email:
public bool SendEmail(Dictionary<string, byte[]> linkedResources)
{
using (SmtpClient mailSender = new SmtpClient("SmtpHost", 22))
{
MailMessage emailMessage = new MailMessage()
{
Subject = "Subject",
SubjectEncoding = Encoding.ASCII,
IsBodyHtml = true,
Body = "Message",
BodyEncoding = Encoding.ASCII,
From = new MailAddress("Sender#domain.com")
};
emailMessage.BodyEncoding = Encoding.ASCII;
emailMessage.IsBodyHtml = true;
AlternateView av = AlternateView.CreateAlternateViewFromString(emailMessage.Body, null, MediaTypeNames.Text.Html);
foreach (var item in linkedResources)
{
MemoryStream streamBitmap = new MemoryStream(item.Value);
var linkedResource = new LinkedResource(streamBitmap, MediaTypeNames.Image.Jpeg);
linkedResource.ContentId = item.Key;
av.LinkedResources.Add(linkedResource);
}
emailMessage.AlternateViews.Add(av);
mailSender.Send(emailMessage);
return true;
}
}
The real question you need to answer is "what is the best way to insert an image in an email". This is a very broad topic that has been answered many times - a little research should lead you to the most common approaches and their pros/cons:
https://sendgrid.com/blog/embedding-images-emails-facts/
https://www.campaignmonitor.com/blog/how-to/2008/08/embedding-images-in-email/
https://www.quora.com/Whats-the-best-practices-for-embedding-images-into-HTML-emails
there is no best solution for that, you can embed the images or attach them as files or just send them as links.
it depends on your requirements. it actually making image tags and link them to my server to get more analytics like the user had opened the email by loading the image throw a handler and the handler receive logs when the user opened the email and I can know how many users opened that email and so on.
Send grid is great for sending emails and getting analytics out of them because it tracks almost everything related to your email, and you should use Send grid to avoid Spam filters
In my experience the only way to solve this is to store that image on the server where it's publicly accessible and then point the img tag src attribute to that url. You have to understand that the HTML that's used in email is severely restricted to regular HTML.
In my WPFapplication I need to be able to open the default email to send an email with attachment previously scanned.
For the scanner i used WIA and I save the scanned image in jpeg.
For email i has tried the MAILTO as follows
string mail1 = "abc#hotmail.com";
string mail2 = "def#yahoo.it";
string attach = #"C:\bla\bla\bla\file.xlsx";
Process.Start(string.Format("mailto:{0}?subject={1}&body={2}&CC={3}&attachment:{4}"
, mail1, subject, body, mail2,attach));
every thing works unless ATTACHMENT when Process.start opens the default email.
you know something better than mailto? or something that gives the ability to attach files to email?
You can upload that file to a server, get a URL and include it into mailto body as a link.
I am using HigLabo library to get inbox messages from Gmail (imap) or Hotmail (pop3).
My Code is similar to this for gmail;
ImapClient client = new ImapClient(ServerName);
client.UserName = UserName;
client.Password = Password;
client.Port = Port;
client.Ssl = Ssl;
MailMessage mailMessage = client.GetMessage(1);
Console.WriteLine(mailMessage.BodyText);
lets assume this message is a HTML mail coming from newegg. So BodyText property has whole content as html but img elements coming as [image: ] because gmail and hotmail does not send images to my application. to see images user has to go their real inbox and click on "show all images" (which is not the case for my client app)
I am wondering is it a solid rule from mail providers to not to send images coming from "untrusted source" or is there a workaround to get also images and show inbox mails to user properly?
I am not sure what's wrong with all other API's lastly I gave a shot to this library today;
limilabs mail.dll
and it is fetching mails exactly how it appears on browsers.
I want send e-mail with some images in content. I think I must attached this images as attachments to e-mail, but my proble is how can I use attached images in mail content?
CODE WHICH SEND MAIL
WebMail.SmtpServer = "my.smtp.server";
WebMail.Send(
clientEmail,
subject,
"<html><head></head><body><img src='???' /></body></html>",
myEmail,
null,
new List<string> () { "path" },
true
);
What I must write as src ?
Any help would be appreciated.
Also good sample at http://blog.devexperience.net/en/12/Send_an_Email_in_CSharp_with_Inline_attachments.aspx
System.Net.Mail.Message Class :
Sample ;
var msg = new System.Net.Mail.Message();
msg.isHTML=true;
msg.Body ="<img src=\"cid:IMG1CONTENT\">";
Attachment mya = new Attachment("file-path-here");
mya.ContentId="IMG1CONTENT";
msg.Attachments.Add(mya);
You can find more details # http://msdn.microsoft.com/en-us/library/system.net.mail.attachment.aspx
To display the images in email content, one solution is to have an absolute URL. Upload the images to your server and use the abosoluter url as the src attribute value
<img src='http://www.yourdomain.com/youfolder/image.jpg' alt='your image desc'/>
But some email clients will disable external images. So user may need to enable "show images" when propmpted so.
I've just written a piece of code for a MVC website that sends a SMTP email using the .NET SmtpClient via our Exchange server. The email it sends has a HTML body with links to images and a file that are hosted on the website.
The email is sent fine when run internally on our network, but when its run from the hosted server that's not on our domin, the email comes through but the body is blank. Does anybody have any idea why? Is is because of the linked images or file that could be a potential threat and come from a server not on the domain and so is not trusted?
Here's the code that sends the email, it uses the MailDefinition class to insert a link to a file into the body that they've requested to download:
MailDefinition md = new MailDefinition();
md.From = "test#testing.com";
md.Subject = "Test Email";
md.IsBodyHtml = true;
ListDictionary replacements = new ListDictionary();
replacements.Add("REQUESTED_LINK", #"C:\MyFile.pdf");
MailMessage email = md.CreateMailMessage(mailTo, replacements, content, new System.Web.UI.Control());
SmtpClient emailClient = new SmtpClient();
emailClient.Host = "MyExchangeServer";
emailClient.Send(email);
My guess is Value for MailDefinition.BodyFileName missing from the code.
The name of the file that contains the message body text. The default is Empty.
On development or in internal sevrer BodyFileName has a some value. On hosted sevrer file is missing so email Body is empty.
see this Example for reference
Fixed, turns out the .html file containing the body of the email hadn't been deployed to the live server, and so the body of the email was blank as a result. Adding it fixed the problem, so it turns out it wasn't a security issue after all. Thanks for your help