如何更改电子邮件文件的扩展名?

发布于 2025-01-22 07:20:18 字数 4338 浏览 5 评论 0原文

我正在使用此课程发送带有PDF附件的电子邮件。该类具有以下代码:

using System.IO;
using System.Net;
using System.Net.Mail;
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.Web.WebDocumentViewer;
using DevExpress.XtraReports.Web.WebDocumentViewer.DataContracts;

namespace DocumentOperationServiceSample.Services
{
    public class CustomDocumentOperationService : DocumentOperationService {
        public override bool CanPerformOperation(DocumentOperationRequest request)
        {
            return true;
        }

        public override DocumentOperationResponse PerformOperation(DocumentOperationRequest request, PrintingSystemBase initialPrintingSystem, PrintingSystemBase printingSystemWithEditingFields)
        {
            using (var stream = new MemoryStream()) {
                printingSystemWithEditingFields.ExportToPdf(stream);
                stream.Position = 0;
                var mailAddress = new MailAddress(request.CustomData);
                var recipients = new MailAddressCollection() { mailAddress };
                var attachment = new Attachment(stream, System.Net.Mime.MediaTypeNames.Application.Pdf);
                return SendEmail(recipients, "Enter_Mail_Subject", "Enter_Message_Body", attachment);
            }
        }

        DocumentOperationResponse SendEmail(MailAddressCollection recipients, string subject, string messageBody, Attachment attachment) {
            string SmtpHost = null;
            int SmtpPort = -1;
            if (string.IsNullOrEmpty(SmtpHost) || SmtpPort == -1) {
                return new DocumentOperationResponse { Message = "Please configure the SMTP server settings." };
            }

            string SmtpUserName = "Enter_Sender_User_Account";
            string SmtpUserPassword = "Enter_Sender_Password";
            string SmtpFrom = "Enter_Sender_Address";
            string SmtpFromDisplayName = "Enter_Sender_Display_Name";
            using (var smtpClient = new SmtpClient(SmtpHost, SmtpPort))
            {
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtpClient.EnableSsl = true;

                if (!string.IsNullOrEmpty(SmtpUserName))
                {
                    smtpClient.Credentials = new NetworkCredential(SmtpUserName, SmtpUserPassword);
                }

                using (var message = new MailMessage())
                {
                    message.Subject = subject.Replace("\r", "").Replace("\n", "");
                    message.IsBodyHtml = true;
                    message.Body = messageBody;
                    message.From = new MailAddress(SmtpFrom, SmtpFromDisplayName);

                    foreach (var item in recipients)
                    {
                        message.To.Add(item);
                    }

                    try
                    {
                        if (attachment != null)
                        {
                            message.Attachments.Add(attachment);
                        }
                        smtpClient.Send(message);
                        return new DocumentOperationResponse
                        {
                            Succeeded = true,
                            Message = "Mail was sent successfully"
                        };
                    }
                    catch (SmtpException e)
                    {
                        return new DocumentOperationResponse
                        {
                            Message = "Sending an email message failed."
                        };
                    }
                    finally
                    {
                        message.Attachments.Clear();
                    }
                }
            }
        }

        protected string RemoveNewLineSymbols(string value)
        {
            return value;
        }
    }
} 

它可以正常工作,但是当我收到电子邮件时,它已将文档附加了一个名为application/pdf的文档。 我试图找出文档的名称来自何处。正如您在下图中看到的那样,当我添加应用程序类型PDF时,它看起来完全是我在电子邮件中附加的内容。

问题是无法使用任何PDF Viewer应用程序打开应用程序/PDF。我必须将文档重命名为application.pdf,以便能够打开它。有没有一种方法可以用使用application.pdf更改application/pdf

I am using this class to send an email with a PDF attachment. The class has the following code:

using System.IO;
using System.Net;
using System.Net.Mail;
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.Web.WebDocumentViewer;
using DevExpress.XtraReports.Web.WebDocumentViewer.DataContracts;

namespace DocumentOperationServiceSample.Services
{
    public class CustomDocumentOperationService : DocumentOperationService {
        public override bool CanPerformOperation(DocumentOperationRequest request)
        {
            return true;
        }

        public override DocumentOperationResponse PerformOperation(DocumentOperationRequest request, PrintingSystemBase initialPrintingSystem, PrintingSystemBase printingSystemWithEditingFields)
        {
            using (var stream = new MemoryStream()) {
                printingSystemWithEditingFields.ExportToPdf(stream);
                stream.Position = 0;
                var mailAddress = new MailAddress(request.CustomData);
                var recipients = new MailAddressCollection() { mailAddress };
                var attachment = new Attachment(stream, System.Net.Mime.MediaTypeNames.Application.Pdf);
                return SendEmail(recipients, "Enter_Mail_Subject", "Enter_Message_Body", attachment);
            }
        }

        DocumentOperationResponse SendEmail(MailAddressCollection recipients, string subject, string messageBody, Attachment attachment) {
            string SmtpHost = null;
            int SmtpPort = -1;
            if (string.IsNullOrEmpty(SmtpHost) || SmtpPort == -1) {
                return new DocumentOperationResponse { Message = "Please configure the SMTP server settings." };
            }

            string SmtpUserName = "Enter_Sender_User_Account";
            string SmtpUserPassword = "Enter_Sender_Password";
            string SmtpFrom = "Enter_Sender_Address";
            string SmtpFromDisplayName = "Enter_Sender_Display_Name";
            using (var smtpClient = new SmtpClient(SmtpHost, SmtpPort))
            {
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtpClient.EnableSsl = true;

                if (!string.IsNullOrEmpty(SmtpUserName))
                {
                    smtpClient.Credentials = new NetworkCredential(SmtpUserName, SmtpUserPassword);
                }

                using (var message = new MailMessage())
                {
                    message.Subject = subject.Replace("\r", "").Replace("\n", "");
                    message.IsBodyHtml = true;
                    message.Body = messageBody;
                    message.From = new MailAddress(SmtpFrom, SmtpFromDisplayName);

                    foreach (var item in recipients)
                    {
                        message.To.Add(item);
                    }

                    try
                    {
                        if (attachment != null)
                        {
                            message.Attachments.Add(attachment);
                        }
                        smtpClient.Send(message);
                        return new DocumentOperationResponse
                        {
                            Succeeded = true,
                            Message = "Mail was sent successfully"
                        };
                    }
                    catch (SmtpException e)
                    {
                        return new DocumentOperationResponse
                        {
                            Message = "Sending an email message failed."
                        };
                    }
                    finally
                    {
                        message.Attachments.Clear();
                    }
                }
            }
        }

        protected string RemoveNewLineSymbols(string value)
        {
            return value;
        }
    }
} 

It works fine, but when I receive the email it has attached an document named application/pdf.
I was trying to find out where the document's name comes from. As you can see in the below image, when I add the application type PDF, it appears exactly what I get attached in email.

enter image description here

The problem is that application/pdf cannot be opened with any PDF viewer app. I have to rename the document to application.pdf in order to be able to open it. Is there a way to change application/pdf with application.pdf?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一笑百媚生 2025-01-29 07:20:18

如果有人在寻找答案:

var attachment = new Attachment(stream, "report.pdf ", System.Net.Mime.MediaTypeNames.Application.Pdf);

If anyone is looking for the answer:

var attachment = new Attachment(stream, "report.pdf ", System.Net.Mime.MediaTypeNames.Application.Pdf);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文