[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1258
  • Last Modified:

SmtpClient Attachments w/ path in the filename

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?

Thanks!
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("info@domain.com");
            mailMessage.To.Add("info@domain.com");
            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);
            mailMessage.Attachments.Add(attachedFile);
            SmtpClient smtpClient = new SmtpClient("smtp.domain.com", 3535);
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials = new NetworkCredential("info@domain.com", "<password>", null);
            smtpClient.Send(mailMessage);
            return;
        }
    }
}

Open in new window

0
jchristn123
Asked:
jchristn123
2 Solutions
 
tetorvikCommented:
Try using:
string fileToAttach = "C:\\testattachment.txt";

or

string fileToAttach = @"C:\testattachment.txt";
0
 
informaniacCommented:
string fileToAttach = @"C:\testattachment.txt";
0
 
jchristn123Author Commented:
Thanks guys!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now