Answered by:
Problem in Sending smtp mail with c++

Question
-
i try with this code but it not send any mail to destination mail address. any one can help me to correct it ???!!!
#pragma comment(lib, "wsock32.lib")
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <iostream>
using namespace std;
bool EmailSMTP(char* hname, char* sndr, char* rece, char *sbj, char *usr, char *passwd, int port)
{
int iResult;
WSADATA wsaData;
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 0;
}
int iProtocolPort = port;
char tekst[4096] = "";
SOCKET hServer;
LPHOSTENT lpHostEntry;
LPSERVENT lpServEntry;
SOCKADDR_IN SockAddr;
lpHostEntry = gethostbyname(hname);
if (!lpHostEntry)
{
cout << "Nie znaleziono hosta" << " Błąd" ;
return 0;
}
hServer = socket(PF_INET, SOCK_STREAM, 0);
if (hServer == INVALID_SOCKET)
{
cout << "Nie udało się utworzyć serwera." << " Błąd";
return 0;
}
lpServEntry = getservbyname("mail", 0);
if (!lpServEntry)
iProtocolPort = htons(IPPORT_SMTP);
else
iProtocolPort = lpServEntry->s_port;
SockAddr.sin_family = AF_INET;
SockAddr.sin_port = iProtocolPort;
SockAddr.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
connect(hServer, (PSOCKADDR)&SockAddr, sizeof(SockAddr));
recv(hServer, tekst, sizeof(tekst), 0);
sprintf(tekst, "HELO\r\n");
send(hServer, tekst, strlen(tekst), 0);
recv(hServer, tekst, sizeof(tekst), 0);
sprintf(tekst, "AUTH LOGIN\r\n");
send(hServer, tekst, strlen(tekst), 0);
recv(hServer, tekst, sizeof(tekst), 0);
sprintf(tekst, "%s\r\n", usr);
send(hServer, tekst, strlen(tekst), 0);
recv(hServer, tekst, sizeof(tekst), 0);
sprintf(tekst, "%s\r\n", passwd);
send(hServer, tekst, strlen(tekst), 0);
recv(hServer, tekst, sizeof(tekst), 0);
sprintf(tekst, "MAIL FROM:<%s>\r\n", sndr);
send(hServer, tekst, strlen(tekst), 0);
recv(hServer, tekst, sizeof(tekst), 0);
sprintf(tekst, "RCPT TO:<%s>\r\n", rece);
send(hServer, tekst, strlen(tekst), 0);
recv(hServer, tekst, sizeof(tekst), 0);
sprintf(tekst, "DATA\r\n");
send(hServer, tekst, strlen(tekst), 0);
recv(hServer, tekst, sizeof(tekst), 0);
sprintf(tekst, "From: tester <%s>\r\n", sndr);
send(hServer, tekst, strlen(tekst), 0);
sprintf(tekst, "To: tester <%s>\r\n", rece);
send(hServer, tekst, strlen(tekst), 0);
sprintf(tekst, "Subject: %s\r\n", sbj);
send(hServer, tekst, strlen(tekst), 0);
sprintf(tekst, "\r\n.\r\n");
send(hServer, tekst, strlen(tekst), 0);
recv(hServer, tekst, sizeof(tekst), 0);
sprintf(tekst, "QUIT\r\n");
send(hServer, tekst, strlen(tekst), 0);
recv(hServer, tekst, sizeof(tekst), 0);
closesocket(hServer);
return 1;
}
int main()
{
EmailSMTP("mail.domain.com", "info@domain.com", "samplemail@gmail.com", "hello", "info", "password", 25);
return 0;
}Thanks.
Tuesday, May 27, 2014 4:38 PM
Answers
-
Hi,
According to your code, you are trying to send email to gmail account. As far as I know, gmail SMTP server will not accept any mail without authentication. You may need to do authentication with SSL or TLS enabled.
About SSL/TLS you may refer to the link below.
By the way, don't forget to call WSACleanup when done using the Winsock dll.
May
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- Marked as answer by May Wang - MSFT Tuesday, June 10, 2014 1:58 AM
Wednesday, May 28, 2014 8:49 AM
All replies
-
Hi,
According to your code, you are trying to send email to gmail account. As far as I know, gmail SMTP server will not accept any mail without authentication. You may need to do authentication with SSL or TLS enabled.
About SSL/TLS you may refer to the link below.
By the way, don't forget to call WSACleanup when done using the Winsock dll.
May
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- Marked as answer by May Wang - MSFT Tuesday, June 10, 2014 1:58 AM
Wednesday, May 28, 2014 8:49 AM -
Hi
thanks for your attention.
but no, i want use mail servers with authentication for example smtp mail server, port, username and password such as gmail, hotmail and another services. Thanks for your links, i now follow it for resolving my problem with it.
Thanks. :-)
Wednesday, May 28, 2014 3:33 PM -
Hi,
How about your issue now? Is it fixed? If it is fixed, could you please share your solution with us? If not, please feel free to let us know.
》Thanks for your links, i now follow it for resolving my problem with it.
Do the links work for you?
May
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.
- Edited by May Wang - MSFT Tuesday, June 10, 2014 2:03 AM
Wednesday, June 4, 2014 6:05 AM