Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Long Path Exception when adding attachments

Long Path Exception when adding attachments

Scheduled Pinned Locked Moved C#
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NJdotnetdev
    wrote on last edited by
    #1

    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

    F N 2 Replies Last reply
    0
    • N NJdotnetdev

      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

      F Offline
      F Offline
      F ES Sitecore
      wrote on last edited by
      #2

      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");

      1 Reply Last reply
      0
      • N NJdotnetdev

        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

        N Offline
        N Offline
        NJdotnetdev
        wrote on last edited by
        #3

        Can someone please advise? Really appreciate your time and help.

        L 1 Reply Last reply
        0
        • N NJdotnetdev

          Can someone please advise? Really appreciate your time and help.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Did you try the suggestion that was already made?

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

          N 1 Reply Last reply
          0
          • L Lost User

            Did you try the suggestion that was already made?

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

            N Offline
            N Offline
            NJdotnetdev
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups