2

I have a requirement to create a .Net application which will send a mail message (with attachments and possibly html formatting) but without using System.Net.Mail (we can't ask for user SMTP details) or Outlook (this is for PCs without Outlook installed).

I've been looking into MAPI today but it seems that it can only be implemented by creating a C/C++ library and then using a .NET wrapper which is a lot of work in an unfamiliar area. Most of the sample code and projects around are quite old too, and some rely on the SMTP server details anyway once you start poking around.

Is there a .net friendly solution to sending email without SMTP, Outlook, or delving into C++ and MAPI? Maybe a third party MAPI library?

Carl Onager
  • 4,112
  • 2
  • 38
  • 66

3 Answers3

2

I've found one way to do it that avoids all the pitfalls mentioned above but it's not brilliant. This article covers what is required but it appears to be a bit hit and miss depending on the setup of your system.

Basically you have to use the System.Runtime.InteropServices to decorate the various classes needed with a StructLayout attribute so that they are handled in memory correctly. This allows you to use the MAPI32.DLL SendMail function:

<DllImport("MAPI32.DLL")> _
Private Shared Function MAPISendMail(ByVal sess As IntPtr,
         ByVal hwnd As IntPtr, ByVal message As MapiMessage,
         ByVal flg As Integer, ByVal rsv As Integer) As Integer
End Function

I have developed some .NET code based on this which mostly works but there is a problem in that managed code is simply not compatible with MAPI in the long run which has led to this follow up question.

Community
  • 1
  • 1
Carl Onager
  • 4,112
  • 2
  • 38
  • 66
  • After reading some documentation on it, I stand partially corrected. This might indeed work, given the user has a Mapi-compatible client (Outlook, Thunderbird) installed. – CodeCaster Oct 28 '13 at 18:10
0

you are conused smtp is http://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol that is THE EMAIL protocol.

you MUST use a smtp server.

Send email using System.Net.Mail through gmail

Community
  • 1
  • 1
Nahum
  • 6,959
  • 12
  • 48
  • 69
  • It's perfectly possible, if Outlook is installed, to send email using Outlook without having any SMTP details in the .NET code. Thus using an SMTP server directly is not a necessity. – Carl Onager Oct 28 '13 at 10:00
0

An SMTP or other mail server is a necessity if you want to send mails. That you redirect your mail message using an email application like Outlook which in turn talks to Exchange who finally delivers your message using SMTP does noet neglect the fact that SMTP is used in the end.

You want to do the impossible: you do not want to rely on an already installed mailing application (Outlook, Thunderbird), nor do you want to connect to a mail server: then you cannot send mail.

As for this part of your problem description:

we can't ask for user SMTP details

You can then perhaps write a webservice that you host, which is a layer over an SMTP or Exchange server under your control. You write this service to accept mail message from your applications, so clients can call it without knowing about the underlying transport or the server configuration; all they need to know is your service's address.

Your applications can then send a SendEmail() request to the service, without the need for configuring an SMTP server or Outlook connection at the client.

Carl Onager
  • 4,112
  • 2
  • 38
  • 66
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • 1
    If the email is internal and the company is using Exchange, there's no need for SMTP to get involved. I receive 10 emails a day from my colleagues, that's done via MAPI. – ta.speot.is Oct 28 '13 at 10:07
  • Obviously an SMTP server is necessary at some point, that's a given. I've edited the question to be more precise about the exact requirements. – Carl Onager Oct 28 '13 at 10:08
  • You are correct in that regard that I assumed OP wants to send mails to the outside world. – CodeCaster Oct 28 '13 at 10:09
  • @Clara please explain your actual problem. You want to send emails, but you don't want to use an email sending protocol. Explain why. – CodeCaster Oct 28 '13 at 10:10
  • @CodeCaster Please reread the edited question, I can't think of a way to make it easier to understand that I need to send email from an application without using System.Net.Mail our Outlook. The question has nothing to do with SMTP servers, that's just a diversion down the wrong path. – Carl Onager Oct 28 '13 at 10:14
  • @Clara please read my answer again, **there is no way to send mails without accessing a mail server at some point**. You can for example do so in a webservice that you create, like I already pointed out in my answer. – CodeCaster Oct 28 '13 at 10:15
  • What about third party libraries for MAPI? – Carl Onager Oct 28 '13 at 10:17
  • MAPI is used to access Microsoft Exchange Server. Are you going to assume Exchange is installed and that authentication and discovery is set up correctly for the user using your program? – CodeCaster Oct 28 '13 at 10:20
  • *Extended* MAPI is used with Exchange but *simple* MAPI can, theoretically, be used with other mail clients. – Carl Onager Oct 28 '13 at 10:38