Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello friends, i have to send email via my website with multiple file attached, but when i'm attaching any file the control jump into catch block.otherwise it's working well.
code is below please have a look.
C#
protected void btnSend_Click(object sender, EventArgs e)
    {
        try
        {
            string str = "";
            HttpFileCollection fileCollection = Request.Files;
            for (int i = 0; i < fileCollection.Count; i++)
            {
                HttpPostedFile uploadfile = fileCollection[i];

                string fileName = Path.GetFileName(uploadfile.FileName);
               // fileName = GetUniqueFileName(fileName);
                if (uploadfile.ContentLength > 0)
                {
                    uploadfile.SaveAs(Server.MapPath("~/Upload/MailAttachments/") + fileName);
                    str += fileName + ",";
                }
            }

            string BodyText = "";
            BodyText = "<b>Dear User,</b><br><br>";
            BodyText = BodyText + "Following are the details of the patient who has requested for a appointment you through your website 'ahms.co.in'<br /><br />";
            BodyText = BodyText + "Patient Name:  " + txtPatient.Text + "<br>";
            BodyText = BodyText + "Email :  " + txtEmail.Text + "<br>";
            BodyText = BodyText + "Mobile Number:  " + txtMobile.Text + "<br>";
            BodyText = BodyText + "Health issues:  " + txthealthissue.Text + "<br> <br />";
            BodyText = BodyText + "Appointment Date:  "+txtDate.Text+"<br/>";
            BodyText = BodyText + "Time Slot choosed: " + ddtimeslot.Text + "<br/>";
            BodyText = BodyText + "Thanks,<br />Web Admin";

           

            if (obj.SendMail("info@ahms.co.in", "mail@ahms.co.in ", "Request for Appointment from : ahms.co.in", BodyText, str) == "mailsend")
                {
                    lblMsg.Text = "Thankyou!!We'll get back to you soon";
                    Reset();
                }
                else
                {
                    lblMsg.Text = "Mail server encounter an error.Try again.";

                }
     }

        catch (Exception ex)
        {
            Response.Write(ex);
        }



    }

here is mail sending function

C#
public String SendMail(String MailTo, String MailFrom, String Subject, String MailBody, string pAttachmentPath)
    {
        try
        {
            MailMessage msg = new MailMessage();
            MailAddress mailaddressfrom = new MailAddress(MailFrom);
            MailAddress mailaddressTo = new MailAddress(MailTo);
            msg.From = mailaddressfrom;
            msg.To.Add(mailaddressTo);
            msg.Subject = Subject;
            msg.Body = MailBody;
            msg.IsBodyHtml = true;

            if (pAttachmentPath.Trim() != "")
            {
                System.Net.Mail.Attachment MyAttachment = new System.Net.Mail.Attachment(pAttachmentPath);
                msg.Attachments.Add(MyAttachment);
                msg.Priority = System.Net.Mail.MailPriority.High;
            }
            SmtpClient objSmtp = new SmtpClient();
            
            objSmtp.Send(msg);
            return "mailsend";
        }
        catch (Exception ex)
        {
            return ex.ToString();
        }
    }
Posted
Updated 24-Oct-13 20:54pm
v3
Comments
Khandwawala Hatim 25-Oct-13 2:21am    
What is the exception you are getting in catch block?
Mohd Arif Khan 25-Oct-13 2:55am    
file not found
Mohd Arif Khan 25-Oct-13 2:59am    
i mailsending function the error has thrown.
ex = {"Could not find file 'C:\\Program Files\\Common Files\\Microsoft Shared\\DevServer\\10.0\\2.jpg,'.":"C:\\Program Files\\Common Files\\Microsoft Shared\\DevServer\\10.0\\2.jpg,"}
Mohd Arif Khan 25-Oct-13 3:00am    
it can not find the path of the uploaded file. phil.o the , is for sepration for multiple files

C#
str += fileName + ",";


Try to remove + "," and see what happens. It's just a guess, we would need the exact exception message, and the line from where it has been thrown.
 
Share this answer
 
Hi,

As you use here...

C#
uploadfile.SaveAs(Server.MapPath("~/Upload/MailAttachments/") + fileName);
                    str += fileName + ",";


Modified :

C#
string path = Server.MapPath("~/Upload/MailAttachments/") + fileName;
uploadfile.SaveAs(Server.MapPath(path);
str = "Upload/MailAttachments/" + fileName;


// here you want to add multiple files so beware about last comma...(I use it as single file. first try this.)
// one more suggestion you use filename as uploaded file name, But I think you can generate a random string for this for all time...


or a alternate method also you can try I describe in my blog[^].

Hope it will help you.

Thanks
 
Share this answer
 
v2
Comments
Mohd Arif Khan 25-Oct-13 4:43am    
no it's not working, exception is thrown it is a physical path but virtual path is expected
Hemant Singh Rautela 25-Oct-13 4:55am    
Then you can try as I posted in my BLOG...It is working solution for attachement.

you should read this ...
http://msdn.microsoft.com/en-us/library/56wesadc.aspx for use of Attachment(Attachment data = new Attachment(textMessage);)
Hemant Singh Rautela 25-Oct-13 5:04am    
I updated my code as
str = "Upload/MailAttachments/" + fileName;
try this one...
Mohd Arif Khan 25-Oct-13 5:10am    
this way i have already tried. not worked
Hemant Singh Rautela 25-Oct-13 5:34am    
Then you have to read this:
http://msdn.microsoft.com/en-us/library/System.Net.Mail.Attachment(v=vs.110).aspx

Or as I explained in my blog...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900