SmtpClient Attachments w/ path in the filename

Posted on 2009-04-19
Last Modified: 2012-05-06
Hello, I'm trying to send emails via SMTP using C#.  However the file names need to have the path in them, and I can't seem to get this working.  Each time, the code has a problem with illegal characters.  Any thoughts?

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Net;

using System.Net.Mail;

namespace SMTP_Test


    class Program


        static void Main(string[] args)


            string fileToAttach = "C:\testattachment.txt";

            MailMessage mailMessage = new MailMessage();

            mailMessage.From = new MailAddress("");


            mailMessage.Subject = "This is a test email subject from C#";

            mailMessage.Body = "This is a test email body from C#";

            Attachment attachedFile = new Attachment(fileToAttach);


            SmtpClient smtpClient = new SmtpClient("", 3535);

            smtpClient.UseDefaultCredentials = false;

            smtpClient.Credentials = new NetworkCredential("", "<password>", null);






Open in new window

Question by:jchristn123
    LVL 9

    Accepted Solution

    Try using:
    string fileToAttach = "C:\\testattachment.txt";


    string fileToAttach = @"C:\testattachment.txt";
    LVL 20

    Assisted Solution

    string fileToAttach = @"C:\testattachment.txt";
    LVL 1

    Author Closing Comment

    Thanks guys!

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    729 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

    19 Experts available now in Live!

    Get 1:1 Help Now