I have a C# app that generates emails using an HTML format (AlternateView) that contains an embedded image (jpeg).
The emails can be viewed correctly on Android, iOS, Outlook (2010) on Windows, but not on the native email client on Windows 8/8.1 Phone and Tablet (although it works find in Outlook running on the Win 8.1 tablet).
However the offending email client on the Win 8 phones/tablets can correctly see emails with embedded images sent from Outlook running on a PC.
The only thing I could find was to make sure the ContentType is set on the LinkedResource -which I do in the constructor:
LinkedResource pic1 = new LinkedResource(imgPath, MediaTypeNames.Image.Jpeg);
Code below.
Any suggestions?
string picId = "Pic1-" + Guid.NewGuid().ToString();
string htmlBody = "<html><body><h1>Test Message</h1><img src=\"cid:" + picId + "\"></body></html>";
AlternateView avHtml = AlternateView.CreateAlternateViewFromString
(htmlBody, null, MediaTypeNames.Text.Html);
// Create a LinkedResource object for each embedded image
LinkedResource pic1 = new LinkedResource(imgPath, MediaTypeNames.Image.Jpeg);
pic1.ContentId = picId;
avHtml.LinkedResources.Add(pic1);
// Add the alternate views instead of using MailMessage.Body
MailMessage m = new MailMessage();
m.AlternateViews.Add(avHtml);
// Address and send the message
m.From = new MailAddress(fromAddress, fromName);
m.To.Add(new MailAddress(toAddress));
m.Subject = subject;
SmtpClient client = new SmtpClient(AsbSmtpServer);
client.Send(m);
Related
I created a razor page that sends and email confirming to a group of users that a request has been made. When I look my inbox on the web or the windows email app, the mail looks jsut the way I want it. But the users use the outlook client to check on their emails and the mail looks way too different with the logo taking a whole table cell and the rest looks tiny. I use an alternate view to embbed the image but I don't know if the image is causin the problem or something else. Here's my code:
string contentID = Guid.NewGuid().ToString().Replace("-", "");
tab = tab.Replace("/img/logo500.png", "cid:" + contentID);
// tab = tab.Replace("<div id=\"divLinkAprob\"></div>", "APROBAR");
tab = tab.Replace("<div id=\"enviadoPor\"></div>", "Aprobado por: " + usuario);
tab += "Aprobado por: " + usuario;
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(tab, null, "text/html");
//path of image or stream
LinkedResource imagelink = new LinkedResource("wwwroot/img/logo500.png", "image/png");
imagelink.ContentId = contentID;
imagelink.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
htmlView.LinkedResources.Add(imagelink);
mail.AlternateViews.Add(htmlView);
Mail from microsoft mail app
Mail from outlook app 1
Mail from outlook app 2
In an ASP/C# application I'm sending an email with 3 file attached. The 3 files are the same type, same extension and more or less same size ( but none are empty ).
The email is sent correctly. If I open it using outlook, I have no problems. I can see the body, and the 3 files attached.
But here is my issue: If I send that mail to a Gmail Adress, then on Gmail I can see only 2 attachments.
And if I click on the download all attachment icon ( on the right ), it will download the 2 visible attachment + the third one but empty.
It's a really weird issue.
Also there is a 4th attachment which is an embedded image. And this image is display correctly in the mail body.
Here is the code I'm using to send the mail:
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("SMTP_IP_ADRESS", SMTP_IP_PORT);
mail.From = new MailAddress("MYEMAIL#DOMAIN.COM");
mail.To.Add("GMAIL_EMAIL");
mail.To.Add("OUTLOOK_EMAIL");
mail.Subject = "MSN "+Request.Params["nameMsn"];
Attachment imageAttachment = new
Attachment(Server.MapPath(Url.Content("~/Content/images/image.png")));
string contentID = "logoCare";
imageAttachment.ContentId = contentID;
mail.IsBodyHtml = true;
mail.Body = "<html><body>Have a good day.<br/>Best regards. <br/><br/><img width='200'
src=\"cid:"
+ contentID + "\"></body></html>";
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFileBase file = Request.Files[i];
var attachment = new Attachment(file.InputStream, file.FileName,
MediaTypeNames.Application.Octet);
mail.Attachments.Add(attachment);
}
mail.Attachments.Add(imageAttachment);
SmtpServer.Send(mail);
The third attachment you see empty can possibly be the CID embedded image that web based email clients (like Gmail) can't manage, meanwhile it works with desktop email clients like Outlook. Can you verify this?
Please take a look at here
I am making a windows form to capture image from laptop and send it to phone through email or whatsapp or any other social networking site. Though I got the capturing image code, I couldn't find the code for sending the image. Please help.
You can send email with image with this code:
using (var client = new SmtpClient())
{
MailMessage newMail = new MailMessage();
newMail.To.Add(new MailAddress("you#your.address"));
newMail.Subject = "Test Subject";
newMail.IsBodyHtml = true;
var inlineLogo = new LinkedResource(Server.MapPath("~/Path/To/YourImage.png"));
inlineLogo.ContentId = Guid.NewGuid().ToString();
string body = string.Format(#"
<img src=""cid:{0}"" />
", inlineLogo.ContentId);
var view = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
view.LinkedResources.Add(inlineLogo);
newMail.AlternateViews.Add(view);
client.Send(newMail);
}
https://stackoverflow.com/a/11000938/5364144
This question already has answers here:
Send inline image in email
(13 answers)
Closed 6 years ago.
I want to send an email with a logo, but the image is blank in inbox
Here is the code:
MailMessage msg = new MailMessage();
msg.From = new MailAddress(txtEmail.Text);
StringBuilder message = new StringBuilder();
message.AppendLine("<img src=#'images/logo.png' />");
message.AppendLine("<p>" + txtBody.Text + "</p>");
msg.Body = message.ToString();
msg.To.Add(txtTo.Text);
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(txtEmail.Text, txtPassword.Text);
client.Host = "Smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.Send(msg);
Problem is in the below line where you are providing a relative path for the image which is not going to work. You need to provide a absolute path for the image else your email client won't be able to download the image since it won't know from where to download.
message.AppendLine("<img src=#'images/logo.png' />");
Should be something like
message.AppendLine("<img src='www.somesitename.com/storage/logo.ong' />");
you are inserting the image using html image tag and you specified a path to it. What it means is that when the user opens up the mail the browser is going to try to find that image using the given relative path.
Assuming you are opening it up using gmail its going to try to look for the image under gmail.com/images/logo.png. Its either going to find gmail's logo or 404 not found
What you want to do is to include the full path of the image
I am building a windows 8 desktop app. I have an email form with a textbox and a button.
XAML below.
<TextBox x:Name="email_txt"></TextBox>
<Button x:Name="email_btn" Content="Emial Me" Click="email_btn_Click"/>
How do I send a an email with an attachment to the email address entered in email_txt when email_btn is clicked.
I used the following in the c# code behind the XAML page
private async void email_btn_Click(Object sender, RoutedEventArgs e)
{
var mailto = new Uri("mailto:?to=tr#gmail.com&subject=Hello&body=Test Tocuh Email");
await Windows.System.Launcher.LaunchUriAsync(mailto);*/
}
This code just opens MS Outlook with the message type.
How do I send an email with an attachment when the button is clicked?
Consider using SmtpClient class. Note that this class by default will take smtp configurations from you application config file.
var mailMessage = new MailMessage("test#example.com", email_txt.Text, "Hello", "Test");
mailMessage.IsBodyHtml = true;
byte[] attachmentData; //get attachment data as byte array.
var attachmentStream = new MemoryStream(attachmentData);
var attachment = new Attachment(attachmentStream, "Test");
mailMessage.Attachments.Add(attachment);
var smtpClient = new SmtpClient(smtpServer);
smtpClient.Send(mailMessage);
The WinRT (Win App) platform doesn't have any built-in SMTP client. Instead you can use the share charm in order to send emails.
See Sharing content source app sample.
How to share files
How to share text
How to share HTML
I would include some code, but I'm not on my Windows 8 machine.