Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Email attachments - even after dispose no release?

Posted on 2010-08-15
2
Medium Priority
?
361 Views
Last Modified: 2013-12-17
in C# I have 6 attachments for emails going out.. 3 are built in crystal 3 are static handouts..
With both types after the run the pdf's can SOMETIMES not be able to be deleted or moved because someone has it open.. I made sure Crystal is closing & disposing at it's end, but I am sure it is the Attachment due to the fact the 3 static PDF's can also get locked.. After 15/30 minutes the resources unlock.. so sounds like a dispose problem.. But.. I dispose on the finally process of a try so if it aborts, it still should dispose.

Below is the code, attempting to abbrevate where not really part of attachment logic.




using System.Net.Mail;
:
:
    Attachment attachmentmsg1;
    Attachment attachmentmsg2;
    Attachment attachmentmsg3;
    Attachment attachmentmsg4;
    Attachment attachmentmsg5;
    Attachment attachmentmsg6;
:
:
        try
        {
            mMailMessage = new MailMessage();
            // Set the sender address of the mail message
            mMailMessage.From = new MailAddress("someone@comcast.net");

:
:
                SetAttachments();
                PrintBody();
:
:
                mMailMessage.Body = fullBody;

:               // Instantiate a new instance of SmtpClient
                SmtpClient mSmtpClient = new SmtpClient("smtp.comcast.net");
                // Send the mail message
                mSmtpClient.Send(mMailMessage);
                mSmtpClient = null;
            }

        }
        catch (Exception)
        {

            throw;
        }
        finally
        {
            attachmentmsg1.Dispose();
            attachmentmsg2.Dispose();
            attachmentmsg3.Dispose();
            attachmentmsg4.Dispose();
            attachmentmsg5.Dispose();
            attachmentmsg6.Dispose();
        }
    private void SetAttachments()
    {
       string strReportPath =  strAppPath + "Reports\\EmailRptPerUnit.rpt";
       string strReportPath2 = strAppPath + "Reports\\EmailRptUnitIndv.rpt";
        string UT = sglUnitInfo.UnitType.Trim();
        string UN = sglUnitInfo.UnitNo.Trim();
        string UnitRpt = PathForUnitAttachments + UT + UN + "UnitRpt.pdf";
        string IndvRpt = PathForUnitAttachments + UT + UN + "IndivRpt.pdf";
        string IndvRpt2 = PathForUnitAttachments + UT + UN + "IndivRpt.doc";
        string ReqTrains = PathForHandouts + "BasicLdrRequirements.pdf";
        string MissingForm = PathForHandouts + "FormForMissingCourses.pdf";
        string ProblemTracking = PathForHandouts + "GuideToTrackingProblems.pdf";
        try
        {
            dsUnitUntrained dsUT = new dsUnitUntrained();
            dsUnitCourses dsCo = new dsUnitCourses();

            crPdf.CreateEnglishPdf(strReportPath, UnitRpt, UT, UN, "CR_Email_Untrained", dsUT,"PDF");
            crPdf.CreateEnglishPdf(strReportPath2, IndvRpt, UT, UN, "CR_Email_Untrained", dsUT, "PDF");
            crPdf.CreateEnglishPdf(strReportPath2, IndvRpt2, UT, UN, "CR_Email_Untrained", dsUT, "doc");

            attachmentmsg1 = new Attachment(UnitRpt);
            mMailMessage.Attachments.Add(attachmentmsg1);
            attachmentmsg2 = new Attachment(IndvRpt);
            mMailMessage.Attachments.Add(attachmentmsg2);
            attachmentmsg3 = new Attachment(IndvRpt2);
            mMailMessage.Attachments.Add(attachmentmsg3);
            attachmentmsg4 = new Attachment(ReqTrains);
            mMailMessage.Attachments.Add(attachmentmsg4);
            attachmentmsg5 = new Attachment(MissingForm);
            mMailMessage.Attachments.Add(attachmentmsg5);
            attachmentmsg6 = new Attachment(ProblemTracking);
            mMailMessage.Attachments.Add(attachmentmsg6);

        }
        catch (Exception)
        {
            
            throw;
        }
    }

Open in new window

0
Comment
Question by:moosetracker
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 20

Accepted Solution

by:
informaniac earned 2000 total points
ID: 33440622
Dispose the mailMessage object also
0
 
LVL 1

Author Closing Comment

by:moosetracker
ID: 33440708
You are brilliently correct... I could of swore I checked that for close and/or dispose, and neither was present for that object..
0

Featured Post

Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question