#include "stdafx.h"
#include "easendmailobj.tlh"
using namespace EASendMailObjLib;
int _tmain(int argc, _TCHAR* argv[])
{
::CoInitialize( NULL );
IMailPtr oSmtp = NULL;
oSmtp.CreateInstance( "EASendMailObj.Mail");
oSmtp->LicenseCode = _T("TryIt");
// Set your sender email address
oSmtp->FromAddr = _T("test@emailarchitect.net");
// Add recipient email address
oSmtp->AddRecipientEx( _T("support@emailarchitect.net"), 0 );
// Set email subject
oSmtp->Subject = _T("simple email from Visual C++ project");
// Set email body
oSmtp->BodyText = _T("this is a test email sent from Visual C++ project, do not reply");
// Your SMTP server address
oSmtp->ServerAddr = _T("smtp.emailarchitect.net");
// User and password for ESMTP authentication, if your server doesn't
// require User authentication, please remove the following codes.
oSmtp->UserName = _T("test@emailarchitect.net");
oSmtp->Password = _T("testpassword");
// If your smtp server requires SSL connection, please add this line
// oSmtp->SSL_init();
_tprintf(_T("Start to send email ...\r\n" ));
if( oSmtp->SendMail() == 0 )
{
_tprintf( _T("email was sent successfully!\r\n"));
}
else
{
_tprintf( _T("failed to send email with the following error: %s\r\n"),
(const TCHAR*)oSmtp->GetLastErrDescription());
}
if( oSmtp != NULL )
oSmtp.Release();
return 0;
}
But this program uses a win32 console application to run properly; I tested it with my email and phone number and other required info and it works. Yet I need to get it to work for a Windows forms application, and simply pasting the code inside an action event (properly implementing the tlh and tli files it requires and installing easendmail, and the directives for easendmailobj.tlh and namespace) gave me the error:
fatal error C1189: #error : comdef h header cannot be included under clr safe or clr pure
So I followed the advice in this page ( http://social.msdn.m...b0-79e4cf35c5ff ) and went to Properties->Common Language Runtime Support, and changed it to (/clr). I didn't get C1189 anymore, but I still had to convert the System strings to standard strings, so I used the code from this page ( http://stackoverflow...ng-to-stdstring , the one that has 20 points) to convert, then wrote down the code to send emails in a header file:
#pragma once
#include <string>
#include <iostream>
#include <sstream>
#include <tchar.h>
#include "easendmailobj.tlh"
using namespace EASendMailObjLib;
using namespace std;
class TextMessage
{
private:
string email;
string pass;
string phone;
string subject;
string message;
string smtp;
public:
TextMessage::TextMessage(string email, string pass, string phone, string subject, string message, string smtp)
{
this->email = email;
this->pass = pass;
this->phone = phone;
this->subject = subject;
this->message = message;
this->smtp = smtp;
}
string send()
{
::CoInitialize( NULL );
IMailPtr oSmtp = NULL;
oSmtp.CreateInstance( "EASendMailObj.Mail");
oSmtp->LicenseCode = _T("TryIt");
// Set your sender email address
oSmtp->FromAddr = email.c_str();
// Add recipient email address
oSmtp->AddRecipientEx(phone.c_str(), 0 );
// Set email subject
oSmtp->Subject = subject.c_str();
// Set email body
oSmtp->BodyText = message.c_str();
// Your SMTP server address
oSmtp->ServerAddr = smtp.c_str();
// User and password for ESMTP authentication, if your server doesn't
// require User authentication, please remove the following codes.
oSmtp->UserName = email.c_str();
oSmtp->Password = pass.c_str();
// If your smtp server requires SSL connection, please add this line
// oSmtp->SSL_init();
//_tprintf(_T("Start to send email ...\r\n" ));
if( oSmtp->SendMail() == 0 )
{
return "Email was sent successfully!\n";//_tprintf( _T("email was sent successfully!\r\n"));
}
else
{
stringstream s;
s << "Failed to send email with the following error:\n\n";
s << (const TCHAR*)oSmtp->GetLastErrDescription();
/*_tprintf( _T("failed to send email with the following error: %s\r\n"),
(const TCHAR*)oSmtp->GetLastErrDescription());*/
string ss = s.str();
return ss;
}
if( oSmtp != NULL )
oSmtp.Release();
//return 0;
}
TextMessage(void){}
~TextMessage(void){}
};
And added #include <tchar.h> because for some reason _T() was not recognized. So the difference between
oSmtp->FromAddr = email.c_str();
and
oSmtp->FromAddr = _T("test@emailarchitect.net");
is that I couldn't conceal the string email inside _T(email) because I got
error C2065: 'Lemail' : undeclared identifier
so to use the email string and all the others without code errors I wrote it as
oSmtp->FromAddr = email;
but then I got this error:
error C2664: 'EASendMailObjLib::IMail::PutFromAddr' : cannot convert parameter 1 from 'std::string' to '_bstr_t'
so I instead made email a constant string (email.c_str()) and I got no errors there. However as I run the program, I get the following:
Debug Assertion Failed! Program: ...nal\TeenageTextingAssistant\Debug\TeenageTextingAssistant.exe File f:\dd\vctools\crt_bld\self_x86\crt\src\dbgheap.c Line: 1516 Expression: _CrtIsValidHeapPointer(pUserData)
The line it refers to is this:
_ASSERTE(_CrtIsValidHeapPointer(pUserData));
I went to check the output of the program, and i get:
'TeenageTextingAssistant.exe': Loaded 'E:\Advanced C++\Final\TeenageTextingAssistant\Debug\TeenageTextingAssistant.exe', Symbols loaded. 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\SysWOW64\mscoree.dll', Cannot find or open the PDB file 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded. 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\SysWOW64\msvcp100d.dll', Symbols loaded. 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\SysWOW64\ws2_32.dll', Cannot find or open the PDB file 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\SysWOW64\msvcrt.dll', Cannot find or open the PDB file 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\SysWOW64\rpcrt4.dll', Cannot find or open the PDB file 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\SysWOW64\sspicli.dll', Cannot find or open the PDB file 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\SysWOW64\cryptbase.dll', Cannot find or open the PDB file 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\SysWOW64\sechost.dll', Cannot find or open the PDB file 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\SysWOW64\nsi.dll', Cannot find or open the PDB file 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\SysWOW64\winmm.dll', Cannot find or open the PDB file 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\SysWOW64\user32.dll', Cannot find or open the PDB file 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\SysWOW64\gdi32.dll', Cannot find or open the PDB file 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\SysWOW64\lpk.dll', Cannot find or open the PDB file 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\SysWOW64\usp10.dll', Cannot find or open the PDB file 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\SysWOW64\advapi32.dll', Cannot find or open the PDB file 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\SysWOW64\ole32.dll', Cannot find or open the PDB file 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\SysWOW64\oleaut32.dll', Cannot find or open the PDB file 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\SysWOW64\imm32.dll', Cannot find or open the PDB file 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\SysWOW64\msctf.dll', Cannot find or open the PDB file 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscoreei.dll', Cannot find or open the PDB file 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\SysWOW64\shlwapi.dll', Cannot find or open the PDB file 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll', Cannot find or open the PDB file 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\SysWOW64\msvcr100_clr0400.dll', Cannot find or open the PDB file 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\assembly\NativeImages_v4.0.30319_32\mscorlib\16126cae96ea2422253ae06eeb672abc\mscorlib.ni.dll', Cannot find or open the PDB file 'TeenageTextingAssistant.exe' (Managed (v4.0.30319)): Loaded 'C:\windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\SysWOW64\uxtheme.dll', Cannot find or open the PDB file 'TeenageTextingAssistant.exe': Loaded 'C:\Program Files (x86)\Lenovo\Onekey Theater\ActiveDetect32.dll', Cannot find or open the PDB file 'TeenageTextingAssistant.exe': Loaded 'C:\Program Files (x86)\Lenovo\Onekey Theater\WindowsApiHookDll32.dll', Cannot find or open the PDB file 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\SysWOW64\version.dll', Cannot find or open the PDB file The thread 'Win32 Thread' (0x1080) has exited with code 0 (0x0). 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\nlssorting.dll', Cannot find or open the PDB file 'TeenageTextingAssistant.exe' (Managed (v4.0.30319)): Loaded 'E:\Advanced C++\Final\TeenageTextingAssistant\Debug\TeenageTextingAssistant.exe', Symbols loaded. 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\clrjit.dll', Cannot find or open the PDB file 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\diasymreader.dll', Cannot find or open the PDB file 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\assembly\NativeImages_v4.0.30319_32\System\811a7bc79f8f0a5be8065292a320819e\System.ni.dll', Cannot find or open the PDB file 'TeenageTextingAssistant.exe' (Managed (v4.0.30319)): Loaded 'C:\windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. First-chance exception at 0x77871985 in TeenageTextingAssistant.exe: 0xC0000005: Access violation reading location 0xe29652a7. 'TeenageTextingAssistant.exe': Loaded 'C:\Windows\SysWOW64\dwmapi.dll', Cannot find or open the PDB file First-chance exception at 0x59447009 (msvcr100d.dll) in TeenageTextingAssistant.exe: 0xC0000005: Access violation reading location 0xe29652bc. First-chance exception at 0x753dc41f in TeenageTextingAssistant.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000.. An unhandled exception of type 'System.TypeInitializationException' occurred in Unknown Module. Additional information: The type initializer for '<Module>' threw an exception. An unhandled exception of type 'System.TypeInitializationException' occurred in Unknown Module. Additional information: The type initializer for '<Module>' threw an exception. The thread 'Win32 Thread' (0x1040) has exited with code 0 (0x0). The thread 'Win32 Thread' (0xcac) has exited with code 0 (0x0). The thread 'Win32 Thread' (0x1088) has exited with code 0 (0x0). The program '[3116] TeenageTextingAssistant.exe: Managed (v4.0.30319)' has exited with code 0 (0x0). The program '[3116] TeenageTextingAssistant.exe: Native' has exited with code 0 (0x0).
While when I try to build the solution, I get:
1>------ Rebuild All started: Project: TeenageTextingAssistant, Configuration: Debug Win32 ------ 1> stdafx.cpp 1> AssemblyInfo.cpp 1> TeenageTextingAssistant.cpp 1> Generating Code... 1> .NETFramework,Version=v4.0.AssemblyAttributes.cpp 1> TeenageTextingAssistant.vcxproj -> E:\Advanced C++\Final\TeenageTextingAssistant\Debug\TeenageTextingAssistant.exe ========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
Is it normal for the program not to find the pdb files? Also, the code I use to call the TextMessage header file is:
string email, pass, phone, smtp, mess; msclr::interop::marshal_context context; email = context.marshal_as<std::string>(yemtbox->Text); pass = context.marshal_as<std::string>(passtbox->Text); smtp = context.marshal_as<std::string>(smtps[emprovcbox->SelectedIndex]); String^ sphone = /*L"" +*/ phonetbox->Text + "@" + smsgs[carcbox->SelectedIndex]; phone = context.marshal_as<std::string>(sphone); mess = context.marshal_as<std::string>(msbox->Text); TextMessage tm(email, pass, phone, "TTA sent message", mess, smtp); stringstream s; s << tm.send(); string ss = s.str(); String^ sss = gcnew String(ss.c_str()); textLabel->Text = L"" + sss;
Sorry if it's long, but I wanted to explain everything I did in case there's a problem somewhere. I've searched and there's supposed to be another way of sending emails in a windows form project ( http://msdn.microsof...smtpclient.aspx ), yet I hardly understand the code they give, or what directives should be used. Am I doing this the right way or is all the code I posted above too redundant or unnecessary? If so what is a simpler way to send emails through a Windows forms application? Thank you for any replies in advance
This post has been edited by gfcf14: 04 December 2012 - 05:24 PM

New Topic/Question
Reply



MultiQuote




|