tags:

views:

216

answers:

3

hi every one

I'm having problem in mail attachment.I'm using the fileupload control to get the path of the file from the local machine.

It's giving the proper path but while attching the file from any folder its giving error "Invalid mail attachment".

Following is the code..

  protected void sndmail_Click(object sender, EventArgs e)
    {
        objmail = new MailMessage();
        objmail.From = txt_sender.Text;
        objmail.To = txt_recipient.Text;
        objmail.Cc = txt_cc.Text;
        objmail.BodyFormat = MailFormat.Text;
        objmail.Priority = MailPriority.High;
        objmail.Subject = txt_sub.Text;
        objmail.Body = txt_body.Text;
        SmtpMail.SmtpServer = "localhost";
        SmtpMail.Send(objmail);
        Response.Write("Mail send successfully...");

    }


    protected void attch_Click(object sender, EventArgs e)
    {
        string mypath = System.IO.Path.GetFullPath(FileUpload1.FileName);
        MessageBox.Show(mypath); 
        MailAttachment attch = new MailAttachment(mypath);

        objmail.Attachments.Add(attch);
    }

Can you help me work out why I'm seeing this error?

+1  A: 

The FileName property of the FileUpload control will give you the name of the file on the client, while the code that is creating the mail message is running on the server. You will need to first store the file on the server, then refer to that path when creating the mail attachment.

string fileNameOnServer = Path.Combine("<some writeable path on your server>", FileUpload1.FileName);
FileUpload1.SaveAs(fileNameOnServer);

// now you can user fileNameOnServer to attach the file to a mail message
objMail.Attachments.Add(new Attachment(fileNameOnServer));
Fredrik Mörk
no sir its not working..
Aarsh Thakur
+1  A: 

When the objmail.Attachments.Add(attch); code is executed, the objmail variable is not instantiated and hence you are getting the error.

This line of code should be written in sndmail_Click method after objmail = new MailMessage();

Please mind the fact that attch is a local variable in attch_Click method and would not be accessible in the sndmail_Click method. You should make this variable available to both methods, hence declare along with where you've declared the objmail variable.

Edit:

However, as Fredrick has outlined in his answer you need to save the file in the server in the attch_Click method and in the sndmail_Click method add the saved file to the objmail.Attachments collection.

Nahom Tijnam
no sir its still giving same error.
Aarsh Thakur
Nahom Tijnam
A: 

objmail = new MailMessage();

    objmail.Attachments.Add(new MailAttachment(FileUpload1.PostedFile.FileName));
    objmail.From = txt_sender.Text;
    objmail.To = txt_recipient.Text;
    objmail.BodyFormat = MailFormat.Text;
    objmail.Priority = MailPriority.High;
    objmail.Subject = txt_sub.Text;
    objmail.Body = txt_body.Text;
    SmtpMail.SmtpServer = "";
    SmtpMail.Send(objmail);
    Response.Write("Mail send successfully...");

now its working file.... using this line.....(SmtpMail.SmtpServer = "";)

thanx for alll replies...

Aarsh Thakur