简体   繁体   中英

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM