Adding a picture header to an HTML email - c#

I'm trying to send an automated email from an application.
The main problem I'm having is that the picture doesn't display.
Secondary to that, the actual resource I'm supposed to be using is a PSD file I can only access with a URL.
The attach code is in from previous experiments, it attaches the file I select from the dialog in line 3
So can anyone tell me where I'm going wrong?
try{
OpenFileDialog diag = new OpenFileDialog();
diag.ShowDialog();
FileInfo inf = new FileInfo(diag.FileName);
string htmlBody = "<html><header><h1>TestPic</h1><br><img src=\"cid:filename\"></header></html>";
AlternateView avHtml = AlternateView.CreateAlternateViewFromString
(htmlBody, null, MediaTypeNames.Text.Html);
LinkedResource inline = new LinkedResource(#inf.FullName, MediaTypeNames.Image.Jpeg);
inline.ContentId = Guid.NewGuid().ToString();
inline.ContentLink = new Uri("PSD_Url.com");
avHtml.LinkedResources.Add(inline);
MailMessage mail = new MailMessage();
mail.AlternateViews.Add(avHtml);
Attachment att = new Attachment(inf.FullName);
att.ContentDisposition.Inline = true;
mail.From = new MailAddress("from#gmail.com");
mail.To.Add("to#gmail.com");
mail.Subject = "Test Email";
mail.Body = String.Format(
"<h3>TEST Has Sent You A Mail</h3>" +
#"<img src=""cid:{0}"" />", inline.ContentId);
mail.IsBodyHtml = true;
mail.Attachments.Add(att);
mail.BodyEncoding = Encoding.Unicode;
mail.HeadersEncoding = Encoding.Unicode;
mail.SubjectEncoding = Encoding.Unicode;
SmtpClient smp = new SmtpClient("mailhost.com", 80);
smp.Credentials = new System.Net.NetworkCredential(#"from#gmail.com", #"password");
smp.DeliveryFormat = SmtpDeliveryFormat.International;
smp.Send(mail);
}
catch(SmtpException sEx)
{
throw sEx;
}

.PSD files will not render in most email clients. They are limited to .JPG, .PNG and .GIF. In your case, you might want to run your .PSD through imagemagick or some image converter to convert it first.
Besides that, make sure the image URL is absolute and hosted online and it should work.

Related

specify relative path for an image when sending an email from C# desktop App

In a C# desktop App I send a mail to a specific user and it has an image and the end of the body as shown below
MailMessage email = new MailMessage();
email.To.Add(new MailAddress(correo));
email.From = new MailAddress("email#email.com");
email.Subject = "some text ( " + DateTime.Now.ToString("dd / MMM / yyy hh:mm:ss") + " ) ";
email.Body = "some text";
email.IsBodyHtml = true;
email.Priority = MailPriority.Normal;
string text = "some text";
AlternateView plainView = AlternateView.CreateAlternateViewFromString(text, Encoding.UTF8, MediaTypeNames.Text.Plain);
LinkedResource LinkedImage = new LinkedResource(#"C:\Users\myUser\Documents\App\CPresentacion\Resources\comunicaciones.jpg");
LinkedImage.ContentId = "ImagenGCI";
LinkedImage.ContentType = new ContentType(MediaTypeNames.Image.Jpeg);
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(
"some text <br/><img src=cid:ImagenGCI>", Encoding.UTF8,
MediaTypeNames.Text.Html);
htmlView.LinkedResources.Add(LinkedImage);
email.AlternateViews.Add(htmlView);
email.AlternateViews.Add(plainView);
SmtpClient smtp = new SmtpClient();
smtp.Host = "192.xxx.x.xxx";
smtp.Port = 25;
smtp.EnableSsl = false;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("email#email.com", "");
string output = null;
try
{
smtp.Send(email);
email.Dispose();
output = "Correo electrónico fue enviado satisfactoriamente.";
CargarDGV();
Limpiar(this);
}
catch (Exception ex)
{
output = "Error enviando correo electrónico: " + ex.Message;
}
and it works fine when I send the email from my computer but it cannot find the image when another user in another computer tries to sen the email from the App, I know the problem is in the linkedResource path but don´t know how to specify the path to work in another computer, this is my project' folders, could you please help to specify the correct path in order to work in any specific computer
With this line of code:
var path = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
You can get the directory path when your app is installed and form the proper route to your resources.

I want to email the screenshot in tabular format stored in my local using C#?

I want to automate my work by sending the screenshot taken during automation in tabular email format . I had successfully attached as document, but I want it in tabular format. This is code what I had already tried
public static void email_send() {
string htmlBody = "<html><body><h1>Picture</h1><br><img src=\"cid:test1.jpeg\"></body></html>";
AlternateView avHtml = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
LinkedResource inline = new LinkedResource("test1.jpeg", MediaTypeNames.Image.Jpeg);
inline.ContentId = Guid.NewGuid().ToString();
avHtml.LinkedResources.Add(inline);
Attachment att = new Attachment(#"D:/test123.png");
att.ContentDisposition.Inline = true;
MailMessage mail = new MailMessage();
mail.AlternateViews.Add(avHtml);
System.Net.Mail.SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("*****");
mail.To.Add("*******");
mail.Subject = "Test Mail - 1";
mail.Body = String.Format(
"<h3>Client: " + " Has Sent You A Screenshot</h3>" +
#"<img src=""cid:{0}"" />", inline.ContentId);
mail.IsBodyHtml = true;
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("******", "******");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
You have missed the attachment.
Your linked resource also needs to be attached to the email. You need to add the following code:
mail.Attachments.Add(att);

Sending email with html template with images to gmail,exchange

I am using the attached code to send email and images. It is working with gmail.
Now, I want to send to email using html templates. How do I use the attached code to do it?
private void SendEmail1()
{
string strMailContent = "Welcome new user";
string fromAddress = "xxx";
string toAddress = "xxx";
string contentId = "image1";
string path = Server.MapPath(#"Images/ml2.png"); // my logo is placed in images folder
MailMessage mailMessage = new MailMessage(fromAddress, toAddress);
mailMessage.Subject = "Welcome new User";
LinkedResource logo = new LinkedResource(path,"image/png");
logo.ContentId = "companylogo";
// done HTML formatting in the next line to display my logo
AlternateView av1 = AlternateView.CreateAlternateViewFromString("<html><body><img src=cid:companylogo/><br></body></html>" + strMailContent, null, "text/html");
av1.LinkedResources.Add(logo);
mailMessage.AlternateViews.Add(av1);
mailMessage.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["Host"];
smtp.UseDefaultCredentials = true;
smtp.Port = int.Parse(ConfigurationManager.AppSettings["Port"]);
smtp.Send(mailMessage);
}
Looks like your html isn't formatted correctly
AlternateView av1 = AlternateView.CreateAlternateViewFromString("<html><body><img src=cid:companylogo/><br></body></html>" + strMailContent, null, "text/html");
Should be
AlternateView av1 = AlternateView.CreateAlternateViewFromString($"<html><body><img src=cid:companylogo/><br>{strMailContent}</body></html>", "text/html");
Looks like your html isn't formatted correctly and you're passing in a null encoding.
AlternateView av1 = AlternateView.CreateAlternateViewFromString("<html><body><img src=cid:companylogo/><br></body></html>" + strMailContent, null, "text/html");
Should be
AlternateView av1 = AlternateView.CreateAlternateViewFromString($"<html><body><img src=cid:companylogo/><br>{strMailContent}</body></html>", "text/html");

Image path in Xamarin.Android

I want to send a mail in xamarin.android.I can send as text with the code below without image but the problem occurs when i try to send with an image as logo. I mean, i can't find the path of the image.
My sending mail method is:
public static void SendMail(List<string> to, List<string> cc, string subject, string body,string mfrom)
{
string messageHeader = "Android E-Mail Testi";
MailMessage msg = new MailMessage();
msg.From = new MailAddress(mfrom);
msg.To.Add(new MailAddress(to[0]));
msg.CC.Add(new MailAddress(cc[0]));
var inlineLogo = new LinkedResource("Drawable://logo.png");//This path is not working.
inlineLogo.ContentId = Guid.NewGuid().ToString();
msg.Body = string.Format(#"<img class=""img-responsive"" src=""cid:{0}"" style=""width:25%; float:left""/>
<br/><br/><h3>" + messageHeader + #"</h3>" + body, inlineLogo.ContentId);
var view = AlternateView.CreateAlternateViewFromString(msg.Body, null, "text/html");
view.LinkedResources.Add(inlineLogo);
msg.AlternateViews.Add(view);
msg.IsBodyHtml = true;
msg.Subject = subject;
msg.SubjectEncoding = Encoding.UTF8;
SmtpClient smtpClient = new SmtpClient("xx.xxx.x.xxx");
msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
List<MemoryStream> mStreams = null;
msg.BodyEncoding = Encoding.Unicode;
smtpClient.Send(msg);
if (mStreams != null)
foreach (MemoryStream mStream in mStreams)
mStream.Close();
}
I can send mail by using this code in asp.net.Only difference is file path. I used it like that and it works for web development:
var inlineLogo = new LinkedResource(HostingEnvironment.MapPath("~/images/logo.png"));
Which method should i use for image(logo.png) path in xamarin?
var inlineLogo = new LinkedResource(???);
I don't know the real path.How can i reach the path of the image in the drawable folder of the Resources folder?
Directory is:
Maybe it will help you. I created ToolbarItem with icon from the same folder Resources/drawable in such way:
new ToolbarItem("ToolbarItemName", "iconName.png", () => { //Some action; })

How to add images from resources folder as attachment and embed into outlook mail body in C#

I have a couple of images stored in visual studio project Resources folder, and I have to load them and display on the outlook mail body. Here it is the code:
Bitmap b = new Bitmap(Properties.Resources.MyImage);
ImageConverter ic = new ImageConverter();
Byte[] ba = (Byte[])ic.ConvertTo(b, typeof(Byte[]));
MemoryStream logo = new MemoryStream(ba);
LinkedResource companyImage = new LinkedResource(logo);
companyImage.ContentId = "companyLogo";
mailitem.HTMLBody += "<img src=\"cid:companyLogo\">";
However, it cannot display on the mail body but a ‘empty box with red x’. Can you give me some ideas?
Create an attachment and set the PR_ATTACH_CONTENT_ID property (DASL name "http://schemas.microsoft.com/mapi/proptag/0x3712001F") using Attachment.PropertyAccessor.SetProperty.
Your HTML body (MailItem.HTMLBody property) would then need to reference that image attachment through the cid:
<img src="cid:xyz">
where xyz is the value of the PR_ATTACH_CONTENT_ID property.
Look at an existing message with OutlookSpy (I am its author) - click IMessage button.
attachment = mailitem.Attachments.Add("c:\temp\MyPicture.jpg");
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "MyId1");
mailitem.HTMLBody = "<html><body>Test image <img src=""cid:MyId1""></body></html>";
Maybe you can 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);
var fileName = Guid.NewGuid.ToString();
var path = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)),
fileName);
File.WriteAllBytes(path, Properties.Resources.Pic);
LinkedResource inline = new LinkedResource(path, MediaTypeNames.Image.Jpeg); //Jpeg or something
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 = "Here is your subject;
mail.Body = String.Format(
"Here is the previous HTML Body" +
#"<img src=""cid:{0}"" />", inline.ContentId);
mail.IsBodyHtml = true;
mail.Attachments.Add(att);
No need to change your picture to memory stream
For easy use, please do your content of e-mail to be HTML
You can find in the code above
You can refer to this link too

Categories

Resources