Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET email
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("mail@blabla.com");
                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";
                        message.Attachments.Add(attachedShipping);
                    }
                    if (File.Exists(returndocPath))
                    {
                        Attachment attachedRReturnDoc = new Attachment(returndocPath);
                        attachedRReturnDoc.ContentDisposition.FileName = "Return Documentation.pdf";
                        message.Attachments.Add(attachedRReturnDoc);
                    }
                }
                System.Net.Mail.SmtpClient server = new System.Net.Mail.SmtpClient("mail.blabla.com");
                server.Credentials = new System.Net.NetworkCredential("email@email.com", "*******");
                server.Send(message);              
 
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
Posted 28-Nov-11 7:35am
Edited 30-Nov-11 14:21pm
v2
Comments
Naerling at 28-Nov-11 13:44pm
   
Any exceptions? Looks like something could be wrong with RMAConfig.
wonder-FOOL at 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?
Naerling at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  

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

  Print Answers RSS
0 DamithSL 405
1 OriginalGriff 245
2 Sergey Alexandrovich Kryukov 207
3 Peter Leow 185
4 Afzaal Ahmad Zeeshan 154
0 OriginalGriff 7,460
1 DamithSL 5,519
2 Sergey Alexandrovich Kryukov 4,954
3 Maciej Los 4,936
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 30 Nov 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100