Mail attachment problem

I am having a problem with an email attachment. I am using filesystem control to get file path from local machine.

It specifies the correct path, but, masking a file from any folder, gives the error "Invalid email attachment".

Below 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 figure out why I am seeing this error?

0


a source to share


3 answers


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 a working file .... using this line ..... (SmtpMail.SmtpServer = "";)

thanx for alll's answers ...

0


a source


The FileName property of the FileUpload control will give you the name of the file on the client, and the code that creates the mail message is running on the server. You will need to save the file to the server first and then refer to this path when creating an email 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));

      

+1


a source


When the code is executed objmail.Attachments.Add(attch);

, the variable is objmail

not created and hence you get an error.

This line of code should be written in the method sndmail_Click

afterobjmail = new MailMessage();

Note that this attch

is a local variable in the method attch_Click

and will not be available in the method sndmail_Click

. You must make this variable available to both methods, so declare along with where you declared the variable objmail

.

Edit

However, as Fredrik pointed out in his answer, you need to save the file to the server in a method attch_Click

and in the method sndmail_Click

add the saved file to the collection objmail.Attachments

.

+1


a source







All Articles