Tutorial :Create a Email message in .NET



Question:

I want to be able to create an email message with an attachment, but not send it. The email should open in Outlook where the user can send himself.

I have been playing around with Mailto: command in order to open a new mail message, however, Outlook client doesn't seem to support adding attachments using the Mailto: command.

I do not want to use COM to do this.

Does anyone know of a way to achieve this? I think it might not be possible without COM.


Solution:1

The only way around COM (or VSTO) I can think of would be to use the command line options of Outlook. Using Process.Start you could start Outlook with the options described in this answer (by VonC):

Open a new mail message:

    outlook.exe /c ipm.note  

Open a new mail message and populate sender:

    outlook.exe /c ipm.note /m someone@example.com  

Open a new mail message with attachment:

    outlook.exe /c ipm.note /a filename  

Combination:

    outlook.exe /c ipm.note /m someone@example.com&subject=test%20subject&body=test%20body  

You can retrieve the installation folder of Outlook from the following Registry keys:

Outlook 2007:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\12.0\Outlook\InstallRoot  

Outlook 2003:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\11.0\Outlook\InstallRoot  

Outlook XP:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\10.0\Outlook\InstallRoot  


Solution:2

If you want to open the mail message in Outlook, then I'm pretty sure you will need to use COM. Is there any particular reason you want to interact with Outlook rather than automate the sending using SMTP and the System.Net.Mail namespace?

Edit: It seems you can specify an attachment using a mailto link, by the way. Example:

mailto:foo@bar.com?subject=foo&body=bar&attachment="C:/foo/bar.txt"  

Have you tried this with Outlook? However, I would still recommend COM as the way to go, since it gives you a good deal more control over what you can do with Outlook and mail messages.


Solution:3

You've answered yourself there - you can either do it through COM (via the primary interop assemblies), or you can send via SMTP directly (which would not touch the user's email client at all)


Solution:4

I'm not sure if you can link it into Outlook (I'm guessing you'll have to use MAPI), but System.Net.Mail is great.


Solution:5

you can also save the message to folder instead sending only by configuring smtp settings

<system.net>  <mailSettings>    <smtp deliveryMethod="SpecifiedPickupDirectory">      <specifiedPickupDirectory pickupDirectoryLocation="c:\pickup-smtp\"/>            </smtp>  </mailSettings>  


Solution:6

Easy:

using Microsoft.Office.Interop.Outlook;    private void CreateMailItem()  {      Outlook.MailItem mailItem = (Outlook.MailItem)          this.CreateItem(Outlook.OlItemType.olMailItem);      mailItem.Subject = "This is the subject";      mailItem.To = Environment.UserName + "@example.com";      mailItem.Body = "This is the message.";      mailItem.Importance = Outlook.OlImportance.olImportanceLow;      mailItem.Display(false);  }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »