Below is a revised version of your function with several improvements and corrections. In particular, we now:
• Use floating‑point arithmetic when computing the size from the Base‑64 string so that integer division does not lose precision.
• Eliminate the duplicate check on the credential type (if you needed to check against two different AWS SES types, replace the duplicate with the proper second value).
• Make sure that file paths are mapped only once, and we check for file existence before reading.
• Wrap the non‑AWS SES branch’s MailMessage in a using‑block so that attachments and streams are disposed properly.
• Use more idiomatic C# naming (camel‑casing for locals) and null/empty checks.
Review and modify the code as needed (for example, if you have more than one AWS SES type to check) and according to your coding conventions:
private string GetAttachmentSize()
{
float totalSize = 0;
// Check if CredentialType is non‑empty and equals AWS SES.
// (Replace the second check with the appropriate enum value if needed.)
if (!string.IsNullOrEmpty(CredentialType) &&
(CredentialType == CVEnum.CredentialType.CredentialType_API_AWSSES /* ||
CredentialType == CVEnum.CredentialType.someOtherAWSSESType */))
{
// Calculate size for attachments stored in dtEmailAttachment
if (dtEmailAttachment != null && dtEmailAttachment.Rows.Count > 0)
{
foreach (DataRow dr in dtEmailAttachment.Rows)
{
// Get the attachment data
byte[] attachmentData = dr["Attachment"] as byte[];
if (attachmentData != null)
{
// Convert to Base64 and compute the original byte size.
// Force floating point division by using floats.
string base64 = Convert.ToBase64String(attachmentData);
totalSize += base64.Length * 3f / 4f;
}
}
}
// Calculate size for attachments provided by file paths
if (lstEmailAttachmentPath != null && lstEmailAttachmentPath.Count > 0)
{
foreach (var filePath in lstEmailAttachmentPath)
{
string mappedPath = HttpContext.Current.Server.MapPath(filePath);
if (File.Exists(mappedPath))
{
byte[] fileData = File.ReadAllBytes(mappedPath);
string base64 = Convert.ToBase64String(fileData);
totalSize += base64.Length * 3f / 4f;
}
}
}
}
else
{
// For non‑AWS SES attachments, use MailMessage to gather attachments
using (MailMessage mm = new MailMessage())
{
// Process file attachments from file paths
if (lstEmailAttachmentPath != null && lstEmailAttachmentPath.Any())
{
foreach (string attachmentPath in lstEmailAttachmentPath)
{
if (!string.IsNullOrWhiteSpace(attachmentPath))
{
string mappedPath = HttpContext.Current.Server.MapPath(attachmentPath);
if (File.Exists(mappedPath))
{
// The Attachment constructor that takes a file path reads the file for you.
mm.Attachments.Add(new Attachment(mappedPath));
}
}
}
}
// Process attachments provided as DataRows (byte array + filename)
if (dtEmailAttachment != null && dtEmailAttachment.Rows.Count > 0)
{
foreach (DataRow dr in dtEmailAttachment.Rows)
{
byte[] attachmentBytes = dr["Attachment"] as byte[];
if (attachmentBytes != null)
{
// Create a MemoryStream from the byte array.
MemoryStream ms = new MemoryStream(attachmentBytes);
string attachmentName = dr["AttachmentName"] != null
? dr["AttachmentName"].ToString()
: "Attachment";
mm.Attachments.Add(new Attachment(ms, attachmentName));
}
}
}
// Sum up the sizes of all attachments.
foreach (Attachment attachment in mm.Attachments)
{
if (attachment.ContentStream.CanSeek)
{
attachment.ContentStream.Position = 0;
}
totalSize += attachment.ContentStream.Length;
}
} // MailMessage and its attachments are disposed here.
}
// Convert total size from bytes to kilobytes.
float sizeInKB = totalSize / 1024f;
return sizeInKB.ToString();
}