C# sending mail - error

Posted on 2012-08-30
Last Modified: 2012-09-06
I have this piece of code bellow and I get the following error.

    Page.Title = "Post Your Advertisement";
    var result = "";
            ModelState.AddError("title", "Please provide a title");
        if(Request["title"].Length > 200){
            ModelState.AddError("title", "Your title cannot exceed 200 characters");
            ModelState.AddError("description", "You must provide a description");
            ModelState.AddError("duration", "Please choose a duration");
            ModelState.AddError("price", "Please provide a price");
        if(!Request["price"].IsDecimal () && !Request["Price"].IsEmpty()){
            ModelState.AddError("price", "Please provide a valid number for the price");
            ModelState.AddError("condition", "Please state the condition of your item");
            ModelState.AddError("email", "Please provide your email address");
        if(!Request["email"].IsEmpty() && !Functions.IsValidEmail(Request["Email"])){
            ModelState.AddError("email", "Please provide a valid email address");
            ModelState.AddFormError(@"Please fix the errors below 
                                    before submitting the form");
            var message = "<p>Details of your item for sale:</p>";
            message += "Title: " + Request["title"] + "<br />";
            message += "Description: " + Request["description"] + "<br />";
            message += "Duration: " + Request["duration"] + " days<br />";
            message += "Price: " + String.Format("{0:c}", Request["price"].AsFloat()) + "<br />";
            message += "Condition: " + Request["condition"];
                    to: Request["email"],
                    subject: "Advertisement confirmation",
                    body: message,
                    isBodyHtml: true
            result = "Your advertisement details have been sent to you by email";
<form id="post-advert" action="@Href("~/Sell")" method="post">
        <legend>Post Your Advertisement</legend>
            <label for="title">Title*</label>
            <input type="text" id="title" name="title" value="@Request["title"]" />
            <label for="description">Description*</label>
            <textarea id="description" name="description">@Request["description"]</textarea>
            <label for"duration">Duration*</label>
            <select id="duration" name="duration">
                <option value="">--Choose one--</option>
                <option value="1" @Helpers.Selected("duration","1")>1 Day</option>
                <option value="3" @Helpers.Selected("duration","3")>3 Days</option>
                <option value="7" @Helpers.Selected("duration","7")>7 Days</option>
                <option value="14" @Helpers.Selected("duration","14")>14 Days</option>
           <label for="price">Price*</label>
            <input type="text" id="price" name="price" value="@Request["price"]" />
            <label for="condition">Condition*</label>
            <input type="radio" id="condition" name="condition" value="Fair" @Helpers.Checked("condition","Fair") />Fair 
            <input type="radio" id="condition" name="condition" value="Good" @Helpers.Checked("condition","Good") />Good 
            <input type="radio" id="condition" name="condition" value="As New" @Helpers.Checked("condition","As New") />As New 
            <label for="email">Your Email Address*</label>
            <input type="text" id="email" name="email" value="@Request["email"]" />
            <input type="submit" name="Submit" value="Post" />

Open in new window


Server Error in '/' Application.
Could not find a part of the path 'C:\Mail\f0a8e85e-fe75-477e-a23c-02f47b9a7c4b.eml'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Mail\f0a8e85e-fe75-477e-a23c-02f47b9a7c4b.eml'.

Source Error:

Line 41:             message += "Price: " + String.Format("{0:c}", Request["price"].AsFloat()) + "<br />";
Line 42:             message += "Condition: " + Request["condition"];
Line 43:             WebMail.Send(
Line 44:                     to: Request["email"],
Line 45:                     subject: "Advertisement confirmation",

Source File: c:\webmatrix\Final Code\Chapter5\Classifieds\Sell.cshtml    Line: 43

Stack Trace:

[DirectoryNotFoundException: Could not find a part of the path 'C:\Mail\f0a8e85e-fe75-477e-a23c-02f47b9a7c4b.eml'.]
   System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +224
   System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath) +1142
   System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) +78
   System.IO.FileStream..ctor(String path, FileMode mode) +71
   System.Net.Mail.SmtpClient.GetFileMailWriter(String pickupDirectory) +163
   System.Net.Mail.SmtpClient.Send(MailMessage message) +1206

[SmtpException: Failure sending mail.]
   System.Net.Mail.SmtpClient.Send(MailMessage message) +1829
   System.Web.Helpers.WebMail.Send(String to, String subject, String body, String from, String cc, IEnumerable`1 filesToAttach, Boolean isBodyHtml, IEnumerable`1 additionalHeaders) +617
   ASP._Page_Sell_cshtml.Execute() in c:\webmatrix\Final Code\Chapter5\Classifieds\Sell.cshtml:43
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +207
   System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors) +68
   System.Web.WebPages.WebPage.ExecutePageHierarchy() +156
   System.Web.WebPages.StartPage.RunPage() +19
   System.Web.WebPages.StartPage.ExecutePageHierarchy() +65
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76
   System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContext context) +249
Question by:yguyon28
    1 Comment
    LVL 8

    Accepted Solution

    to me it looks like "C:\Mail\f0a8e85e-fe75-477e-a23c-02f47b9a7c4b.eml" is not accessible by the code.can you make sure?

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now