Email on outlook looks different with image taking different dimentions - c#

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

Related

Sending email in Asp.net / C# with attachments - Issues with Gmail

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

How to create a content id image attachment for SendGrid in C#

I was unable to send email images, with Sendgird, to gmail using an inline/embedded method.
Here is my SO thread covering this. So I tried adding a cid attachment, but I'm not sure if I'm adding it right because the image isn't being shown in the email but being sent as an attachment at the bottom of the email.
So how do I attach the image as a cid?
Here is my code.
In the email I have this
<img alt="SpaceImage" title="Space Image" style="display: block" width="225" height="126" src="cid:spacethumbnail" />
Then in my c# I have this.
Attachment attachment = new Attachment();
attachment.ContentId = "spacethumbnail";
attachment.Content = Convert.ToBase64String(newEvent.SpaceThumbnail);
attachment.Type = "image/jpg";
attachment.Disposition = "inline"; // fixed the issue
attachment.Filename = "space-thumbnail.jpg";
and then I add this attachment to my send grid email like this
Mail mail = new Mail();
mail.Subject = message.Subject;
mail.From = new Email("Yoga#yoga.com");
mail.AddContent(new Content("text/HTML", message.Body));
foreach(Attachment attachment in attachments)
{
mail.AddAttachment(attachment);
}
// add multiple recipients if needed
Personalization personalization = new Personalization();
foreach (string emailAddress in message.Destination.Split(','))
{
personalization.AddTo(new Email(emailAddress));
}
mail.AddPersonalization(personalization);
dynamic response = sg.client.mail.send.post(requestBody: mail.Get());
How do I add the image as a cid attachment and not a regular attachment?
Since I don't like it when answers are in comments, for better visibility, I'm posting the answer from #user1186050 that seems to have solved the question.
In the C# file, adding: attachment.Disposition = "inline"; fixed the issue.

MailMessage with HTML embedded image not displaying in windows 8 email clients

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);

How can I send an email containing a link with parameter in C#?

I'm new in asp.net. I'm building a web-based application with C#, and I thought a lot about how to authenticate the user by his ID using the email.
I searched about it and I tried this code but it didn't worked because it sends a page rather than link with ID.
string URI = "http://localhost:49496/Activated.aspx";
string myParameters = "param1="+id.Text;
string HtmlResult = "";
using (WebClient wc = new WebClient())
{
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
HtmlResult = wc.UploadString(URI, myParameters);
}
MailMessage o = new MailMessage("f#hotmail.com", EMPemail, "KAUH Account Activation", "Hello, " + name + "<br /> Your KAUH Account about to activate click the link below to complete the activation process <br />" + HtmlResult);
NetworkCredential netCred = new NetworkCredential("f#hotmail.com", "______");
SmtpClient smtpobj = new SmtpClient("smtp.live.com", 587);
smtpobj.EnableSsl = true;
o.IsBodyHtml = true;
smtpobj.Credentials = netCred;
smtpobj.Send(o);
I need the ID in the activation page to change the "Activated" column from NO to YES.
Now my question: is the idea to authenticate the user by sending the ID secure?if it is,How can I perform it?
Best way to authenticate the user
You can easily create a 'secure' authentication by creating a hash, with for example MD5 or SHA1. The information you hash could be, for example, appending the e-mail and user ID as a string. You can save the hash inside the database for fast comparision (that you do when the user clicks the link with the hash you created). When the hash is inside the database, the authentication is complete.
You could also append the e-mail to the link, so you are 99.9999% sure. This is, however, not really needed as the end user does not know the ID.
Sending the page instead of the link issue
You are adding the HtmlResult that contains the content of the page you requested with wc.UploadString. Create the link inside the e-mail body manually using HTML:
MailMessage o = new MailMessage("f2#hotmail.com", EMPemail, "KAUH Account Activation", "Hello, " + name + "<br /> Your KAUH Account about to activate click the link below to complete the activation process <br />Click here");
Firstly I would definitely look at encrypting the ID that you will have in the URL. Just leaving the ID blank can leave you vulnerable to attacks.
Also if your body is html you should be able to add the link in the body with something similar:
string sURL= "http://localhost:49496/Activated.aspx?sID=" + Uri.EscapeDataString(id.Text);
then add the following to your HTML body:
<a style='text-decoration: none; font-weight: bold;' href='" + sURL+ "'>Click Here to Activate</a>
and to get the ID in the activation.aspx.cs page:
string sID = Uri.UnescapeDataString(Request.QueryString["sID"]);

.IsBodyHtml=true causes an empty email body to be sent, but .IsBodyHtml=false sends the body content

I have a WCF service that sends email based on user input. It was brought to my attention that, recently, a particular user's email were being delivered without any body text. If .IsBodyHtml is set to true, no body text is transferred; but, if .IsBodyHtml is set to false, the body has the appropriate text. However, it doesn't seem to be consistent, as it seems to occur only when said user's email address is set as the "From" address.Tech Details:We have an MS Exchange mail server. I'm composing a MailMessage object passing it to the built-in SMTP class to send the message.
The code has been simplified, a bit, for brevity/clarity. Nevertheless, the original code is pretty standard/straight-forward. email refers to a LINQ-to-SQL class object
MailMessage message = new MailMessage();
message.From = new MailAddress(email.fromAddress);
message.To.Add(email.toRecipient);
message.Subject = email.emailSubject;
//set email body
message.IsBodyHtml = true;
message.Body = email.emailBody;
Attachment attachmentFile = null;
if (email.hasAttachment == true)
{
//retrieve attachments for emailID
var attachments = from attach in db.EmailAttachments
where attach.emailID == emailID
select attach;
foreach (var attachment in attachments)
{//attach each attachment
string filePath = Path.Combine(UPLOAD_DIRECTORY, emailID.ToString(), attachment.fileName);
attachmentFile = new Attachment(filePath);
message.Attachments.Add(attachmentFile); //set attachment from input path
}
}
SmtpClient client = new SmtpClient(SMTP_SERVER, SMTP_PORT); //set SMTP server name/URL and port
client.Send(message); //try to send the SMTP email
Since the problem relates to a user, it is probably related to the setting of this user.
Log in as that user and open outlook
Select: File -> Options -> Mail
Scroll down to the section "Message Format"
Probably "Convert to PlainText Format is selected" change this to "Convert to HTML Format"

Categories