Get email attachment size in asp.net
Is there any way to know the size of the attachment that is being sent in an email in asp.net c#?
开发者_如何学JAVAthanks!
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.
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.
I believe u can use HttpFileCollection class. It gives you access to all the files uploaded by client. Here is the msdn link
Attachment.ContentDisposition.Size
will give the attachment size (in bytes)
string smtpClientHost = "mymailsender.example.com";
string emailAddressFrom = "noreply@example.com";
MailMessage mailMessage = new MailMessage {
From = new MailAddress(strEmailAddressFrom)
};
mailMessage.To.Add(new MailAddress("examplerecipient@anotherdomain.com"));
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);
精彩评论