views:

250

answers:

3

I have a page that have fileupload control, on the submission of the form, when the fileupload control has file, file is sent via attachment in a mail and working absulutly fine, but when the fileupload control does not have file, ATT00006.dat file is automatically sent via email attachment.

Reference URL: http://nextech.pk/Enquiry.aspx?Enq=cu

Advance Thanks for any help

Edit -- Code:

 hpf = fup1.PostedFile;
    String toEmail = "[email protected]";
    String fromEmail = "[email protected]";
    MailMessage objMail = new MailMessage(fromEmail, toEmail);
    objMail.IsBodyHtml = true;

    StringBuilder MailBody = new StringBuilder();

    MailBody.Append("<html><head></head><body> <br>");
    MailBody.Append("<br>" + "An enquiry is filed <br><br>");
    MailBody.Append("<strong><u>Enquirer Information</u></strong>" + "<br><br>");
    MailBody.Append("<strong>Contact Name:</strong>&#09;" + txtFirstName.Text + "<br>");
    MailBody.Append("<strong>Email:</strong>&#09;&#09;&#09; " + txtEmail.Text + "<br>");
    MailBody.Append("<strong>Institute:</strong>&#09;&#09; " + txtInstitute.Text + "<br>");
    MailBody.Append("<strong>Phone #:</strong>&#09;&#09; " + txtPhone.Text + "<br>");

    MailBody.Append("<br><strong>Description:</strong><br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; " + txtEnquiry.Text + "<br>");

    if (hpf != null)
    {
        MailBody.Append("<br>" + "This email also contains an attachment:- <Strong>(" + hpf.FileName + ")</Strong><br>");
    }

    MailBody.Append("</body></html>");
    objMail.Body = MailBody.ToString();
    if (hpf != null)
    {
        System.IO.Stream inputStream = hpf.InputStream;
        String fileName = hpf.FileName;
        Attachment attach = new Attachment(inputStream, fileName);

        objMail.Attachments.Add(attach);
    }
    SmtpClient SmtpClnt = new SmtpClient();
    SmtpClnt.Send(objMail);
A: 

I think the mail server you are using (or antivirus software used by the mail server) is automatically adding this file.

Does the file in question contain anything, or is it empty?

Cocowalla
but when we attach file, then this ATT00006.dat is not attached in email?
Muhammad Akhtar
what does mean check this URL http://nextech.pk/Enquiry.aspx?Enq=cu if user upload file in fileupload control then file will be attached otherwise empty
Muhammad Akhtar
Sorry, what I mean is does the file ATT00006.dat have any contents?
Cocowalla
its 121 bytes and I have try to open it and it is not opened, but I don't think this file contain something
Muhammad Akhtar
If the file is 121 bytes, then it must contain *something* :)Try opening it in notepad to see what it contains
Cocowalla
Its empty, I have open in notepad
Muhammad Akhtar
+1  A: 

Its a mis-match in the attachment type that the system doesn't understand. Please post your code and what you do when there is not file as an attachment.

Shoban
I have posted code, please check, thanks
Muhammad Akhtar
When there is not file attached. Do you see the text "This email also contains an attachment " text along with the ATT00006.dat attachment.
Shoban
yes.. this is the text.... This email also contains an attachment:- ()
Muhammad Akhtar
if user upload file, then it is like... This email also contains an attachment:- (uploaded file name)
Muhammad Akhtar
I asked when there is no attachment then do you see the line which says "the email also contains .... ".... i.e do you see it everytime or only when there is atttchment?
Shoban
every time i am seeing in the email
Muhammad Akhtar
ok. the instead of using if (hpf != null) . Try using if (fup1.HasFile) and see whether it works.
Shoban
yes, I already have assumed same and have tested it on live server
Muhammad Akhtar
and.....?
Shoban
what???????????
Muhammad Akhtar
A: 

Finally it works. actually this page is made by one of other developer

what was the issue is, he took object httppostedfile and assign

System.Web.HttpPostedFile hpf = fup1.PostedFile;

he put the conditon every where and this return always true whether fileupload control has file or not, because of fup1 control existenance and this will not have hpf.hasfile property, so finally I use fileupload control directly and use hasfile property and other peroperty directly

if (hpf != null)
    { 
    }

so the code look like...

String toEmail = "[email protected]";
    String fromEmail = "[email protected]";
    MailMessage objMail = new MailMessage(fromEmail, toEmail);

    objMail.IsBodyHtml = true;
    objMail.Subject = "Subject";

    StringBuilder MailBody = new StringBuilder();

    MailBody.Append("<html><head></head><body> <br>");
    MailBody.Append("<br>" + "An enquiry is filed <br><br>");
    MailBody.Append("<strong><u>Enquirer Information</u></strong>" + "<br><br>");
    MailBody.Append("<strong>Contact Name:</strong>&#09;" + txtFirstName.Text + "<br>");
    MailBody.Append("<strong>Email:</strong>&#09;&#09;&#09; " + txtEmail.Text + "<br>");
    MailBody.Append("<strong>Institute:</strong>&#09;&#09; " + txtInstitute.Text + "<br>");
    MailBody.Append("<strong>Phone #:</strong>&#09;&#09; " + txtPhone.Text + "<br>");

    MailBody.Append("<br><strong>Description:</strong><br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; " + txtEnquiry.Text + "<br>");

    if (fup1.HasFile)
    {
        MailBody.Append("<br>" + "This email also contains an attachment:- <Strong>(" + fup1.FileName + ")</Strong><br>");
    }

    MailBody.Append("</body></html>");
    objMail.Body = MailBody.ToString();

    if (fup1.HasFile)
    {
        System.IO.Stream inputStream = fup1.PostedFile.InputStream;
        String fileName = fup1.FileName;
        Attachment attach = new Attachment(inputStream, fileName);

        objMail.Attachments.Add(attach);
    }
    SmtpClient SmtpClnt = new SmtpClient();
    SmtpClnt.Send(objMail);

Many Thanks Shoban and CocoWalla for their help

Muhammad Akhtar