|
Hi.I write the code followed and want to put message to inbox. i have installed Outlook in my computer which is configured to exchenge server in the other machine. I want to see my message in Exchange server before send receive in my local Outlook in my case it is not like that message i get the message but only after manual send receive. Please help me to understand what is wrong i am new in MAPI. Thank you.
#include "MailGen.h"
enum { _ENTRYID, _DISPLAY_NAME, _FINDER_ENTRYID, _SUBFOLDERS };
MailGen::MailGen(void)
{
MAPIInitialize(0);
}
MailGen::~MailGen(void)
{
MAPIUninitialize();
}
BOOL MailGen::AddMailToInbox(CString strProfileName)
{
LPMAPISESSION pSession;
LPMAPIFOLDER lpProcessedFolder;
ULONG cbDefStoreEid = 0;
LPENTRYID pDefStoreEid = NULL;
ULONG ulValues;
LPSPropValue pPropValues = NULL;
CString csSearchedMBox;
LPMDB pDefMsgStore = NULL;
LPSPropValue lpRowProp = NULL;
LPSPropValue pVal = NULL;
LPMESSAGE m_pMessage;
try
{
CHECKERR(MAPILogonEx(0, // Handle to parent window
(LPTSTR)(LPCTSTR)strProfileName, // Profile name
NULL, // Password
MAPI_EXTENDED | MAPI_USE_DEFAULT | MAPI_NEW_SESSION,
// MAPI_PASSWORD_UI, // Logon flags
&pSession),"Obtain MAPI session - MAPILogonEx");
LPMAPITABLE lptMsgStores = NULL;
// Obtain a table of message stores from the session.
CHECKERR( pSession->GetMsgStoresTable(NULL, &lptMsgStores), "Obtain a table of message stores from the session - lpSession->GetMsgStoresTable" );
// Look for the default store. If you are not online,
// this is located in the Offline folder.
SizedSPropTagArray(2, tagStores) =
{
2, PR_DEFAULT_STORE, PR_ENTRYID
};
LPSRowSet pRowSetStores = 0;
SRestriction sRes;
SPropValue propStore;
propStore.ulPropTag = PR_DEFAULT_STORE;
propStore.Value.b = TRUE;
propStore.dwAlignPad = 0;
sRes.rt = RES_PROPERTY;
sRes.res.resProperty.relop = RELOP_EQ;
sRes.res.resProperty.ulPropTag = PR_DEFAULT_STORE;
sRes.res.resProperty.lpProp = &propStore;
CHECKERR( HrQueryAllRows(lptMsgStores,
(LPSPropTagArray)&tagStores,
&sRes,
0,
0,
&pRowSetStores),
"Get default message store - HrQueryAllRows");
if (pRowSetStores->cRows > 0)
{
LPSPropValue pPropStore = pRowSetStores->aRow[0].lpProps;
LPMDB pMDBDefault = 0;
//lpRowProp = pPropStore->aRow[0].lpProps;
CHECKERR( pSession->OpenMsgStore(0,
pPropStore[1].Value.bin.cb,
(LPENTRYID)pPropStore[1].Value.bin.lpb,
0,
MDB_NO_DIALOG| MAPI_BEST_ACCESS | MAPI_DEFERRED_ERRORS,
&pDefMsgStore), "Open the message store - pSession->OpenMsgStore");
CHECKERR( pDefMsgStore->GetProps(NULL,
0,
&ulValues,
&pPropValues), "pDefMsgStore->GetProps");
CHECKERR( HrGetOneProp( pDefMsgStore, PR_IPM_SUBTREE_ENTRYID, &pVal ), "HrGetOneProp - PR_IPM_SUBTREE_ENTRYID" );
//ULONG ulObjType = 0;
//CHECKERR( pDefMsgStore->OpenEntry( pVal->Value.bin.cb,
// (LPENTRYID)pVal->Value.bin.lpb,
// NULL, //We want the default interface (IMAPIFolder)
// MAPI_BEST_ACCESS, //Flags
// &ulObjType, //Object returned type
// (LPUNKNOWN *) &lpProcessedFolder), "lpMDB->OpenEntry"); //Returned folder
lpProcessedFolder = OpenInbox(pDefMsgStore);
//CHECKERR(lpProcessedFolder->CreateMessage(NULL,0,&m_pMessage),"inbox->CreateMessage()");
m_pMessage = CreateMessag(lpProcessedFolder,"FROMNAME","fromname@yahoo.com","Test Subject","Test Body");
SPropValue prop;
prop.ulPropTag=PR_MESSAGE_FLAGS;
prop.Value.l=MSGFLAG_UNSENT | MSGFLAG_FROMME;
CHECKERR(m_pMessage->SetProps(1,&prop,NULL),"m_pMessage->SetProps()");
m_pMessage->SaveChanges(FORCE_SAVE);
pSession->Logoff(NULL,0,0);
/*csSearchedMBox = "Mailbox - ";
csSearchedMBox += pPropValues[1].Value.lpszA;*/
}
}
catch( std::string& str )
{
MessageBox(NULL,str.c_str(),NULL, MB_OK | MB_ICONSTOP);
return false;
}
if (pPropValues)
MAPIFreeBuffer(pPropValues);
if (pDefMsgStore)
{
ULONG uflags = LOGOFF_NO_WAIT;
pDefMsgStore->StoreLogoff(&uflags);
pDefMsgStore->Release();
}
if (lpProcessedFolder)
{
lpProcessedFolder->Release();
}
if (pDefStoreEid)
MAPIFreeBuffer(pDefStoreEid);
if (pSession)
{
pSession->Logoff(0, MAPI_LOGOFF_SHARED, 0);
pSession->Release();
}
return true;
}
LPMESSAGE MailGen::CreateMessag(LPMAPIFOLDER folder,CString fromName,CString fromEMail,CString subject,CString body)
{
LPMESSAGE m_pMessage;
CHECKERR(folder->CreateMessage(NULL,0,&m_pMessage),"inbox->CreateMessage()");
set_SenderName(m_pMessage,fromName);
set_SenderEmail(m_pMessage,fromEMail);
set_Subject(m_pMessage,subject);
set_Body(m_pMessage,body);
return m_pMessage;
}
void MailGen::CHECKERR( HRESULT hr, const char* msg )
{
if( hr != S_OK ){
char buf[11];
itoa( hr, buf, 16 );
std::string err("FAILED with error - ");
err += buf;
err += ", ";
err += msg ? msg : "";
throw err;
}
}
LPMAPIFOLDER MailGen::OpenInbox(LPMDB m_pMsgStore)
{
if(!m_pMsgStore) return NULL;
ULONG cbEntryID;
LPENTRYID pEntryID;
DWORD dwObjType;
LPMAPIFOLDER pFolder;
CHECKERR(m_pMsgStore->GetReceiveFolder(NULL,0,&cbEntryID,&pEntryID,NULL),"m_pMsgStore->GetReceiveFolder()");
m_pMsgStore->OpenEntry(cbEntryID,pEntryID, NULL, MAPI_BEST_ACCESS ,&dwObjType,(LPUNKNOWN*)&pFolder);
MAPIFreeBuffer(pEntryID);
return pFolder;
}
void MailGen::set_Subject(LPMESSAGE m_pMapiMessage,LPCTSTR subject)
{
CString strSubject = subject;
if(strSubject.GetLength()>0 && m_pMapiMessage) {
SPropValue prop;
prop.ulPropTag=PR_SUBJECT;
prop.Value.LPSZ=(TCHAR*)subject;
m_pMapiMessage->SetProps(1,&prop,NULL);
}
}
void MailGen::set_SenderName(LPMESSAGE m_pMapiMessage,LPCTSTR senderName)
{
CString strSenderName=senderName;
if(strSenderName.GetLength() > 0 && m_pMapiMessage) {
SPropValue prop;
prop.ulPropTag=PR_SENDER_NAME;
prop.Value.LPSZ=(TCHAR*)senderName;
m_pMapiMessage->SetProps(1,&prop,NULL);
}
}
void MailGen::set_SenderEmail(LPMESSAGE m_pMapiMessage,LPCTSTR senderEmail)
{
CString strSenderEmail=senderEmail;
if(strSenderEmail.GetLength()>0 && m_pMapiMessage) {
SPropValue prop;
prop.ulPropTag=PR_SENDER_EMAIL_ADDRESS;
prop.Value.LPSZ=(TCHAR*)senderEmail;
m_pMapiMessage->SetProps(1,&prop,NULL);
}
}
void MailGen::set_Body(LPMESSAGE m_pMapiMessage,LPCTSTR body)
{
CString strBody=body;
if(strBody.GetLength()>0 && body) {
LPSTREAM pStream=NULL;
if(m_pMapiMessage->OpenProperty(PR_BODY, &IID_IStream, 0, MAPI_MODIFY | MAPI_CREATE, (LPUNKNOWN*)&pStream)==S_OK) {
pStream->Write(body,(ULONG)(_tcslen(body)+1)*sizeof(TCHAR),NULL);
pStream->Release();
pStream = NULL;
}
}
}
|
|
|
|
|
Hi,
I would like to show an automatically generated Email to the user before sending it.
So I set the MAPISendMail(...) Flag to MapiDialog (0x8).
This works fine for Thunderbird as default Mail client.
Outlook Express complains: "Mindestens ein Teil dieser Nachricht konnte nicht angezeigt werden."
this should be in english: "At least on part of the message could not be shown."
Setting the flag to 0, the Mail is sent without problems..... but I need the dialog to be shown.
Any ideas how to fix this?
thanks a lot
Dan
|
|
|
|
|
Hy,
here is my code example:
Where is here my error?
if (mapi.Logon(IntPtr.Zero) == false)
{
......
}
string mailAdress = "SMTP:a@b.de";
mapi.AddRecip(mailAdress, null, false);
if (mapi.Send(subject,message))
{
Console.WriteLine("Erfolgreich gesendet");
}
else
{
Console.WriteLine("Error: {0}", mapi.Error());
}
|
|
|
|
|
I can open a new message with an attachment in Outlook 2003 and Thunderbird, but not in Outlook 2007, where it opens a new mail message with no attachments. Any ideas how to get it to work in Outlook 2007? I know it's possible since Adobe Acrobat Reader can still attach files in emails with one click.
I am using this simple code from the article:
Win32Mapi.Mapi ma = new Win32Mapi.Mapi();
ma.Logon(IntPtr.Zero);
ma.Attach(@"c:\temp.txt");
ma.Send("Subjectheading", "Bodytext", false);
ma.Logoff();
|
|
|
|
|
I'm using part of this class to send an automatically generated mail message. I want the application to pop up the mail form (MAPIDialog). This works great as long as I use it from the primary form. However, when I use it from a secondary form (that is click a button on the primary form to pop up a second form and then click another button to send the mail), I get my mail form to pop up but the mail form has an hourglass cursor and it appears to be hung.
Any ideas why this might happen?
Thanks.
|
|
|
|
|
I also had this problem and the only workaround i found was to hide the form prior to calling MAPI.
ie,
try
{
// workaround a MAPI bug:
// hide the window, otherwise MAPI calls below hang with a wait cursor (hourglass)
Visible = false;
// ... call MAPI ...
}
finally
{
Visible = true; // restore visibility
}
|
|
|
|
|
Just what i was looking for, with a little extra as well.
Thanks very much.
ben
|
|
|
|
|
Thanks for this article. Well written and provides a very useful class. I am using it in one of my projects, with your name intact on the class documentation . I did document the rest of your code though ..... lol
|
|
|
|
|
Anybody know how this approach for accessing Outlook emails compares with the following:
http://www.codeproject.com/useritems/extract_oulook_2003_NET.asp?forumid=280510&select=1439980&df=100&msg=1439980#xx1439978xx
Not sure if they are equivlent in functionalities or not. I also posted a similar question in the comment section of that article.
Thanks a lot!
|
|
|
|
|
I am using MS Outlook and XP. Send mail works fine (the user gets a dialog). But if the user cancels the dialog, from close button or otherwise, and Outlook is running, Outlook hangs with send/receive error message. Needs to restart Outlook
kperwaiz
|
|
|
|
|
Change the send code to the following. If you do not log off and let the object go out of scope you have the issue you are talking about.
try
{
ma.AddRecip(to, null, false);
if (!ma.Send(subject, body))
{
MessageBox.Show("MAPISendMail failed! : " + ma.Error());
return;
}
MessageBox.Show("SimpleMAPICon: email sent successfully.");
}
finally
{
ma.Logoff();
}
|
|
|
|
|
To start off, great code! It works very well. In my application I'm using the Outlook COM object and am able to send emails as well. But I get the infamous Outlook security popup foreach email that is sent out. The message states "A program is trying to automatically send e-mail on your behalf. Do you want to allow this?" Then you have to manually select "Yes" or "No". The above code trigers this popup as well and I was wondering if anyone figured out how to avoid this popup.
Thanks.
David
www.dpatterson.net
|
|
|
|
|
This Microsoft article may help, see Scenario 2:
http://support.microsoft.com/kb/290499/EN-US/[^]
Ant.
I'm hard, yet soft. I'm coloured, yet clear. I'm fruity and sweet. I'm jelly, what am I? Muse on it further, I shall return! - David Walliams (Little Britain)
|
|
|
|
|
David, it seems that u have solved the solution out of the link http://support.microsoft.com/kb/290499/EN-US/[^] refered by Ant.
Can u plz brief it here?
|
|
|
|
|
Requiring an Exchange server isn't acceptable in every situation. I've been looking around for this all day. According to
this, you can write a COM object inside Outlook.
I opt to just use the free tool ClickYes. Although this brings back the security problem that brought the crazy dialog, it fixes the problem in our simple systems. Use it at your own risk...
|
|
|
|
|
Hi,
Thanx for the Great article.
It seems that there are security issues with the app.
when I start the app, I get a messagae from Microsoft Outlook..
Saying that "A program is trying to access the email addresses you have stored in the outlook. Do you want to allow this ?"...
How do we stop this message from coming ???
And what are the security isssues if any ???
Any help will be great.
Regards,
Saleem
|
|
|
|
|
This is a great article and put me on the path to my solution, which was implementing a "send to" in my vb.net app.
It's missing one very easy MAPI function which is perfect for this purpose. So if that's what you're browsing for:
<DllImport("MAPI32.DLL")> _
Public Shared Function MAPISendDocuments(ByVal ulUIParam As IntPtr, ByVal lpszDelimChar As String, _
ByVal lpszFullPaths As String, ByVal lpszFileNames As String, ByVal rsv As Integer) As Integer
End Function
Private Class Mailer
Public filename As String
Public Sub Start()
MAPISendDocuments(IntPtr.Zero, ";", Me.filename, "", 0)
End Sub
End Class
Public Sub SendEmail()
'Mail the file on a new thread so the mail dialog won't be modal to our application
Dim m As New Mailer
m.filename = "C:\yourfile.txt"
Dim t As Threading.Thread
t = New Threading.Thread(AddressOf m.Start)
t.Start()
End Sub
The first argument to the API function is your window handle, and a zero is supposed to cause the dialog to be modeless. There is a similar scheme for the MAPISendMail() function. For me it was always modal, regarless of the value of this argument, so I just started a new thread. If you want a modal mail window, ignore the threading stuff and call the function directly.
|
|
|
|
|
Is it possible to set ContentType to HTML?
|
|
|
|
|
The sample is fantastic but when I try to send an email without Outlook being open, I get the error message:
"Failed to send email: MAPI login failure [3]".
Is the the expected behaviour? Can I get it to send without a client being open?
Maybe I need to check if a client is open first, then advise the user to start their client before sending?
TIA
|
|
|
|
|
In my application, I wanted to have it bring up a new mail message instead of just automatically sending the message, kind of like what you get when you click on a file in Windows and do Send To / Mail Recipient. You can modify the code to do something like this:
public bool Send( string sub, string txt, bool sendAutomatically )
{
lastMsg = new MapiMessage();
lastMsg.subject = sub;
lastMsg.noteText = txt;
// set pointers
lastMsg.originator = AllocOrigin();
lastMsg.recips = AllocRecips( out lastMsg.recipCount );
lastMsg.files = AllocAttachs( out lastMsg.fileCount );
int flags = (sendAutomatically ? 0 : MapiDialog);
error = MAPISendMail( session, winhandle, lastMsg, flags, 0 );
Dealloc();
Reset();
return error == 0;
}
|
|
|
|
|
Sorry... you need this too.
private const int MapiDialog = 0x00000008;
|
|
|
|
|
Do you know how to make the appearing mail dialog modeless? I do the same things you described and a message window blocks my application which I don't need.
|
|
|
|
|
Put it in a separate thread.
|
|
|
|
|
Hi,
I am using SimpleMAPIdotNET to send an email. But I can't send any mail and
1 - an error occurs in SimpleMAPIdotNET project. I don't know, how I can do. Can anybody help me.
My System: windows XP sp1, Outlook 2000.
Where:
File: MainForm.cs
Method: public bool Next( ref MailEnvelop env )
Line: env.date = DateTime.ParseExact( lastMsg.dateReceived, "yyyy/MM/dd HH:mm", DateTimeFormatInfo.InvariantInfo );
Message:
An unhandled exception of type 'System.ArgumentNullException' occurred in mscorlib.dll
Additional information: String reference not set to an instance of a String.
2 - In my project I use the SendForm and classes of "simple MAPI NET" to send an email. MAPI object is a local object in class SendForm. The Logon method returns false (error).
public class SendForm : System.Windows.Forms.Form
{
private Mapi ma new Mapi();
private void buttonSend_Click( object sender, System.EventArgs e )
{
ma.Logon( this.Handle);
if( (txtTO.Text == null) || (txtSubject.Text == null) )
return;
if( (txtTO.Text.Length == 0) || (txtSubject.Text.Length == 0) )
return;
ma.AddRecip( txtTO.Text, null, false );
if( textCC.Text != null )
{
if( textCC.Text.Length > 0 )
ma.AddRecip( textCC.Text, null, true );
}
if( ! ma.Send( txtSubject.Text, txtBody.Text ) )
MessageBox.Show( this, "MAPISendMail failed! "
+ ma.Error(), "Send Mail", MessageBoxButtons.OK,
MessageBoxIcon.Warning ); //
ma.Reset();
this.Close();
}
}
Thanks
TNL
|
|
|
|
|
I have successfully used this code in a win forms app, however if I include an App.config into the project, then it fails. I have tested this out on the source code, and it works fine, so I'm a bit lost as to why having an App.config file in my project will cause the mail send to fail.
My code looks like this:
if (!mapi.Send(subject, body.ToString()))
MessageBox.Show( this, "MAPISendMail failed! " + mapi.Error(), "Send Mail", MessageBoxButtons.OK, MessageBoxIcon.Warning );
else
success = true;
mapi.send returns true when not app.config file is present, but false when one is. Even an empty App.config causes the failure, and even if I have no appSettingsReader class present.
Any similar experiences?
Peter
|
|
|
|
|