Click here to Skip to main content
15,741,892 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hello dear CPers,

I have a web form which functions like a email compose message and send it. I have To,CC, Body area. In addition to them, I have a checkbox where I am having my problem. When user checks this checkbox it supposed to find a file under a specific folder and add a file to the email as an attachment. This process is successfully functioning in my computer. But in the production server this functionality is not working. Here is my code:

MailMessage message = new MailMessage();
                message.IsBodyHtml = true;
                string body = txtTemplate.Text;
                message.Body = body;
                message.Subject = txtEmailSubject.Text;
                message.To.Add(new System.Net.Mail.MailAddress(txtRecipient.Text));
                if (txtCCEmail.Text != "")
                    message.CC.Add(new System.Net.Mail.MailAddress(txtCCEmail.Text));
                message.From = new MailAddress("");
                if (chkAttachment.Checked)
                    string shippingLabelPath = HttpContext.Current.Server.MapPath(RMAConfig.LabelPath + "Shipping Label[" + EmailVariables[0].ToString() + "].pdf");
                    string returndocPath = HttpContext.Current.Server.MapPath(RMAConfig.PDFPath + "Return Documentation [ " + EmailVariables[0].ToString() + " ].pdf");
                    if (File.Exists(shippingLabelPath))
                        Attachment attachedShipping = new Attachment(shippingLabelPath);
                        attachedShipping.ContentDisposition.FileName = "Shipping Label.pdf";
                    if (File.Exists(returndocPath))
                        Attachment attachedRReturnDoc = new Attachment(returndocPath);
                        attachedRReturnDoc.ContentDisposition.FileName = "Return Documentation.pdf";
                System.Net.Mail.SmtpClient server = new System.Net.Mail.SmtpClient("");
                server.Credentials = new System.Net.NetworkCredential("", "*******");

I have made sure the files exists in the folder and they certainly are there. But they are not getting attached in production server. Is there something I am missing?

Thank you all
Updated 30-Nov-11 13:21pm
Sander Rossel 28-Nov-11 13:44pm    
Any exceptions? Looks like something could be wrong with RMAConfig.
wonder-FOOL 28-Nov-11 13:56pm    
No I havent received any exceptions. The code is inside a try-catch block. It is sending the email to the recipients but except the attachments. RMAConfig is a class that I have created. The value of RMAConfig.PdfPath's value is "./Data/Return Documentation/" i dont see any problem with that since I have used the same path to save the file. Do you think it might be something related to the permissions?
Sander Rossel 28-Nov-11 14:15pm    
Perhaps, if your application has no permission to get to the file. Although I think (not sure) that would raise an Exception.

1 solution

Two things that you need to consider while sending an email with attachment.
1. The file that is going to be attached has to have read permission. You already discussed with Naerling.
2. The size of the file has to checked against the actual size permitted to process by IIS worker process. This can be set from the web.config file of your web application.
<!-- Execute maximum 100MB file size and upload execution time out 15 minutes-->
    <httpRuntime maxRequestLength="102400" executionTimeout="5400"/>

Hope this will help you well.
Share this answer

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