views:

132

answers:

2

I have an asp.net website and I allready did .Dispose() here is my code below;

try {

        MailMessage newMail = new MailMessage(MailFrom, MailTo,

MailSubject, MailMsg);

        if (MailAttachment != "")
        {
            Attachment data = new Attachment(MailAttachment,

MediaTypeNames.Application.Octet); newMail.Attachments.Add(data); } newMail.BodyEncoding = System.Text.Encoding.UTF8; newMail.IsBodyHtml = true;

        SmtpClient client = new SmtpClient("192.168.2.205");
        client.Credentials = CredentialCache.DefaultNetworkCredentials;
        client.Send(newMail);

        newMail.Attachments.Dispose();
        newMail.Dispose();

        DeleteAttachment(MailAttachment);

        lblSuccess.Text = "Başvurunuz alınmıştır teşekkürler.";
        lblSuccess.Visible = true;
        ClearForm();
    }
    catch (Exception ex)
    {
        lblSuccess.Text = ex.Message;
        //lblSuccess.Text = "Bir sorun oluştu bir daha deneyiniz.";
        lblSuccess.Visible = true;
    }

But i' m getting the same error, it' s running fine in my localhost but in server i' m getting this error. How can i fix it?

A: 

check that attachment is attached or not in a thread.then send mail

Rohit
+1  A: 

Call dispose on the Attachment object.

Calling Dispose on the SmtpClient, doesn't call it on the Attachments.

Cheers!
Dave

dave wanta
it didn't worked. All same works in local but not in server?
Xenon
Did you call data.Dispose()? Or just on the attachments collection? Try it on the individual attachment. If that doesn't work, the other thing you can do, is read the file into a byte array, and create the attachment from the byte array.
dave wanta
btw, here is a codesnippet to help: byte[] data = File.ReadAllBytes("c:\\temp\\sample.jpg"); MemoryStream ms = new MemoryStream( data ); Attachment a = new Attachment(ms, "sample.jpg"); m.Attachments.Add(a); File.Delete("c:\\temp\\sample.jpg");
dave wanta