deleting email attachments

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?QW5kcmV3?=

    deleting email attachments

    Hi,

    I want to delete my email attachment after sending the email. How do I get
    around this issue ?

    I have set up a method to send email using SMTP.
    public bool CreateMessageWi thAttachment()
    {
    MailMessage message = new MailMessage();
    ....
    client = new SmtpClient(var. hostName, var.portNo);
    client.SendComp leted += new SendCompletedEv entHandler
    (SendCompletedC allback);

    string userState = "test message1";
    client.SendAsyn c(message, userState);
    }

    This method is called last after the email is actually sent. I can't put a
    Dispose() call in the above as I would get an error
    "System.Net.Mai l.SmtpException : Failure sending mail. --->
    System.ObjectDi sposedException : Cannot access a closed file.".

    public static void SendCompletedCa llback(object sender,
    AsyncCompletedE ventArgs e)
    {
    if (e.Error != null)
    {
    Logger.Write(St ring.Format("Pr ocessed: {0}", e.Error.ToStrin g()));
    }
    else
    {
    File.Delete(sAt t.Value);
    }
    }
    Thanks in advance

    regards,
    Andrew

  • Arial

    #2
    Re: deleting email attachments


    "Andrew" <Andrew@discuss ions.microsoft. comwrote in message
    news:86C61D51-E409-450D-A90C-23C4DDF3AC49@mi crosoft.com...
    Hi,
    >
    I want to delete my email attachment after sending the email. How do I get
    around this issue ?
    >
    I have set up a method to send email using SMTP.
    public bool CreateMessageWi thAttachment()
    {
    MailMessage message = new MailMessage();
    ....
    client = new SmtpClient(var. hostName, var.portNo);
    client.SendComp leted += new SendCompletedEv entHandler
    (SendCompletedC allback);
    >
    string userState = "test message1";
    client.SendAsyn c(message, userState);
    }
    >
    This method is called last after the email is actually sent. I can't put a
    Dispose() call in the above as I would get an error
    "System.Net.Mai l.SmtpException : Failure sending mail. --->
    System.ObjectDi sposedException : Cannot access a closed file.".
    >
    public static void SendCompletedCa llback(object sender,
    AsyncCompletedE ventArgs e)
    {
    if (e.Error != null)
    {
    Logger.Write(St ring.Format("Pr ocessed: {0}", e.Error.ToStrin g()));
    }
    else
    {
    File.Delete(sAt t.Value);
    }
    }
    Thanks in advance
    >
    regards,
    Andrew
    >
    you will have to call Dispose mannualy on attachment
    and mailMessage objects
    after mail has been sent

    it looks that attachments doesn't get
    disposed and garbage collected properly

    let us know if this worked ...


    Comment

    • Ignacio Machin ( .NET/ C# MVP )

      #3
      Re: deleting email attachments

      On Sep 24, 7:54 am, Andrew <And...@discuss ions.microsoft. comwrote:
      Hi,
      >
      I want to delete my email attachment after sending the email. How do I get
      around this issue ?
      >
      I have set up a method to send email using SMTP.
      public bool CreateMessageWi thAttachment()
      {
      MailMessage message = new MailMessage();
      ....
      client = new SmtpClient(var. hostName, var.portNo);
      client.SendComp leted += new SendCompletedEv entHandler
      (SendCompletedC allback);
      >
      string userState = "test message1";
      client.SendAsyn c(message, userState);
      >
      }
      >
      This method is called last after the email is actually sent. I can't put a
      Dispose() call in the above as I would get an error
      "System.Net.Mai l.SmtpException : Failure sending mail. --->
      System.ObjectDi sposedException : Cannot access a closed file.".
      Dispose of what?

      Also in your code below you did not include the part of the
      attachment.
      You are sending it async. so of course you cannot delete the file
      after the method ends. You could do it in the SendCompletedCa llback
      method though

      Comment

      • =?Utf-8?B?QW5kcmV3?=

        #4
        Re: deleting email attachments

        Thanks for your reply. However I am still having difficulties in accessing
        the MailMessage object to dispose of it.

        I've tried and got this error when using this code line in :

        public static void SendCompletedCa llback(object sender,
        AsyncCompletedE ventArgs e)
        {
        ((MailMessage)s ender).Dispose( );
        }

        {"The process cannot access the file 'C:\Project\Out put\testDOC.doc ' because
        it is being used by another process."}
        {"Exception has been thrown by the target of an invocation."}

        Any ideas ?

        Thanks in advance

        regards,
        Andrew


        "Arial" wrote:
        >
        "Andrew" <Andrew@discuss ions.microsoft. comwrote in message
        news:86C61D51-E409-450D-A90C-23C4DDF3AC49@mi crosoft.com...
        Hi,

        I want to delete my email attachment after sending the email. How do I get
        around this issue ?

        I have set up a method to send email using SMTP.
        public bool CreateMessageWi thAttachment()
        {
        MailMessage message = new MailMessage();
        ....
        client = new SmtpClient(var. hostName, var.portNo);
        client.SendComp leted += new SendCompletedEv entHandler
        (SendCompletedC allback);

        string userState = "test message1";
        client.SendAsyn c(message, userState);
        }

        This method is called last after the email is actually sent. I can't put a
        Dispose() call in the above as I would get an error
        "System.Net.Mai l.SmtpException : Failure sending mail. --->
        System.ObjectDi sposedException : Cannot access a closed file.".

        public static void SendCompletedCa llback(object sender,
        AsyncCompletedE ventArgs e)
        {
        if (e.Error != null)
        {
        Logger.Write(St ring.Format("Pr ocessed: {0}", e.Error.ToStrin g()));
        }
        else
        {
        File.Delete(sAt t.Value);
        }
        }
        Thanks in advance

        regards,
        Andrew
        >
        you will have to call Dispose mannualy on attachment
        and mailMessage objects
        after mail has been sent
        >
        it looks that attachments doesn't get
        disposed and garbage collected properly
        >
        let us know if this worked ...
        >
        >
        >

        Comment

        • =?Utf-8?B?QW5kcmV3?=

          #5
          Re: deleting email attachments

          Thanks for your reply. If I am understanding you correctly, I am actually
          doing that.

          public static void SendCompletedCa llback(object sender,
          AsyncCompletedE ventArgs e)
          {
          MailMessage MM = (MailMessage)se nder; //Error msg: Exception has been
          thrown by the target of an invocation.
          MM.Dispose();
          ..
          ..
          File.Delete(sAt t.Value);
          }



          Thanks in advance

          regards,
          Andrew


          Comment

          • =?Utf-8?B?QW5kcmV3?=

            #6
            Re: deleting email attachments

            I've traced the error to this line of code:

            client.SendAsyn c(mailMessage, userToken); //

            However in
            public static void SendCompletedCa llback(object sender,
            AsyncCompletedE ventArgs e)
            {
            SmtpClient s = (SmtpClient)sen der;
            }

            I would have thought that s would be of MailMessage class. Not of
            SmtpClient class ? How do I access MailMessage within this event ?

            Thanks in advance

            regards,
            Andrew


            "Andrew" wrote:
            Thanks for your reply. If I am understanding you correctly, I am actually
            doing that.
            >
            public static void SendCompletedCa llback(object sender,
            AsyncCompletedE ventArgs e)
            {
            MailMessage MM = (MailMessage)se nder; //Error msg: Exception has been
            thrown by the target of an invocation.
            MM.Dispose();
            ..
            ..
            File.Delete(sAt t.Value);
            }
            >
            >
            >
            Thanks in advance
            >
            regards,
            Andrew
            >
            >

            Comment

            • Ignacio Machin ( .NET/ C# MVP )

              #7
              Re: deleting email attachments

              On Sep 24, 10:47 am, Andrew <And...@discuss ions.microsoft. comwrote:
              Thanks for your reply. If I am understanding you correctly, I am actually
              doing that.
              >
              public static void SendCompletedCa llback(object sender,
              AsyncCompletedE ventArgs e)
              {
              MailMessage MM = (MailMessage)se nder; //Error msg: Exception has been
              thrown by the target of an invocation.
              MM.Dispose();
              ..
              ..
              File.Delete(sAt t.Value);
              }
              >
              Thanks in advance
              >
              regards,
              Andrew
              You cannot dispose the sender, think about it, the code that call your
              method is holding a reference to it.
              Also, check in the debigger the type of the Sender, you are blindly
              casting it to a MailMessage.

              Comment

              • Ignacio Machin ( .NET/ C# MVP )

                #8
                Re: deleting email attachments

                On Sep 24, 12:54 pm, Andrew <And...@discuss ions.microsoft. comwrote:
                I've traced the error to this line of code:
                >
                client.SendAsyn c(mailMessage, userToken); //
                >
                However in
                public static void SendCompletedCa llback(object sender,
                AsyncCompletedE ventArgs e)
                {
                SmtpClient s = (SmtpClient)sen der;
                }
                >
                I would have thought that s would be of MailMessage class. Not of
                SmtpClient class ? How do I access MailMessage within this event ?
                You can hold a reference to it.
                Or a reference to the file you just attached.

                Comment

                • =?Utf-8?B?QW5kcmV3?=

                  #9
                  Re: deleting email attachments

                  Thanks for your reply.

                  I send tis to :
                  //client.SendAsyn c(MailMessage message, object userToken)
                  client.SendAsyn c(message, userToken);

                  public static void SendCompletedCa llback(object sender,
                  AsyncCompletedE ventArgs e)
                  {
                  ///How do I access the MailMessage object within here ??
                  }



                  Thanks in advance

                  regards,
                  Andrew


                  "Ignacio Machin ( .NET/ C# MVP )" wrote:
                  On Sep 24, 10:47 am, Andrew <And...@discuss ions.microsoft. comwrote:
                  Thanks for your reply. If I am understanding you correctly, I am actually
                  doing that.

                  public static void SendCompletedCa llback(object sender,
                  AsyncCompletedE ventArgs e)
                  {
                  MailMessage MM = (MailMessage)se nder; //Error msg: Exception has been
                  thrown by the target of an invocation.
                  MM.Dispose();
                  ..
                  ..
                  File.Delete(sAt t.Value);
                  }

                  Thanks in advance

                  regards,
                  Andrew
                  >
                  You cannot dispose the sender, think about it, the code that call your
                  method is holding a reference to it.
                  Also, check in the debigger the type of the Sender, you are blindly
                  casting it to a MailMessage.
                  >

                  Comment

                  • Kyr Kyr

                    #10
                    Re: deleting email attachments

                    I am having a similar problem.

                    I connot delete the file recently attached to an email even though I am
                    not running async.

                    Thanks,
                    Kyr

                    *** Sent via Developersdex http://www.developersdex.com ***

                    Comment

                    Working...