4

I am currently using Process.Start to send simple emails from my WinForms app. Can you think of any way to add a file attachment to the email? (Edit: using Process.Start?)

Here's what I use now:

Process.Start("mailto:test@test.invalid?subject=" + HttpUtility.HtmlAttributeEncode("Application error report") + "&body=" + body);
P a u l
  • 7,805
  • 15
  • 59
  • 92

1 Answers1

9

Try something like this -->

MailMessage theMailMessage = new MailMessage("from@email.com", "to@email.com");
theMailMessage.Body = "body email message here";
theMailMessage.Attachments.Add(new Attachment("pathToEmailAttachment"));
theMailMessage.Subject = "Subject here";

SmtpClient theClient = new SmtpClient("IP.Address.Of.Smtp");
theClient.UseDefaultCredentials = false;
System.Net.NetworkCredential theCredential = new System.Net.NetworkCredential("user@name.com", "password");
theClient.Credentials = theCredential;
theClient.Send(theMailMessage);

Alright, based on your edit and additional info, I found this Blog Post by Jon Galloway, "Sending files via the default e-mail client".

This looks like what you may be looking for, though I don't profess any knowledge with this way as I have always used the method I posted.

Hopefully it is of use to you.

Community
  • 1
  • 1
Refracted Paladin
  • 12,096
  • 33
  • 123
  • 233
  • Important question I forgot to ask. How do you want to send it? Outlook, Google, ect.... – Refracted Paladin Feb 05 '10 at 02:37
  • +1 That will teach me for spending time seeing if this was even possible using Process.Start :) Turns out that you can't add attachments that way, so the already correct System.Net.Mail becomes an even better answer. – David Hall Feb 05 '10 at 02:41
  • 1
    What's funny, is if you search my old posts on SO, I posted an almost identical question almost a year ago. I guess this is me giving back! I love SO! – Refracted Paladin Feb 05 '10 at 02:53
  • The user's default email client opens and presents the message ready to send. I have avoided using SmtpClient thus far because I didn't want to turn my program into an email client. It may be a support issue for the user to find and edit in their email host name. I was hoping someone knew a trick for Process.Start... I should have made this more clear. – P a u l Feb 05 '10 at 02:59
  • MAPISendMail seems to work with win7-64 and Thunderbird which is the only email client I have for testing. The wrapper class code is linked to in the Galloway article. I guess a lot of users won't even be using a local email client nowadays (gmail) so don't know what I will do about that. – P a u l Feb 05 '10 at 08:43