|
|
Comments and Discussions
|
|
 |

|
In my app where are some windows - parent MDI, some MDI childs... and some modeless windows, which are opened from MCD childs. There is the only one window ("error dialog"), which shows an error and a link to open mail client to send error report, shown from Application.ThreadException handler by "error dialog".ShowDialog().
The error dialog has .TopMost == false, but it is shown over any other window of the app. It is modal, so all is OK - while link "send report" is not clicked. When it is clicked, the code below is executing:
var ma = new SendFileTo.MAPI();
ma.AddAttachment(attachmentFileName);
ma.AddRecipientTo(recipient);
ma.SendMailPopup("Application error report", "Report text");
and it shows outlook (my mail client) window well, but the dialog and outlook window becomes NOT TOP windows of the app... Another window appears at the top, not even a window that raised an exception handled.
I've tried to call SendMailPopup() from the second UI thread in app, hope that does not affect other app windows, but it returns an eroror == 2 ("MAPI_E_FAILURE, One or more unspecified errors occurred. No message was sent" - says MSDN, nothing is clear).
So the problem is how to open mail client window to be not affected by any other window of the application (to be in a different thread or even a process)?
|
|
|
|

|
Quote: So the problem is how to open mail client window to be not affected by any other window of the application (to be in a different thread or even a process)?
Exactly what I would like to know!
Edit: I don't think its possible unless you have Windows 8. Read about MAPI_DIALOG_MODELESS
here:
http://msdn.microsoft.com/en-us/library/windows/desktop/hh707275(v=vs.85).aspx[^]
modified 17 hrs ago.
|
|
|
|

|
Someone had asked about this already but has somebody managed to find a solution on how to retain the signature within Outlook? Other than that it works well
Thank you in advance
|
|
|
|

|
hey, i am new to this forum and was attracted here by this awsome post.
however i have a further request: is it possible using the first 2 parametres(handles) of MAPISendMail to detect event on send click?
thanks in advance.
|
|
|
|

|
Thank you all for 99% of the work that this class has done for me (without System.Collections.Generic) (only tested Button Send) (work without attachment) (work with many attachment) (work to: TO, CC, BCC) CODE UNDER Test_Button Private Sub Test_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Test_Button.Click Dim mapi As New SendFileTo.MAPI mapi.AddAttachment("C:\xxx\yyy.XML") mapi.AddAttachment("C:\xxx\yyy.pdf") mapi.AddRecipientTo("xxx@mail.it") mapi.AddRecipientCC("yyy@mail.it") mapi.AddRecipientBCC("zzz@mail.it") mapi.SendMailPopup("Subject at 20.30", "Body at 20.30") End Sub CLASS MODIFIED '''all commented lines have been replaced with those under '''to make the code compatible with VB.2003 Imports System Imports System.Runtime.InteropServices Imports System.IO '''Imports System.Collections.Generic Imports System.Windows.Forms Namespace SendFileTO Class MAPI Public Function AddRecipientTo(ByVal email As String) As Boolean Return AddRecipient(email, howTo.MAPI_TO) End Function Public Function AddRecipientCC(ByVal email As String) As Boolean Return AddRecipient(email, howTo.MAPI_CC) End Function Public Function AddRecipientBCC(ByVal email As String) As Boolean Return AddRecipient(email, howTo.MAPI_BCC) End Function Public Sub AddAttachment(ByVal strAttachmentFileName As String) ' m_attachments.Add(strAttachmentFileName) ReDim Preserve m_attachments(m_attachments.Length) m_attachments(m_attachments.Length - 1) = strAttachmentFileName End Sub Public Function SendMailPopup(ByVal strSubject As String, ByVal strBody As String) As Integer Return SendMail(strSubject, strBody, MAPI_LOGON_UI Or MAPI_DIALOG) End Function Public Function SendMailDirect(ByVal strSubject As String, ByVal strBody As String) As Integer Return SendMail(strSubject, strBody, MAPI_LOGON_UI) End 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 Private Function SendMail(ByVal strSubject As String, ByVal strBody As String, ByVal how As Integer) As Integer Dim msg As MapiMessage = New MapiMessage msg.subject = strSubject msg.noteText = strBody msg.recips = GetRecipients(msg.recipCount) msg.files = GetAttachments(msg.fileCount) m_lastError = MAPISendMail(New IntPtr(0), New IntPtr(0), msg, how, 0) If m_lastError > 1 Then MessageBox.Show("MAPISendMail failed! " + GetLastError(), "MAPISendMail") End If Cleanup(msg) Return m_lastError End Function Private Function AddRecipient(ByVal email As String, ByVal howTo As howTo) As Boolean Dim recipient As MapiRecipDesc = New MapiRecipDesc recipient.recipClass = CType(howTo, Integer) recipient.name = email 'm_recipients.Add(recipient) ReDim Preserve m_recipients(m_recipients.Length) m_recipients(m_recipients.Length - 1) = recipient Return True End Function Private Function GetRecipients(ByRef recipCount As Integer) As IntPtr recipCount = 0 If m_recipients.Length = 0 Then Return New IntPtr(0) End If Dim size As Integer = Marshal.SizeOf(GetType(MapiRecipDesc)) Dim intPtr As IntPtr = Marshal.AllocHGlobal(m_recipients.Length * size) 'Dim ptr As Integer = CType(intPtr, Integer) Dim ptr As Integer = intPtr.ToInt32 Dim mapiDesc As MapiRecipDesc For Each mapiDesc In m_recipients ' Marshal.StructureToPtr(mapiDesc, CType(ptr, IntPtr), False) Marshal.StructureToPtr(mapiDesc, New IntPtr(ptr), False) ptr += size Next recipCount = m_recipients.Length Return intPtr End Function Private Function GetAttachments(ByRef fileCount As Integer) As IntPtr fileCount = 0 If m_attachments Is Nothing Then Return New IntPtr(0) End If If (m_attachments.Length <= 0) Or (m_attachments.Length > maxAttachments) Then Return New IntPtr(0) End If Dim size As Integer = Marshal.SizeOf(GetType(MapiFileDesc)) Dim intPtr As IntPtr = Marshal.AllocHGlobal(m_attachments.Length * size) Dim mapiFileDesc As MapiFileDesc = New MapiFileDesc mapiFileDesc.position = -1 'Dim ptr As Integer = CType(intPtr, Integer) Dim ptr As Integer = intPtr.ToInt32 Dim strAttachment As String For Each strAttachment In m_attachments mapiFileDesc.name = Path.GetFileName(strAttachment) mapiFileDesc.path = strAttachment Marshal.StructureToPtr(mapiFileDesc, New IntPtr(ptr), False) ptr += size Next ' fileCount = m_attachments.Count fileCount = m_attachments.Length Return intPtr End Function Private Sub Cleanup(ByRef msg As MapiMessage) Dim size As Integer = Marshal.SizeOf(GetType(MapiRecipDesc)) Dim ptr As Integer = 0 Dim IntPtrZero As New IntPtr(0) If msg.recips.Equals(IntPtrZero) Then 'ptr = CType(msg.recips, Integer) ptr = msg.recips.ToInt32 Dim i As Integer For i = 0 To msg.recipCount - 1 Step i + 1 'Marshal.DestroyStructure(CType(ptr, IntPtr), GetType(MapiRecipDesc)) Marshal.DestroyStructure(New IntPtr(ptr), GetType(MapiRecipDesc)) ptr += size Next Marshal.FreeHGlobal(msg.recips) End If If msg.files.Equals(IntPtrZero) Then size = Marshal.SizeOf(GetType(MapiFileDesc)) 'ptr = CType(msg.files, Integer) ptr = msg.files.ToInt32 Dim i As Integer For i = 0 To msg.fileCount - 1 Step i + 1 'Marshal.DestroyStructure(CType(ptr, IntPtr), GetType(MapiFileDesc)) Marshal.DestroyStructure(New IntPtr(ptr), GetType(MapiFileDesc)) ptr += size Next Marshal.FreeHGlobal(msg.files) End If 'm_recipients.Clear() ReDim m_recipients(-1) 'm_attachments.Clear() ReDim m_attachments(-1) m_lastError = 0 End Sub Public Function GetLastError() As String If m_lastError <= 26 Then Return errors(m_lastError) End If Return "MAPI error [" + m_lastError.ToString() + "]" End Function ReadOnly errors() As String = New String() {"OK [0]", "User abort [1]", "General MAPI failure [2]", "MAPI login failure [3]", "Disk full [4]", "Insufficient memory [5]", "Access denied [6]", "-unknown- [7]", "Too many sessions [8]", "Too many files were specified [9]", "Too many recipients were specified [10]", "A specified attachment was not found [11]", "Attachment open failure [12]", "Attachment write failure [13]", "Unknown recipient [14]", "Bad recipient type [15]", "No messages [16]", "Invalid message [17]", "Text too large [18]", "Invalid session [19]", "Type not supported [20]", "A recipient was specified ambiguously [21]", "Message in use [22]", "Network failure [23]", "Invalid edit fields [24]", "Invalid recipients [25]", "Not supported [26]"} 'Dim m_recipients As New List(Of MapiRecipDesc) 'Dim m_attachments As New List(Of String) Dim m_recipients(-1) As MapiRecipDesc Dim m_attachments(-1) As String Dim m_lastError As Integer = 0 Private Const MAPI_LOGON_UI As Integer = &H1 Private Const MAPI_DIALOG As Integer = &H8 Private Const maxAttachments As Integer = 20 Enum howTo MAPI_ORIG = 0 MAPI_TO MAPI_CC MAPI_BCC End Enum End Class <StructLayout(LayoutKind.Sequential)> _ Public Class MapiMessage Public reserved As Integer Public subject As String Public noteText As String Public messageType As String Public dateReceived As String Public conversationID As String Public flags As Integer Public originator As IntPtr Public recipCount As Integer Public recips As IntPtr Public fileCount As Integer Public files As IntPtr End Class <StructLayout(LayoutKind.Sequential)> _ Public Class MapiFileDesc Public reserved As Integer Public flags As Integer Public position As Integer Public path As String Public name As String Public type As IntPtr End Class <StructLayout(LayoutKind.Sequential)> _ Public Class MapiRecipDesc Public reserved As Integer Public recipClass As Integer Public name As String Public address As String Public eIDSize As Integer Public enTryID As IntPtr End Class End Namespace
|
|
|
|

|
I changed this class from the original code:
Now Works to sending to CC/BCC senders:
Dim mapi As New SendFileTo.MAPI
mapi.AddRecipientCC("something@domain.com")
Now Works to capture LastError value:
Dim LastErrorInt As Integer = mapi.SendMailPopup("subject", "body")
Dim LastError As String = mapi.GetLastError()
To catch errors you can use this way:
If mapi.SendMailPopup("subject", "body") > 0 Then
MessageBox.Show("MAPISendMail failed! " + mapi.GetLastError(), "MAPISendMail")
End If
And finally to clean MailMessage and reset LastError values use this code:
mapi.CleanMailMessage()
Namespace SendFileTo
Class MAPI
Public Function AddRecipientTo(ByVal email As String) As Boolean
Return AddRecipient(email, howTo.MAPI_TO)
End Function
Public Function AddRecipientCC(ByVal email As String) As Boolean
Return AddRecipient(email, howTo.MAPI_CC)
End Function
Public Function AddRecipientBCC(ByVal email As String) As Boolean
Return AddRecipient(email, howTo.MAPI_BCC)
End Function
Public Sub AddAttachment(ByVal strAttachmentFileName As String)
m_attachments.Add(strAttachmentFileName)
End Sub
Public Function SendMailPopup(ByVal strSubject As String, ByVal strBody As String) As Integer
Return SendMail(strSubject, strBody, MAPI_LOGON_UI Or MAPI_DIALOG)
End Function
Public Sub CleanMailMessage()
Cleanup(msg)
End Sub
Public Function SendMailDirect(ByVal strSubject As String, ByVal strBody As String) As Integer
Return SendMail(strSubject, strBody, MAPI_LOGON_UI)
End 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
Dim msg As MapiMessage
Private Function SendMail(ByVal strSubject As String, ByVal strBody As String, ByVal how As Integer) As Integer
msg = New MapiMessage()
msg.subject = strSubject
msg.noteText = strBody
msg.recips = GetRecipients(msg.recipCount)
msg.files = GetAttachments(msg.fileCount)
m_lastError = MAPISendMail(New IntPtr(0), New IntPtr(0), msg, how, 0)
Return m_lastError
End Function
Private Function AddRecipient(ByVal email As String, ByVal howTo As howTo) As Boolean
Dim recipient As MapiRecipDesc = New MapiRecipDesc()
recipient.recipClass = CType(howTo, Integer)
recipient.name = email
m_recipients.Add(recipient)
Return True
End Function
Private Function GetRecipients(ByRef recipCount As Integer) As IntPtr
recipCount = 0
If m_recipients.Count = 0 Then
Return 0
End If
Dim size As Integer = Marshal.SizeOf(GetType(MapiRecipDesc))
Dim intPtr As IntPtr = Marshal.AllocHGlobal(m_recipients.Count * size)
Dim ptr As Integer = CType(intPtr, Integer)
Dim mapiDesc As MapiRecipDesc
For Each mapiDesc In m_recipients
Marshal.StructureToPtr(mapiDesc, CType(ptr, IntPtr), False)
ptr += size
Next
recipCount = m_recipients.Count
Return intPtr
End Function
Private Function GetAttachments(ByRef fileCount As Integer) As IntPtr
fileCount = 0
If m_attachments Is Nothing Then
Return 0
End If
If (m_attachments.Count <= 0) Or (m_attachments.Count > maxAttachments) Then
Return 0
End If
Dim size As Integer = Marshal.SizeOf(GetType(MapiFileDesc))
Dim intPtr As IntPtr = Marshal.AllocHGlobal(m_attachments.Count * size)
Dim mapiFileDesc As MapiFileDesc = New MapiFileDesc()
mapiFileDesc.position = -1
Dim ptr As Integer = CType(intPtr, Integer)
Dim strAttachment As String
For Each strAttachment In m_attachments
mapiFileDesc.name = Path.GetFileName(strAttachment)
mapiFileDesc.path = strAttachment
Marshal.StructureToPtr(mapiFileDesc, CType(ptr, IntPtr), False)
ptr += size
Next
fileCount = m_attachments.Count
Return intPtr
End Function
Private Sub Cleanup(ByRef msg As MapiMessage)
Dim size As Integer = Marshal.SizeOf(GetType(MapiRecipDesc))
Dim ptr As Integer = 0
If msg.recips <> IntPtr.Zero Then
ptr = CType(msg.recips, Integer)
Dim i As Integer
For i = 0 To msg.recipCount - 1 Step i + 1
Marshal.DestroyStructure(CType(ptr, IntPtr), GetType(MapiRecipDesc))
ptr += size
Next
Marshal.FreeHGlobal(msg.recips)
End If
If msg.files <> IntPtr.Zero Then
size = Marshal.SizeOf(GetType(MapiFileDesc))
ptr = CType(msg.files, Integer)
Dim i As Integer
For i = 0 To msg.fileCount - 1 Step i + 1
Marshal.DestroyStructure(CType(ptr, IntPtr), GetType(MapiFileDesc))
ptr += size
Next
Marshal.FreeHGlobal(msg.files)
End If
m_recipients.Clear()
m_attachments.Clear()
m_lastError = 0
End Sub
Public Function GetLastError() As String
If m_lastError <= 26 Then
Return errors(m_lastError)
End If
Return "MAPI error [" + m_lastError.ToString() + "]"
End Function
ReadOnly errors() As String = New String() {"OK [0]", "User abort [1]", "General MAPI failure [2]", "MAPI login failure [3]", "Disk full [4]", "Insufficient memory [5]", "Access denied [6]", "-unknown- [7]", "Too many sessions [8]", "Too many files were specified [9]", "Too many recipients were specified [10]", "A specified attachment was not found [11]", "Attachment open failure [12]", "Attachment write failure [13]", "Unknown recipient [14]", "Bad recipient type [15]", "No messages [16]", "Invalid message [17]", "Text too large [18]", "Invalid session [19]", "Type not supported [20]", "A recipient was specified ambiguously [21]", "Message in use [22]", "Network failure [23]", "Invalid edit fields [24]", "Invalid recipients [25]", "Not supported [26]"}
Dim m_recipients As New List(Of MapiRecipDesc)
Dim m_attachments As New List(Of String)
Dim m_lastError As Integer = 0
Private Const MAPI_LOGON_UI As Integer = &H1
Private Const MAPI_DIALOG As Integer = &H8
Private Const maxAttachments As Integer = 20
Enum howTo
MAPI_ORIG = 0
MAPI_TO
MAPI_CC
MAPI_BCC
End Enum
End Class
<StructLayout(LayoutKind.Sequential)> _
Public Class MapiMessage
Public reserved As Integer
Public subject As String
Public noteText As String
Public messageType As String
Public dateReceived As String
Public conversationID As String
Public flags As Integer
Public originator As IntPtr
Public recipCount As Integer
Public recips As IntPtr
Public fileCount As Integer
Public files As IntPtr
End Class
<StructLayout(LayoutKind.Sequential)> _
Public Class MapiFileDesc
Public reserved As Integer
Public flags As Integer
Public position As Integer
Public path As String
Public name As String
Public type As IntPtr
End Class
<StructLayout(LayoutKind.Sequential)> _
Public Class MapiRecipDesc
Public reserved As Integer
Public recipClass As Integer
Public name As String
Public address As String
Public eIDSize As Integer
Public enTryID As IntPtr
End Class
End Namespace
modified 17 Jul '12 - 11:57.
|
|
|
|

|
change from:
If m_lastError > 1 Then
MessageBox.Show("MAPISendMail failed! " + GetLastError(), "MAPISendMail")
End If
to:
If m_lastError > 0 Then
MessageBox.Show("MAPISendMail failed! " + GetLastError(), "MAPISendMail")
End If
on this function:
Private Function SendMail(ByVal strSubject As String, ByVal strBody As String, ByVal how As Integer) As Integer
|
|
|
|

|
Anyone figure out how to set the "request read receipt" flag as well?
|
|
|
|

|
Code works great, thanks a lot, saved me a ton of time. Curious if anyone has figured out how to code up a set originator routine. Here's what I have added to the class, and it is not working with the popup functionality which launches Windows Live Mail.
MapiRecipDesc m_sender = null;
bool SetSenderInternal(string email)
{
m_sender = new MapiRecipDesc();
m_sender.recipClass = (int)HowTo.MAPI_ORIG;
m_sender.name = email;
m_sender.address = email;
m_sender.eIDSize = 0;
m_sender.entryID = IntPtr.Zero;
return true;
}
IntPtr GetSender()
{
Type rtype = typeof(MapiRecipDesc);
int rsize = Marshal.SizeOf(rtype);
IntPtr ptrr = Marshal.AllocHGlobal(rsize);
Marshal.StructureToPtr(m_sender, ptrr, false);
return ptrr;
}
public bool SetSender(string email)
{
return SetSenderInternal(email);
}
and then in SendMail() I add the following line:
if (m_sender != null)
{
msg.originator = GetSender();
}
|
|
|
|
|

|
I tried using it in ASP.NET application : not working
|
|
|
|

|
Set AspCompat="true" for page, made working.
But it not working after hosting the app to server [Windows 2003].
Any idea?
|
|
|
|

|
Hi,
I can write a *.msg with MAPI dll?.
|
|
|
|

|
Don't forget the imports at the beginning:
Imports System
Imports System.Runtime.InteropServices
Imports System.IO
Imports System.Collections.Generic
Imports System.Windows.Forms
|
|
|
|

|
Your CC and BCC methods do not work properly. They add them to the TO section, not the respective CC / BCC areas.
|
|
|
|

|
change those lines:
Public Function AddRecipientCC(ByVal email As String) As Boolean
Return AddRecipient(email, howTo.MAPI_TO)
End Function
Public Function AddRecipientBCC(ByVal email As String) As Boolean
Return AddRecipient(email, howTo.MAPI_TO)
End Function
to
Public Function AddRecipientCC(ByVal email As String) As Boolean
Return AddRecipient(email, howTo.MAPI_CC)
End Function
Public Function AddRecipientBCC(ByVal email As String) As Boolean
Return AddRecipient(email, howTo.MAPI_BCC)
End Function
|
|
|
|

|
Hi David,
Thanks for a very useful tool. I have one question when using the SendMailDirect method I get a security warning from outlook, can this be bypassed?
Thanks
Darrin
|
|
|
|

|
If you're trying to use the code in SendFileTo.cs in a Console Application Project, you have to add the STAThreadAttribute to the Main method:
[STAThread]
static void Main(string args[])
Otherwise you will get a "General MAPI failure [2]" back from the MAPISendMail method.
|
|
|
|

|
Thanks. this saved my butt. I'd argue that the code shouldn't be using WinForms to handle the exceptions, but should be throwing actual exceptions.
|
|
|
|

|
Thanks for the comment, that's why it didn't work
|
|
|
|

|
What about embedding images like a logo?
|
|
|
|

|
Thank you very much for this one.
It works like a charm with Windows Live Mail.
Now users don't have to use "regsvr32" to make MSMAPI work
which can be a huge problem for some users.
I found a couple of bugs in both versions (C# and VB).
These functions should look like this (the VB version):
howTo.MAPI_TO should be changed like this:
Public Function AddRecipientCC(ByVal email As String) As Boolean
Return AddRecipient(email, howTo.MAPI_CC)
End Function
Public Function AddRecipientBCC(ByVal email As String) As Boolean
Return AddRecipient(email, howTo.MAPI_BCC)
End Function
And a question: How do you ask for a receipt?
Happy Christmas!
Peter
|
|
|
|

|
Can't say thanks enough, saved me a massive headache! Great article.
|
|
|
|
|

|
I'm using Windows 7 32bit and Microsoft Outlook 2007. It did actually work for me with outlook closed. Which email client and version are you using? Which O/S are you using?
|
|
|
|

|
SendMailPopup doesn't work if I have outlook opened. (I am using Win 7 32 bit with outlook 2010). How to fix it?
|
|
|
|

|
Hi!
Does anybody know how can I send the mail message but keeping the pre-defined signature of the e-mail client?
Thanks in advance!
|
|
|
|

|
Did you ever find this out? Problem I have is that the body of the email is also plain text so you can't even insert it after if there's graphics involved!
|
|
|
|

|
Hi, article is great!
But i am experiencing problem in reading mails using MAPI32.dll, though it is working fine with outlook 2007. But in 2003 MAPIReadMail method giving me Error code 17. Please help me out.
jitenSharma
|
|
|
|
|

|
Hi,
Thanks for the example code, definitely made things a lot easier. I noticed using this method to send a message (outlook 2007 is my default client), that if I cancel the message before sending the email will stay in the outbox. Apparently it has to be manually deleted through the outlook UI. Any ideas on how to clear a cancelled message out? Is this an outlook specific thing perhaps? I tried looking through the simple MAPI and nothing seemed obvious regarding this behaviour.
|
|
|
|

|
Are you using SendMailPopup() or SendMailDirect()? I haven't seen this. Outlook isn't going to put it in your outbox until you hit Send. Did you mean that you're seeing it in your draft box after cancelling?
|
|
|
|

|
Your code help me lot thank you very much you rock...
|
|
|
|

|
Using the code works but I'm have a problem when outlook opens.
The mouse dose not work in outlook.
I have to do some action like click on another running program to give it focus,
then when I click back to outlook I can use the mouse
Anyone had this problem and know of a solution.
Thanks
pqsik
|
|
|
|

|
I found the problem was with krypton toolkit buttons.
I used a standard button and it worked.
Thanks
|
|
|
|

|
Excellent: Simple but very helpful
|
|
|
|

|
Thanks a lot,
It works great !
|
|
|
|

|
Hi,
Very nice article and excellent code. But I have one problem.
When I integrate this class in to my application it works in most cases.
It doesn't work in 2 cases:
1. Mapi class used in Application started as non admin and default mail client started as admin (run as administrator)
2. Mapi class used in Application started as admin and default mail client started as non admin.
When the application and default mail client are started as same type (admin or non admin) it works perfectly.
The system is :
Windows 7 x64,
Outlook 2010 x64
UAC on
If anyone have idea how to overcome this problem please help.
Thanks in advance!!
|
|
|
|

|
I am having the same issue. I can open email via process start but not through MAPI. I get error code 2.
|
|
|
|

|
Anyone have a solution to this? I get error code 2 as well.
|
|
|
|

|
I am also seeing this. (Error code = 2) Any solution?
|
|
|
|

|
It works fine. And nicely described.
|
|
|
|

|
Thanks, that help me a lot and it's clearly explained...
|
|
|
|

|
Hi All,
I tried with winform & it worked perfectly. When i tried to convert this in webform, it does not works. Any one have any idea to help me please ?
Regards,
Sathish
|
|
|
|
|
|

|
Worked excellent first time.
|
|
|
|

|
I had tried several times it doesn´t works if outlook is closed, I have Outlook 2007 and Windows 7 32 bits system.
I hope someone can help me!
tkkss
Claudia
|
|
|
|

|
class Mozilla
{
private const string csMozillaNotFound = "mozilla not found";
private static string sMozMapi;
[DllImport("kernel32.dll")]
public static extern IntPtr LoadLibrary(string dllToLoad);
[DllImport("kernel32.dll")]
public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
[DllImport("kernel32.dll")]
public static extern bool FreeLibrary(IntPtr hModule);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate int MAPISendMail(IntPtr sess, IntPtr hwnd, MapiMessage message, int flg, int rsv);
public static bool IsInstalled
{
get
{
if (string.IsNullOrEmpty(sMozMapi))
{
string sPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + "\\Mozilla Thunderbird\\mozMapi32.dll";
if (File.Exists(sPath))
sMozMapi = sPath;
else
sMozMapi = csMozillaNotFound;
}
if (sMozMapi == csMozillaNotFound)
return false;
return true;
}
}
public static int MozMAPISendMail(IntPtr sess, IntPtr hwnd, MapiMessage message, int flg, int rsv)
{
if (Mozilla.IsInstalled)
{
IntPtr pDll = LoadLibrary(sMozMapi);
if (pDll == IntPtr.Zero)
return 0;
IntPtr pAddressOfFunctionToCall = GetProcAddress(pDll, "MAPISendMail");
if (pAddressOfFunctionToCall == IntPtr.Zero)
return 0;
MAPISendMail mAPISendMail = (MAPISendMail)Marshal.GetDelegateForFunctionPointer(
pAddressOfFunctionToCall,
typeof(MAPISendMail));
int res = mAPISendMail(sess, hwnd, message, flg, rsv);
FreeLibrary(pDll);
}
return 0;
}
}
uint SendMail(string strSubject, string strBody, int how)
{
MapiMessage msg = new MapiMessage();
msg.subject = strSubject;
msg.noteText = strBody;
msg.recips = GetRecipients(out msg.recipCount);
msg.files = GetAttachments(out msg.fileCount);
m_lastError = (uint) MAPISendMail(new IntPtr(0), new IntPtr(0), msg, how, 0);
if (m_lastError > 1)
{
if (Mozilla.IsInstalled)
m_lastError = (uint)Mozilla.MozMAPISendMail(new IntPtr(0), new IntPtr(0), msg, how, 0);
if (m_lastError > 1)
MessageBox.Show("MAPISendMail failed! " + GetLastError());
}
Cleanup(ref msg);
return m_lastError;
}
|
|
|
|

|
Very nice code
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
|
A technique for programmatically adding attachments to emails in C# and VB.NET.
| Type | Article |
| Licence | CPOL |
| First Posted | 8 Feb 2007 |
| Views | 215,719 |
| Downloads | 3,835 |
| Bookmarked | 123 times |
|
|