Reputation: 18963
Is there any way to know the size of the attachment that is being sent in an email in asp.net c#?
thanks!
Upvotes: 4
Views: 5178
Reputation: 76
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);
Upvotes: 0
Reputation: 7214
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.
Upvotes: 1
Reputation: 218798
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.
Upvotes: 7