简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM