2

I need to open an new email with an attachment via the default mail manager (without SMTP code) I use:

System.Diagnostics.Process.Start(String.Format("mailto:{0}", txtEmail.Text)) 

Is it possible to add an attachment too?


I could try this

http://www.codeproject.com/Articles/17561/Programmatically-adding-attachments-to-emails-in-C

but I should understand that there is always a Microsoft Outlook need to the client computer...

serhio
  • 28,010
  • 62
  • 221
  • 374
  • 2
    Possible duplicate http://stackoverflow.com/q/1195111/799586 – Bali C Apr 10 '12 at 12:04
  • 1
    That doesn't actually _send_ email, it launches the machine's default email client with pre-populated fields. – Grant Thomas Apr 10 '12 at 12:04
  • @Mr. Disappointment: OK, if you want, I want to OPEN the default email client with a generated image on the disk as attachement... – serhio Apr 10 '12 at 12:14

1 Answers1

0

It's impossible ?!
ive provided a detailed way to do it using System.Net.Mail namespace;

private void button1_Click(object sender, EventArgs e)
{
    SmtpClient smtpserver = new SmtpClient();
    smtpserver.Credentials = new NetworkCredential("email@domain", "passphrase");
    smtpserver.Port = 587;
    smtpserver.Host = "smtp.live.com";
    smtpserver.EnableSsl = true;

    MailMessage mail = new MailMessage();
    mail.From = new MailAddress("email@domain");
    mail.To.Add("recipient@domian");
    mail.Subject = "testing";

    string pathTOAttachment;
    string _Attachment = pathToAttachment;
    Attachment oAttch = new Attachment(_Attachment);
    mail.Attachments.Add(oAttch);

    mail.Body = "message body";

    ThreadPool.QueueUserWorkItem(delegate
    {
        try
        {
            smtpserver.Send(mail);
        }
        //you can get more specific in here
        catch
        {
            MessageBox.Show("failure sending message");
        }
    });
}

Noteworthy (not taken into consideration in this code sample):

  1. some isp may impose size limitation on attachment, some make sure you check it before attempting to send the email.
  2. smtp hosts/port may vary, the most efficient way is to check against a regularly updated database, or let the user set them himself.
  3. the threading part is all about UI responsiveness, but, if the user close the main app window with the mail still sending, it'll be preempted.
Tawfik Khalifeh
  • 939
  • 6
  • 21