hy, can somevone tell me how to send email using smtp server in c++????
please...
Printable View
hy, can somevone tell me how to send email using smtp server in c++????
please...
i would like to add to the question. i have a yahoo account.
how to write a program in C++ that can send e-mails using that Yahoo account?
i found this:
but it doens't work...where is problem?Code:#define _CRTDBG_MAP_ALLOC
#include <windows.h>
#include <stdio.h>
#include <crtdbg.h>
#include "C:/ck2000/components/ChilkatLib/Package/Include/CkSettings.h"
#include "C:/ck2000/components/ChilkatLib/Package/Include/CkEmail.h"
#include "C:/ck2000/components/ChilkatLib/Package/Include/CkEmailBundle.h"
#include "C:/ck2000/components/ChilkatLib/Package/Include/CkMailMan.h"
#include "C:/ck2000/components/ChilkatLib/Package/Include/CkString.h"
#include "C:/ck2000/components/ChilkatLib/Package/Include/CkByteData.h"
void EmailExample(void)
{
CkMailMan mailman;
// Any string passed to UnlockComponent begins the 30-day trial.
bool unlocked = mailman.UnlockComponent("30-day trial");
if (!unlocked)
{
printf("Failed to unlock component\n");
return;
}
mailman.put_SmtpHost("mail.earthlink.net");
// Send an email with a file attachment.
CkEmail email;
email.put_Body("This is the body");
email.AddTo("Chilkat Support","[email protected]");
email.put_FromAddress("[email protected]");
email.put_Subject("This is a test");
// Add a file attachment.
if (!email.AddFileAttachment("dudeCPP.gif",0))
{
email.SaveLastError("errors.xml");
return;
}
// Send the email
if (!mailman.SendEmail(&email))
{
mailman.SaveLastError("errors.xml");
}
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
freopen("stdout.txt","w",stdout);
EmailExample();
CkSettings::cleanupMemory();
_CrtDumpMemoryLeaks();
return 0;
}
How the heck are we supposed to know!? Describe the problem and post any compile errors and where (on what line) they are.
Did you also find the ChilkatLib that it appears to reference?
--
Mats
if found all error except one...
crtdbg.h - it is missing, how can i install that?
Obviously you need to download the API ...
hm...i didn't find link to download!
can you give me link?
crtdbg.h is part of Visual Studio tools, and I suspect you can actually live well without that, if you just remove the functions that uses it, which is particular "_CrtDumpMemoryLeaks();".
--
Mats
Download this: http://sourceforge.net/projects/poco, read the documentation, and use it. POCO will be very usefull to you, since it adds some other API´s to work with XML, Net, etc... Chilkat isn´t nice, it is payed and i don´t like it...
it doens't work....program start...and email doesn't came!
Junk classes. Use the pure!
Code:// Yuri Zarechny for News2News (c) 2006
#include "stdafx.h"
//#include < windows.h >
#include < winsock2.h > //link ws2_32.lib
#include "conio.h"
struct _sendmail {
LPSTR host;
LPSTR IP;
LPSTR sender;
LPSTR recip;
LPSTR subject;
LPSTR body;
SOCKET hSocket;
} sendmail;
BOOL InitSock(LPSTR Server)
{
WSADATA wsd;
if (WSAStartup(MAKEWORD(2,2), &wsd) > 0)
{
printf("Winsock init error: %d\n", WSAGetLastError());
return false;
}
return true;
}
LPSTR GetIP(LPSTR h)
{
in_addr ia;
hostent* hbn;
unsigned int addr;
if (isalpha(h[0])) hbn = gethostbyname(h);
else
{
addr = inet_addr(h);
hbn = gethostbyaddr((char *)&addr, 4, AF_INET);
}
if (hbn==0) return "";
ia.S_un.S_addr=*(DWORD *)hbn->h_addr_list[0];
return inet_ntoa(ia);
}
BOOL ValidateEnvelope(LPSTR h, LPSTR r, LPSTR s, LPSTR *ip)
{
if (h=="" || r=="" || s=="")
{
printf("Invalid host or sender/recipient email address\n");
return false;
}
*ip=GetIP(h);
if (*ip=="")
{
printf("Can not resolve host name %s to ip address\n", h);
return false;
}
return true;
}
BOOL ConnectTo()
{
const u_short SMTP_PORT=25;
sockaddr_in sa;
sa.sin_family=AF_INET;
sa.sin_addr.S_un.S_addr=inet_addr(sendmail.IP);
sa.sin_port=htons(SMTP_PORT);
return (connect(sendmail.hSocket, (SOCKADDR *)&sa, sizeof(sa))==0);
}
BOOL snd(LPSTR txt, bool resp)
{
char tx[4096];
sprintf(tx, "%s\r\n", txt);
if (send(sendmail.hSocket, tx, strlen(tx),0)==SOCKET_ERROR)
{
printf("%s%s\n", tx, " - send operation failed.");
return false;
}
if (!resp) return true;
while (true)
{
char rcv[1024];
char outp[255]="";
HANDLE hEvent=WSACreateEvent();
WSAEventSelect(sendmail.hSocket,hEvent,FD_READ);
DWORD nWait=WSAWaitForMultipleEvents(1, &hEvent, 0, 1000, 0);
WSACloseEvent(hEvent);
if (nWait!=0) return false;
recv(sendmail.hSocket, rcv, sizeof(rcv),0);
strncpy(outp, rcv, strcspn(rcv, "\n")+1);
printf("%s\n", outp);
}
return true;
}
BOOL SendMail()
{
if (!ValidateEnvelope(sendmail.host, sendmail.recip,
sendmail.sender, &sendmail.IP)) return false;
char tmp[255];
if (sendmail.sender=="" || sendmail.recip=="" ||
sendmail.body=="" || sendmail.subject=="") return false;
sendmail.hSocket=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sendmail.hSocket==SOCKET_ERROR) return false;
if (!ConnectTo())
{
printf("Unable to connect to the server.\n");
return false;
}
snd("HELO", true);
sprintf(tmp, "%s%s%s", "MAIL FROM:<", sendmail.sender, ">");
snd(tmp, true);
sprintf(tmp, "%s%s%s", "RCPT TO:<", sendmail.recip, ">");
snd(tmp, true);
snd("DATA", true);
sprintf(tmp, "%s%s", "From: ", sendmail.sender);
snd(tmp, false);
sprintf(tmp, "%s%s", "To: ", sendmail.recip);
snd(tmp, false);
sprintf(tmp, "%s%s", "Subject: ", sendmail.subject);
snd(tmp, false);
snd("", false);
snd(sendmail.body ,false);
snd(".", true);
snd("NOOP", true);
snd("QUIT", true);
closesocket(sendmail.hSocket);
return true;
}
void _tmain() {
// provide valid host name and email addresses
sendmail.host = "smtp.myisp.ca";
sendmail.sender = "[email protected]";
sendmail.recip = "[email protected]";
sendmail.subject = "Testing Winsock SMTP functionality";
sendmail.body = "Test message:\n\n"
"Windows Sockets (Winsock) provides a general-purpose networking "
"application programming interface (API) based on the socket "
"interface from the University of California at Berkeley.\n";
if (InitSock(sendmail.host))
{
SendMail();
WSACleanup();
}
printf("\nPress ENTER to quit");
getch();
return;
}
tnx...but when i run this email doesn't come...??????
Maybe your SMTP requires authentication... Use POCO, that itīs implementation support authentication.