Search code examples
c#processmailtosystem.diagnostics

C# start mail client


I am starting the default mail client with System.Diagnostics.Process

string mailto = string.Format("mailto:{0}?Subject={1}&Body={2}",
                "xxx@xx.xx", Title, TextBlockTechnicalError.Text);
System.Diagnostics.Process.Start(mailto);

However, the content of the email body is cut off half way.It is not showing all that is in TextBlockTechnicalError. How can I avoid this?


Solution

  • I think it is not possible to avoid this, because it is cut off due to maximum length of the command line (at around 8k characters).

    If you know that all users will be using Outlook as mail client, you can try this method

    public void Mail(string receiver, string subject, string body)
    {
      Outlook.Application outlook = System.Diagnostics.Process.GetProcessesByName("OUTLOOK").Length > 0
        ? Marshal.GetActiveObject("Outlook.Application") as Outlook.Application
        : new Outlook.Application();
      Outlook.MailItem mailItem = outlook.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
      if (mailItem == null) throw new Exception("Outlook failed!");
      mailItem.To = receiver ?? string.Empty;
      mailItem.Subject = subject;
      mailItem.Body = body;
      mailItem.Display();
    }