We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,703 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Error - Could not find a part of the path

I want to send the verification mail before the user can sign in for which I have written some code, but the problem if that it gives the error "Could not find a part of the path", but I cannot understand why it is giving the error. The error occurs while trying to read the .txt fle using StreamReader.

it says - Could not find a part of the path 'C:\JobPortal\EmailTemplates\VerifyNewUser.txt'.

but it should be searching at the following path -

http://localhost:49292/JobPortal/EmailTemplates/VerifyNewUser.txt

The code is pasted below:

protected void RegisterUser_CreatedUser(object sender, EventArgs e)
    {
        MembershipUser newUser = Membership.GetUser(RegisterUser.UserName);
        Guid newUserId = (Guid)newUser.ProviderUserKey;
        string urlBase = Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath;
        string verifyUrl = "/VerifyNewUser.aspx?ID=" + newUserId.ToString();
        string fullPath = urlBase + verifyUrl;
        string AppPath = Request.ApplicationPath;
        StreamReader sr = new StreamReader(AppPath + "/EmailTemplates/VerifyNewUser.txt");


        MailMessage Mailmsg = new MailMessage();
        Mailmsg.IsBodyHtml = true;
        Mailmsg.From = new MailAddress("someidxxx@xxxxmail.com", "Jobs Portal Registration System");
        Mailmsg.To.Add(new MailAddress(RegisterUser.Email));
        Mailmsg.Subject = "New User Registration";

        Mailmsg.Body = sr.ReadToEnd();
        sr.Close();

        Mailmsg.Body = Mailmsg.Body.Replace("<%UserName%>", RegisterUser.UserName);
        Mailmsg.Body = Mailmsg.Body.Replace("<%VerificationUrl%>", fullPath);

        SmtpClient mailclient = new SmtpClient();
        mailclient.EnableSsl = true;
        mailclient.Send(Mailmsg);
        //FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */);
    }
3
Contributors
2
Replies
5 Days
Discussion Span
1 Year Ago
Last Updated
3
Views
tapandesai007
Light Poster
29 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Instead of ApplicationPath try Server.MapPath. This will give the location relative to the server files (within IIS or whatever server you are using) instead of the location of the files on disc ,which is what ApplicationPath is giving you.
Server.MapPath will give the location from localhost, instead of from the C drive.

hericles
Veteran Poster
1,065 posts since Nov 2007
Reputation Points: 156
Solved Threads: 228
Skill Endorsements: 10

Ya hercles told correct use server.mappath my notification is don`t forgot "~" symbol in front of path

string path=server.MapPath("~\your_file_name.Extension");
mani-hellboy
Junior Poster in Training
69 posts since Feb 2012
Reputation Points: 0
Solved Threads: 7
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0609 seconds using 2.72MB