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

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

Related

Email on outlook looks different with image taking different dimentions

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

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.

.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"

send plain/text email and getting =0D=0A in email response from server

When i send a email from my site to Uniform http://co.za registrar and cc myself in the mail i get an email returned from them that they received the email, but cannot find some of the information as it has those funny characters added to it. I saw that there was a previous post on the site but none of the answers seem to work for me, and i tried alot of post and blog on google.
The entire body of the email is constructed of the template you get from the registrar site and i used etc as the detail i need to populate and replace those, then send the body of the email off to the registrar.
EX:
1a. Complete domain name: < domainname >
gets replaces with the domain name exmaple.co.za
My Code:
MailMessage emailmsg = new MailMessage("myemail#domain.co.za", "coza-admin#co.za");
emailmsg.Subject = "N domain domain.co.za";
emailmsg.Bcc.Add("myemail#domain.co.za");
emailmsg.Body = Body;
emailmsg.IsBodyHtml = false;
var plainView = AlternateView.CreateAlternateViewFromString(Body, new ContentType("text/plain; charset=iso-8859-1"));
plainView.TransferEncoding = TransferEncoding.SevenBit;
emailmsg.AlternateViews.Add(plainView);
SmtpClient sSmtp = new SmtpClient();
sSmtp.Send(emailmsg);
This code send a bcc copy to myself and from the looks of it through outlook all seems fine and i should be but when they receive it the characters gets added. it seems to randomly add those characters. then obviously the request fails as the seconds name server cannot be found.
Returned Email:
Administrative Info
Contact: Administrator=0D=0A 4b. Title/posi= (Administrator)
Company: CompanyName Ltd
Postal: P.O Box 12841, Centrahill, 6006
Phone: +27.00000000=0D=0A 4f. Fax Number: +27.00000000=
Provided Nameserver information
Primary Server : ns1.nameserver.co.za
Secondary 1 : ns2.nameserver.co.za=0d=0a,
Any help would be appreciated.
note: the registrar requires the the email be in plain text and not base64 or html whatsoever as their system picks up plain/text email and faxes and automatically processes them.
Thanks
I figured it out, changed my code to the following:
MailMessage emailmsg = new MailMessage("from#address.co.za", "to#address.co.za")
emailmsg.Subject = "Subject";
emailmsg.IsBodyHtml = false;
emailmsg.ReplyToList.Add("from#address.co.za");
emailmsg.BodyEncoding = System.Text.Encoding.UTF8;
emailmsg.HeadersEncoding = System.Text.Encoding.UTF8;
emailmsg.SubjectEncoding = System.Text.Encoding.UTF8;
emailmsg.Body = null;
var plainView = AlternateView.CreateAlternateViewFromString(EmailBody, emailmsg.BodyEncoding, "text/plain");
plainView.TransferEncoding = TransferEncoding.SevenBit;
emailmsg.AlternateViews.Add(plainView);
SmtpClient sSmtp = new SmtpClient();
sSmtp.Send(emailmsg);
All characters spaces are kept like they should be in the email template.
Hope this will help someone aswell.
=OD=OA is quoted-printable representation of CrLf.
If you want to use the iso-8859-1 characterset, then you will want to get rid of this line:
plainView.TransferEncoding = TransferEncoding.SevenBit;
If you insist on using 7 bit encoding, then you can only use ASCII characters in your email, so change the is0-8859-1 characterset to "us-ascii"

How to change email FROM address field?

I was trying to change FROM field with Name and Email address. But When I receive the email, Its shown as below
My network <commity#company.com [My network <commity#company.com]
Mycode is like below
const string from = "My network <commity#company.com>";
StringDictionary headers = new StringDictionary();
headers.Add("to", invitee.userEmail);
headers.Add("subject", ltEmailSubject.Text);
headers.Add("from", from);
headers.Add("content-type", MediaTypeNames.Text.Html);
_emailSuccessful = SPUtility.SendEmail(elevatedSite.RootWeb, headers,
emailBody + Environment.NewLine + "");
I want FROM email address should showup like below
My network [commity#company.com]
SPUtility.SendMail will always use the from smtp address as specified in Central Administration > Outgoing Email Settings and the friendly name is set to the title of the site.
Instead you can use (from http://www.systemnetmail.com/faq/3.2.1.aspx)
MailMessage mail = new MailMessage();
//set the addresses
//to specify a friendly 'from' name, we use a different ctor
mail.From = new MailAddress("me#mycompany.com", "Steve James" );
mail.To.Add("you#yourcompany.com");
//set the content
mail.Subject = "This is an email";
mail.Body = "this is the body content of the email.";
//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1");
smtp.Send(mail);
I know this thread is old, but in case someone comes across this as I did, your original code would have worked fine using SPUtility.SendMail if you added quotes around the friendly name in the from address like this:
const string from = "\"My network\" <commity#company.com>";
I believe if you look at the mail object in the .Net Framework, you could do what you wish to do.
Maybe this site could help you out further?

Categories