0

Alright so pretty much I know there is a simple solution for this for the life of me though I can't find it. I want to send an attachment via mail, now I have it so that it thinks it's going to send an attachment like:

message.To.Add(recieve + "@txt.att.net");
message.From = new MailAddress(user);
message.Subject = subject;
message.Body = body;
message.Attachments.Add(new Attachment(add_photo.FileName));
client.Send(message);

You know but if add_photo(The File Dialog) is emtpy it throws and error, I tried adding a catch statement for it but the program just kinda crashes almost (not like crashes crashes but functionality wise).

Anyway, I was thinking if there is no file selected by the dialog I'll just set one myself, something really small that wouldn't even matter. So I have a picture in my resources called 'DD.png' and I would like to set it if there is no file in the dialog any ideas?

Here's what I have:

if (!string.IsNullOrEmpty(add_photo.FileName))
{
    add_photo.FileName = (Path.GetFullPath(Turbo_Bomber.Properties.Resources.DD.ToString()));
}
#region Providers
if (provider == "AT&T")
{
    message.To.Add(recieve + "@txt.att.net");
    message.From = new MailAddress(user);
    message.Subject = subject;
    message.Body = body;
    message.Attachments.Add(new Attachment(add_photo.FileName));
    client.Send(message);
} // etc

Any ideas? Thank you guys.

Chris Schiffhauer
  • 17,102
  • 15
  • 79
  • 88
Frank
  • 75
  • 2
  • 13

1 Answers1

0

Stick with your first go, with a small change:

message.To.Add(recieve + "@txt.att.net");
message.From = new MailAddress(user);
message.Subject = subject;
message.Body = body;
if (!string.IsNullOrEmpty(add_photo.FileName))
{
    message.Attachments.Add(new Attachment(add_photo.FileName));
}    
client.Send(message);

Now you don't need to add a 'mystery' attachment.

simon at rcl
  • 7,326
  • 1
  • 17
  • 24
  • yes but what if there is no attachment to be sent, it's going to throw FileNotFoundExpection won't it? – Frank Feb 05 '14 at 17:53
  • message.Attachments.Add(new Attachment(add_photo.FileName)); It's going to try to attach something that's not there, and it's going to throw a FileNotFound Exception. – Frank Feb 05 '14 at 18:03
  • The point of the change is that if there isn't a filename it won't create an attachment. With no attachment, it won't try to attach something that's not there. No error. – simon at rcl Feb 05 '14 at 18:05
  • Still throws an error though: "A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll" – Frank Feb 05 '14 at 18:11
  • So you have an invalid filename in add_photo.FileName? – simon at rcl Feb 05 '14 at 18:13
  • I haven't selected a file, there for it's invalid? I'm sure what you're getting at? – Frank Feb 05 '14 at 18:15
  • No it's not. The code I show will only add an attachment if there is some text in the add_photo.FileName. If it's adding an attachment, then there is some text in the add_photo.FileName. If the system later errors looking for that filename, it must be because the filename if invalid. You haven't shown what you're doing with the add_photo.FileName, so I have (possibly foolishly) assumed that it's valid. You also haven't shown, come to think of it, that the error is due to an invalid filename in the attachment. Do you know how to debug? – simon at rcl Feb 05 '14 at 18:18
  • The add_photo.filename is just to hold the value to add the attachment one(because it needs to be a string) you know? So if I don't want an attachment it either: a. shouldn't try to add one (meaning FileName is empty) b. Should add SOMETHING to hold it's place (Filename should equal something) Just so that is has SOMETHING to send because Filename requires a string value to be set to, you know what I mean? Meaning the OpenFileDialog (FileName) hasn't even been set to a value. – Frank Feb 05 '14 at 18:22
  • Adding the attachment requires a filename, something like "C:\AFolder\AnotherFolder\AFileName.png". If add_photo.FineName is NOT A FILENAME then you're sunk. – simon at rcl Feb 05 '14 at 18:24
  • I know that, that's exactly what I'm trying to do. If there is no file name (meaning a path given to one) just add one from the Embedded Resources from the program: aka - dd_photo.FileName = Turbo_Bomber.Properties.Resources.DD } – Frank Feb 05 '14 at 18:27