Szymek G
Szymek G

Reputation: 31

Illegal characters in the path

I'm trying to send mail with attachments using smtp client. Everything goes well when I'm trying to add an attachment like that:

System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(@"C:\icon.jpg");
mail.Attachments.Add(attachment);

but when I try to read a path from the console like:

string path = Console.Read();
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(path);
mail.Attachments.Add(attachment);

I'm getting the exception

Illegal charcters in the path

Is there anyone who could explain to me why it doesn't work?

Upvotes: 0

Views: 312

Answers (1)

Cleptus
Cleptus

Reputation: 3531

The problem with your code is that Console.Read() function is intended to read only the next character from the input.

You should use Console.ReadLine() instead, which will read an entire line from the input.

string path = Console.ReadLine();
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(path);
mail.Attachments.Add(attachment);

Upvotes: 1

Related Questions