【问题标题】:How do I rename file before sending如何在发送前重命名文件
【发布时间】:2021-08-30 02:35:52
【问题描述】:

我正在使用带有 c# 的 Mailkit 来发送带有附件的电子邮件。

如何在发送邮件前重命名附件?

我目前正在使用下面的代码,但在 IIS 中部署时会引发错误。


                var username = "username";
                var password = "password";
                var displayname = "display";

                var from = new MailboxAddress(displayname, username);
                var to = new MailboxAddress("User", emailto);

                msg.From.Add(from);
                msg.To.Add(to);

                msg.Subject = emailsubject;

                var attachment = new MimePart("application","zip")
                {
                    Content = new MimeContent(File.OpenRead(Path.Combine(fileutil.GetDir, "originalname.zip"))),
                    ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
                    ContentTransferEncoding = ContentEncoding.Base64,
                    FileName = "new filename.zip"
                };

                var msgbody = new BodyBuilder
                {
                    HtmlBody = string.Format(@"Message"),
                    TextBody = "Test Message!"
                };

                msgbody.Attachments.Add(attachment);
                msg.Body = msgbody.ToMessageBody();

                var client = new SmtpClient();
                client.Connect("smtp-mail.outlook.com", 587, SecureSocketOptions.StartTls);
                client.Authenticate(username, password);

                client.Send(msg);
                client.Disconnect(true);
                client.Dispose();

编辑:经过一番挖掘,我发现这是抛出的异常


The server's SSL certificate could not be validated for the following reasons:
• The server certificate has the following errors:
  • The revocation function was unable to check revocation for the certificate.
  • The revocation function was unable to check revocation because the revocation server was offline.
• An intermediate certificate has the following errors:
  • The revocation function was unable to check revocation for the certificate.
  • The revocation function was unable to check revocation because the revocation server was offline.```

【问题讨论】:

  • 你得到什么异常(和堆栈跟踪)?如果你保留原来的名字,它会抛出吗?
  • @Fildor 它抛出 500,An error occurred while attempting to establish an SSL or TLS connection
  • ^^ 无论您使用“新名称”还是“原始名称”?看起来问题完全出在其他地方:SSL/TLS。
  • @Fildor 它适用于我的本地 IIS,在放置 mimepart 之前也有效(重命名附件)
  • @Fildor 是的,如果我只使用msgbody.Attachments.Add("originalname.zip") 就可以,但这将不允许重命名文件

标签: c# .net-core razor-pages mailkit


【解决方案1】:

试试这个:

var client = new SmtpClient();
client.ServerCertificateValidationCallback = (o, c, ch, e) => true;
client.Connect("smtp-mail.outlook.com", 587, SecureSocketOptions.StartTls);
client.Authenticate(username, password);

【讨论】:

    猜你喜欢
    • 2023-04-05
    • 1970-01-01
    • 2014-06-04
    • 2011-11-13
    • 1970-01-01
    • 2013-11-24
    • 2019-04-24
    • 2012-01-04
    • 2010-11-15
    相关资源
    最近更新 更多