在asp.net中获取电子邮件附件大小

发布于 2024-10-21 00:05:20 字数 54 浏览 3 评论 0 原文

有没有办法知道在 asp.net c# 中电子邮件中发送的附件的大小?

谢谢!

Is there any way to know the size of the attachment that is being sent in an email in asp.net c#?

thanks!

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

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

发布评论

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

评论(4

红墙和绿瓦 2024-10-28 00:05:20

如果您使用 System.Net.Mail 并通过 Attachment 类,那么每个附件应该有一个 ContentStream 属性包含实际文件。此属性的类型为 Stream,有一个 Length 属性(类型为 long),它给出文件的大小(以字节为单位)。

If you're using System.Net.Mail and attaching the file via the Attachment class, then each attachment should have a ContentStream property containing the actual file. This property, of type Stream, has a Length property (of type long) which gives the size of the file in bytes.

枕头说它不想醒 2024-10-28 00:05:20

通常,附件在 System.Net.Mail 中采用 Base64 编码。 base64编码需要3个字节,并将它们转换为4个字节。

您需要做的是确定附件的长度(Stream.Length、byte[] 长度或文件长度),并将其除以 0.75。

这将为您提供附件的大小,为 SMTP 进行 base64 编码。

Typically attachments are base64 encoded in System.Net.Mail. base64 encoding takes 3 bytes, and converts them to 4 bytes.

What you need to do, is determine the length of the attachment (either as Stream.Length, the byte[] length, or the File length), and divide it by .75.

This will give you the size of the attachment, base64 encoded for SMTP.

疯了 2024-10-28 00:05:20

我相信你可以使用 HttpFileCollection 类。它使您可以访问客户端上传的所有文件。
这是 msdn 链接

I believe u can use HttpFileCollection class. It gives you access to all the files uploaded by client.
Here is the msdn link

挖鼻大婶 2024-10-28 00:05:20

附件.ContentDisposition.Size 将给出附件大小(以字节为单位)

string smtpClientHost = "mymailsender.example.com";
string emailAddressFrom = "[email protected]";

MailMessage mailMessage = new MailMessage {
  From = new MailAddress(strEmailAddressFrom)
};

mailMessage.To.Add(new MailAddress("[email protected]"));

mailMessage.Body = "Email body content here...";

mailMessage.Subject = "An important subject...";

foreach(var attachment in attachments)
    if (attachment.ContentDisposition.Size < ByteSize.FromMegaBytes(1).Bytes) 
        mailMessage.Attachments.Add(attachment);

SmtpClient smtpClient = new SmtpClient(strSmtpClientHost);

smtpClient.Send(mailMessage);

Attachment.ContentDisposition.Size will give the attachment size (in bytes)

string smtpClientHost = "mymailsender.example.com";
string emailAddressFrom = "[email protected]";

MailMessage mailMessage = new MailMessage {
  From = new MailAddress(strEmailAddressFrom)
};

mailMessage.To.Add(new MailAddress("[email protected]"));

mailMessage.Body = "Email body content here...";

mailMessage.Subject = "An important subject...";

foreach(var attachment in attachments)
    if (attachment.ContentDisposition.Size < ByteSize.FromMegaBytes(1).Bytes) 
        mailMessage.Attachments.Add(attachment);

SmtpClient smtpClient = new SmtpClient(strSmtpClientHost);

smtpClient.Send(mailMessage);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文