SA Developer .NET

Welcome to SA Developer .NET Sign in | Join | Help
in Search

SMTP Email with Attachment

Last post 09-08-2008, 10:09 by mjohl.inox. 4 replies.
Sort Posts: Previous Next
  •  09-03-2008, 21:04 14507

    Wink [;)] SMTP Email with Attachment

    For anyone that is writing code to send mail with SMTP and attaching a file or files......remember the next piece of advise.

    The Normal Code:
    try
        {
        MailMessage message = new MailMessage(txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text);
        message.Attachments.Add(@"C:\temp\test.doc");
        SmtpClient emailClient = new SmtpClient(txtSMTPServer.Text);
        System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(txtSMTPUser.Text, txtSMTPPass.Text);
        emailClient.UseDefaultCredentials = false;
        emailClient.Credentials = SMTPUserInfo;
        emailClient.Send(message); litStatus.Text = "Message Sent";
        }
    catch (Exception ex)
        {
        litStatus.Text = ex.ToString();
        }

    Problem:
    If you try and delete "C:\temp\test.doc" after sending the email, you will get a IO exception because the file is still being used by a process.
    I battled for four hours thinking that the Thread I was running is what was holding the file, but it wasn't.
    After alot of searching I realised my mistake, knocked my head on the table a couple of times and fixed my idiotic error.

    To release the file for deletion you have to dispose of the attachments after "emailClient.Send(message);":
    message.Attachments.Dispose();

    In all normal examples they never tell you to do this, but I will from now on make it a habit to dispose the attachments after sending the email and setting message & emailClient to null

    Hope it helped someone.


    What not...if not.

    Software is never finished and can always be improved.
  •  09-03-2008, 21:27 14509 in reply to 14507

    Re: SMTP Email with Attachment

    Interesting ...

    Quick question, why do so many developers still adhere to the .Net 1.1 way of setting the SMTPClient? Isn't it more beneficial to use the SMTP configuration under <system.web> in the web.config file?

    Just asking?


    'Not everything is binary, if you look closer you might find some hexadecimal in there somewhere'
  •  09-04-2008, 7:19 14519 in reply to 14507

    Re: SMTP Email with Attachment

    I would agree that using web.config is a better idea, but 99% of my work is done on GUI Software and in the last year I have used only a web.config once.

    The other reason why I stick to the SMTP way is because I have coded over 50 applications for clients in a corporate enviroment and I know that when I use it, it is going to work.

    Yes 


    What not...if not.

    Software is never finished and can always be improved.
  •  09-08-2008, 9:25 14611 in reply to 14509

    Re: SMTP Email with Attachment

    Because so many developers are developing smart clients and not websites? I'm not an ASP fan, give me WPF or even WinForms anyday:-P


    And so the kief looked and lo, it was kief.
  •  09-08-2008, 10:09 14614 in reply to 14611

    Re: SMTP Email with Attachment

    I agree with riccardospagni in this case.

    I can do ASP, but prefer WinForm anyday.


    What not...if not.

    Software is never finished and can always be improved.
View as RSS news feed in XML
Powered by Community Server (Commercial Edition), by Telligent Systems