Long Path Exception when adding attachments
-
I am getting a long path exception while adding attachments to MailMessage. Our file path exceeds 260, but that's how it is and we want it to work. Following is what I have done to avoid the exception:
private void CreateAttachments(MailMessage mailMessage, List filesToAdd)
{
foreach (string fileToAdd in filesToAdd)
{
SafeFileHandle fileHandle = null;
try
{
fileHandle = Common.CreateFile(Common.prefixPath(fileToAdd), Common.GENERIC_READ, FileShare.Read, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero);
if (!fileHandle.IsInvalid)
{
using (FileStream fs = new FileStream(fileHandle, FileAccess.Read))
{
Attachment data = new Attachment(fs, fileToAdd);
mailMessage.Attachments.Add(data);
}
}
}
finally
{
if ((fileHandle != null) || fileHandle.IsInvalid)
fileHandle.Dispose();
}
}
}Above LOC doesn't throw an exception while adding attachments. But, while sending(smtpClient.Send(mailMessage)) it throws an exception "Failure Sending Mail". UPDATED CODE
-
I am getting a long path exception while adding attachments to MailMessage. Our file path exceeds 260, but that's how it is and we want it to work. Following is what I have done to avoid the exception:
private void CreateAttachments(MailMessage mailMessage, List filesToAdd)
{
foreach (string fileToAdd in filesToAdd)
{
SafeFileHandle fileHandle = null;
try
{
fileHandle = Common.CreateFile(Common.prefixPath(fileToAdd), Common.GENERIC_READ, FileShare.Read, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero);
if (!fileHandle.IsInvalid)
{
using (FileStream fs = new FileStream(fileHandle, FileAccess.Read))
{
Attachment data = new Attachment(fs, fileToAdd);
mailMessage.Attachments.Add(data);
}
}
}
finally
{
if ((fileHandle != null) || fileHandle.IsInvalid)
fileHandle.Dispose();
}
}
}Above LOC doesn't throw an exception while adding attachments. But, while sending(smtpClient.Send(mailMessage)) it throws an exception "Failure Sending Mail". UPDATED CODE
In this line;
Attachment data = new Attachment(fs, fileToAdd, "text/plain");
fileToAdd doesn't have to be the path to the original file, it's just a token so you can drop the path
Attachment data = new Attachment(fs, Path.GetFileName(fileToAdd), "text/plain");
-
I am getting a long path exception while adding attachments to MailMessage. Our file path exceeds 260, but that's how it is and we want it to work. Following is what I have done to avoid the exception:
private void CreateAttachments(MailMessage mailMessage, List filesToAdd)
{
foreach (string fileToAdd in filesToAdd)
{
SafeFileHandle fileHandle = null;
try
{
fileHandle = Common.CreateFile(Common.prefixPath(fileToAdd), Common.GENERIC_READ, FileShare.Read, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero);
if (!fileHandle.IsInvalid)
{
using (FileStream fs = new FileStream(fileHandle, FileAccess.Read))
{
Attachment data = new Attachment(fs, fileToAdd);
mailMessage.Attachments.Add(data);
}
}
}
finally
{
if ((fileHandle != null) || fileHandle.IsInvalid)
fileHandle.Dispose();
}
}
}Above LOC doesn't throw an exception while adding attachments. But, while sending(smtpClient.Send(mailMessage)) it throws an exception "Failure Sending Mail". UPDATED CODE
Can someone please advise? Really appreciate your time and help.
-
Can someone please advise? Really appreciate your time and help.
-
Yes. I was able to fix this issue. The above code was correct, the only exception of "File was closed" was giving me trouble. Fixed it by keeping the file stream opened while sending(SMPTPClient.send()) the email. Thanks for the help.