Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello everybody
I am sending a email to client, and want to attach a pdf file with this mail. This pdf file is exists in project, but i am facing problem to provide proper path of pdf file.

C#
string AppPath = HttpContext.Current.Request.ApplicationPath.ToString();
String _fileNam = AppPath + "\\master\\Voucher.PDF"; 
  Attachment _attach = new Attachment(_fileNam);
            _mailMessage.Attachments.Add(_attach);


But its showing me an error related to path. If you have any suggestion then please tell me.

Thanks
dEV kASHYAP
Posted
Updated 10-Apr-12 21:51pm
v2

The reason may be that the property
HttpContext.Current.Request.ApplicationPath returns the virtual application path.
Converting the path to a physical path as explained here
http://www.cambiaresearch.com/articles/35/how-do-i-get-the-application-path-in-an-aspnet-application[^]
may solve the problem
 
Share this answer
 
Woukld be helpful if we knew what the exact error message was? But based on your description it sounds like you are not forming the path to your file properly and your code certainly isn't 100% reliable. I would say you need to do the following to get a well-formed, proper file address:

C#
String _fileNam = System.IO.Path.Combine(AppPath, @"\master\Voucher.pdf");


If this doesn't solve your issue then check the value of _fileNam by debugging - if you can't really debug, get your code to send you an email containing the path of the file (but don't try attaching the file to that email.) Then, take that path and paste it into a windows explorer (NOT internet explorer) address bar on your server - if it works, you have a different issue to the file not being found. Also, make absolutely sure you have copied all the files to your web server.

Hope this helps,
Ed
 
Share this answer
 
Hi,
C#
  using System;
using System.Web.Mail;

namespace WebMail
{
    class Class1
    {
        static void Main(string[] args)
        {
            try 
            {
                MailMessage oMsg = new MailMessage();
                // TODO: Replace with sender e-mail address.
                oMsg.From = "sender@somewhere.com";
                // TODO: Replace with recipient e-mail address.
                oMsg.To = "recipient@somewhere.com";
                oMsg.Subject = "Send Using Web Mail";
                
                // SEND IN HTML FORMAT (comment this line to send plain text).
                oMsg.BodyFormat = MailFormat.Html;
                
                // HTML Body (remove HTML tags for plain text).
                oMsg.Body = "<HTML><BODY>Hello World!</BODY></HTML>";
                
                // ADD AN ATTACHMENT.
                // TODO: Replace with path to attachment.
                String sFile = @"C:\temp\Hello.txt";  
                MailAttachment oAttch = new MailAttachment(sFile, MailEncoding.Base64);
  
                oMsg.Attachments.Add(oAttch);

                // TODO: Replace with the name of your remote SMTP server.
                SmtpMail.SmtpServer = "MySMTPServer";
                SmtpMail.Send(oMsg);

                oMsg = null;
                oAttch = null;
            }
            catch (Exception e)
            {
                Console.WriteLine("{0} Exception caught.", e);
            }
        }
    }
}

best Luck
 
Share this answer
 
v2

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