-
April 24th, 2017, 04:36 PM
#1
Recommend a C or C++ code to send a basic email using the SMTP protocol
I have seen many tutorials with confusing functions that are difficult to translate and understand. I am also not looking for a GUI implementation. I am looking for a basic C or C++ program that has basic(C-file) libraries and not (.exe libraries) to include, that can send a email with a SMTP or equivalent protocol.
The program should except simple parameters like from-email, to-email, email-subject, email-body text, host, email-username, email-password.
I have experience with SMTP with Java programming, here is example link that uses few library imports that are also easy to find online to download, the link is: https://www.tutorialspoint.com/java/...ding_email.htm . Any help will be appreciated with C or C++ programming code.
-
April 25th, 2017, 04:03 AM
#2
Re: Recommend a C or C++ code to send a basic email using the SMTP protocol
You may want to look at this thread.
Victor Nijegorodov
-
April 26th, 2017, 01:41 PM
#3
Re: Recommend a C or C++ code to send a basic email using the SMTP protocol
Originally Posted by VictorN
You may want to look at this thread.
Greetings VictorN,
I visited the given thread post about the "how to send mails from VC++ program without using outlook", this topic was on par with the type of email application that I need but I got errors running the code provided by "Andreas Masur"
Andreas Masur's Code:
Code:
#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include <stdlib.h>
#include <fstream.h>
#include <iostream.h>
#include <windows.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
// Insist on at least Winsock v1.1
const VERSION_MAJOR = 1;
const VERSION_MINOR = 1;
#define CRLF "\r\n" // carriage-return/line feed pair
void ShowUsage(void)
{
cout << "Usage: SENDMAIL mailserv to_addr from_addr messagefile" << endl
<< "Example: SENDMAIL smtp.myisp.com rcvr@elsewhere.com my_id@mydomain.com message.txt" << endl;
exit(1);
}
// Basic error checking for send() and recv() functions
void Check(int iStatus, char *szFunction)
{
if((iStatus != SOCKET_ERROR) && (iStatus))
return;
cerr << "Error during call to " << szFunction << ": " << iStatus << " - " << GetLastError() << endl;
}
int main(int argc, char *argv[])
{
int iProtocolPort = 0;
char szSmtpServerName[64] = "";
char szToAddr[64] = "";
char szFromAddr[64] = "";
char szBuffer[4096] = "";
char szLine[255] = "";
char szMsgLine[255] = "";
SOCKET hServer;
WSADATA WSData;
LPHOSTENT lpHostEntry;
LPSERVENT lpServEntry;
SOCKADDR_IN SockAddr;
// Check for four command-line args
if(argc != 5)
ShowUsage();
// Load command-line args
lstrcpy(szSmtpServerName, argv[1]);
lstrcpy(szToAddr, argv[2]);
lstrcpy(szFromAddr, argv[3]);
// Create input stream for reading email message file
ifstream MsgFile(argv[4]);
// Attempt to intialize WinSock (1.1 or later)
if(WSAStartup(MAKEWORD(VERSION_MAJOR, VERSION_MINOR), &WSData))
{
cout << "Cannot find Winsock v" << VERSION_MAJOR << "." << VERSION_MINOR << " or later!" << endl;
return 1;
}
// Lookup email server's IP address.
lpHostEntry = gethostbyname(szSmtpServerName);
if(!lpHostEntry)
{
cout << "Cannot find SMTP mail server " << szSmtpServerName << endl;
return 1;
}
// Create a TCP/IP socket, no specific protocol
hServer = socket(PF_INET, SOCK_STREAM, 0);
if(hServer == INVALID_SOCKET)
{
cout << "Cannot open mail server socket" << endl;
return 1;
}
// Get the mail service port
lpServEntry = getservbyname("mail", 0);
// Use the SMTP default port if no other port is specified
if(!lpServEntry)
iProtocolPort = htons(IPPORT_SMTP);
else
iProtocolPort = lpServEntry->s_port;
// Setup a Socket Address structure
SockAddr.sin_family = AF_INET;
SockAddr.sin_port = iProtocolPort;
SockAddr.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
// Connect the Socket
if(connect(hServer, (PSOCKADDR) &SockAddr, sizeof(SockAddr)))
{
cout << "Error connecting to Server socket" << endl;
return 1;
}
// Receive initial response from SMTP server
Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() Reply");
// Send HELO server.com
sprintf(szMsgLine, "HELO %s%s", szSmtpServerName, CRLF);
Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() HELO");
Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() HELO");
// Send MAIL FROM: <sender@mydomain.com>
sprintf(szMsgLine, "MAIL FROM:<%s>%s", szFromAddr, CRLF);
Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() MAIL FROM");
Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() MAIL FROM");
// Send RCPT TO: <receiver@domain.com>
sprintf(szMsgLine, "RCPT TO:<%s>%s", szToAddr, CRLF);
Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() RCPT TO");
Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() RCPT TO");
// Send DATA
sprintf(szMsgLine, "DATA%s", CRLF);
Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() DATA");
Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() DATA");
// Send all lines of message body (using supplied text file)
MsgFile.getline(szLine, sizeof(szLine)); // Get first line
do // for each line of message text...
{
sprintf(szMsgLine, "%s%s", szLine, CRLF);
Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() message-line");
MsgFile.getline(szLine, sizeof(szLine)); // get next line.
} while(MsgFile.good());
// Send blank line and a period
sprintf(szMsgLine, "%s.%s", CRLF, CRLF);
Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() end-message");
Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() end-message");
// Send QUIT
sprintf(szMsgLine, "QUIT%s", CRLF);
Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() QUIT");
Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() QUIT");
// Report message has been sent
cout << "Sent " << argv[4] << " as email message to " << szToAddr << endl;
// Close server socket and prepare to exit.
closesocket(hServer);
WSACleanup();
return 0;
}
My errors so far are:
- fatal error: fstream.h: No such file or directory
- fatal error: iostream.h: No such file or directory
Do you have any more alternate links to the C or C++ email solution?
-
April 26th, 2017, 02:20 PM
#4
Re: Recommend a C or C++ code to send a basic email using the SMTP protocol
Victor Nijegorodov
-
April 26th, 2017, 03:25 PM
#5
Re: Recommend a C or C++ code to send a basic email using the SMTP protocol
Code:
#include <stdio.h>
#include <stdlib.h>
#include <fstream.h>
#include <iostream.h>
#include <windows.h>
#include <winsock2.h>
This is pre the c++98 standard. In standard c++ this becomes
Code:
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <windows.h>
#include <winsock2.h>
using namespace std;
Also
Code:
const BYTE VERSION_MAJOR = 1;
const BYTE VERSION_MINOR = 1;
This should now compile - but with warnings.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 26th, 2017, 09:19 PM
#6
Re: Recommend a C or C++ code to send a basic email using the SMTP protocol
Originally Posted by 2kaud
Code:
#include <stdio.h>
#include <stdlib.h>
#include <fstream.h>
#include <iostream.h>
#include <windows.h>
#include <winsock2.h>
This is pre the c++98 standard. In standard c++ this becomes
Code:
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <windows.h>
#include <winsock2.h>
using namespace std;
Also
Code:
const BYTE VERSION_MAJOR = 1;
const BYTE VERSION_MINOR = 1;
This should now compile - but with warnings.
Greetings 2Kaud,
Thank you, you were right about the corrections that you made, including the standard definitions that you provided. Everything compiled except a build error, something went wrong in winsock2.h which is wierd because it is not part of the actual code that I have written. Here is the error:
Code:
In file included from /usr/include/w32api/winsock2.h:56:0,
from main.cpp:8:
/usr/include/w32api/psdk_inc/_fd_types.h:100:2: warning: #warning "fd_set and associated macros have been defined in sys/types. This can cause runtime problems with W32 sockets" [-Wcpp]
#warning "fd_set and associated macros have been defined in sys/types. \
^~~~~~~
main.cpp:36:31: error: stray '#' in program
int main(int argc, char *argv[])
^
main.cpp:36:32: error: invalid digit "9" in octal constant
int main(int argc, char *argv[])
^~~
main.cpp:36:37: error: stray '#' in program
int main(int argc, char *argv[])
^
main.cpp:36:38: error: invalid digit "9" in octal constant
int main(int argc, char *argv[])
^~~
main.cpp:39:32: error: stray '#' in program
char szSmtpServerName[64] = "";
^
main.cpp:39:33: error: invalid digit "9" in octal constant
char szSmtpServerName[64] = "";
^~~
main.cpp:39:40: error: stray '#' in program
char szSmtpServerName[64] = "";
^
main.cpp:39:41: error: invalid digit "9" in octal constant
char szSmtpServerName[64] = "";
^~~
main.cpp:40:24: error: stray '#' in program
char szToAddr[64] = "";
^
main.cpp:40:25: error: invalid digit "9" in octal constant
char szToAddr[64] = "";
^~~
main.cpp:40:32: error: stray '#' in program
char szToAddr[64] = "";
^
main.cpp:40:33: error: invalid digit "9" in octal constant
char szToAddr[64] = "";
^~~
main.cpp:41:26: error: stray '#' in program
char szFromAddr[64] = "";
^
main.cpp:41:27: error: invalid digit "9" in octal constant
char szFromAddr[64] = "";
^~~
main.cpp:41:34: error: stray '#' in program
char szFromAddr[64] = "";
^
main.cpp:41:35: error: invalid digit "9" in octal constant
char szFromAddr[64] = "";
^~~
main.cpp:42:24: error: stray '#' in program
char szBuffer[4096] = "";
^
main.cpp:42:25: error: invalid digit "9" in octal constant
char szBuffer[4096] = "";
^~~
main.cpp:42:34: error: stray '#' in program
char szBuffer[4096] = "";
^
main.cpp:42:35: error: invalid digit "9" in octal constant
char szBuffer[4096] = "";
^~~
main.cpp:43:22: error: stray '#' in program
char szLine[255] = "";
^
main.cpp:43:23: error: invalid digit "9" in octal constant
char szLine[255] = "";
^~~
main.cpp:43:31: error: stray '#' in program
char szLine[255] = "";
^
main.cpp:43:32: error: invalid digit "9" in octal constant
char szLine[255] = "";
^~~
main.cpp:44:25: error: stray '#' in program
char szMsgLine[255] = "";
^
main.cpp:44:26: error: invalid digit "9" in octal constant
char szMsgLine[255] = "";
^~~
main.cpp:44:34: error: stray '#' in program
char szMsgLine[255] = "";
^
main.cpp:44:35: error: invalid digit "9" in octal constant
char szMsgLine[255] = "";
^~~
main.cpp:56:34: error: stray '#' in program
lstrcpy(szSmtpServerName, argv[1]);
^
main.cpp:56:35: error: invalid digit "9" in octal constant
lstrcpy(szSmtpServerName, argv[1]);
^~~
main.cpp:56:41: error: stray '#' in program
lstrcpy(szSmtpServerName, argv[1]);
^
main.cpp:56:42: error: invalid digit "9" in octal constant
lstrcpy(szSmtpServerName, argv[1]);
^~~
main.cpp:57:26: error: stray '#' in program
lstrcpy(szToAddr, argv[2]);
^
main.cpp:57:27: error: invalid digit "9" in octal constant
lstrcpy(szToAddr, argv[2]);
^~~
main.cpp:57:33: error: stray '#' in program
lstrcpy(szToAddr, argv[2]);
^
main.cpp:57:34: error: invalid digit "9" in octal constant
lstrcpy(szToAddr, argv[2]);
^~~
main.cpp:58:28: error: stray '#' in program
lstrcpy(szFromAddr, argv[3]);
^
main.cpp:58:29: error: invalid digit "9" in octal constant
lstrcpy(szFromAddr, argv[3]);
^~~
main.cpp:58:35: error: stray '#' in program
lstrcpy(szFromAddr, argv[3]);
^
main.cpp:58:36: error: invalid digit "9" in octal constant
lstrcpy(szFromAddr, argv[3]);
^~~
main.cpp:61:25: error: stray '#' in program
ifstream MsgFile(argv[4]);
^
main.cpp:61:26: error: invalid digit "9" in octal constant
ifstream MsgFile(argv[4]);
^~~
main.cpp:61:32: error: stray '#' in program
ifstream MsgFile(argv[4]);
^
main.cpp:61:33: error: invalid digit "9" in octal constant
ifstream MsgFile(argv[4]);
^~~
main.cpp:155:27: error: stray '#' in program
cout << "Sent " << argv[4] << " as email message to " << szToAddr << endl;
^
main.cpp:155:28: error: invalid digit "9" in octal constant
cout << "Sent " << argv[4] << " as email message to " << szToAddr << endl;
^~~
main.cpp:155:34: error: stray '#' in program
cout << "Sent " << argv[4] << " as email message to " << szToAddr << endl;
^
main.cpp:155:35: error: invalid digit "9" in octal constant
cout << "Sent " << argv[4] << " as email message to " << szToAddr << endl;
^~~
In file included from main.cpp:8:0:
/usr/include/w32api/winsock2.h:995:34: error: conflicting declaration of C function 'int select(int, _types_fd_set*, _types_fd_set*, _types_fd_set*, PTIMEVAL)'
WINSOCK_API_LINKAGE int WSAAPI select(int nfds,fd_set *readfds,fd_set *writefds,fd_set *exceptfds,const PTIMEVAL timeout);
^~~~~~
In file included from /usr/include/sys/types.h:68:0,
from /usr/include/stdio.h:61,
from main.cpp:3:
/usr/include/sys/select.h:73:5: note: previous declaration 'int select(int, _types_fd_set*, _types_fd_set*, _types_fd_set*, timeval*)'
int select __P ((int __n, fd_set *__readfds, fd_set *__writefds,
^~~~~~
main.cpp:36:30: error: expected ',' or '...' before '&' token
int main(int argc, char *argv[])
^
main.cpp:36:35: error: expected ')' before ';' token
int main(int argc, char *argv[])
^
main.cpp:36:5: warning: second argument of 'int main(int, char*)' should be 'char **' [-Wmain]
int main(int argc, char *argv[])
^~~~
main.cpp:36:38: error: expected unqualified-id before numeric constant
int main(int argc, char *argv[])
^~~
main.cpp:36:38: error: expected constructor, destructor, or type conversion before numeric constant
main.cpp:36:42: error: expected unqualified-id before ')' token
int main(int argc, char *argv[])
Last edited by Computer_Science; April 26th, 2017 at 09:36 PM.
-
April 27th, 2017, 02:12 AM
#7
Re: Recommend a C or C++ code to send a basic email using the SMTP protocol
I tested the revised code with Visual studio 2017 and it compiled OK with warnings. What compiler are you using? The issue seems to be with the file _fd_types.h which isn't used with VS2017.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 27th, 2017, 11:49 AM
#8
Re: Recommend a C or C++ code to send a basic email using the SMTP protocol
Originally Posted by 2kaud
I tested the revised code with Visual studio 2017 and it compiled OK with warnings. What compiler are you using? The issue seems to be with the file _fd_types.h which isn't used with VS2017.
Great and thank you, I was using Cygwin when I encountered the fd_types.h error. I switched to Microsoft Visual Studio 14.0 cl compiler and now get the error of "fatal error C1034: cstdio: no include path set". Not sure how to fix this error within Visual Studios?
-
April 27th, 2017, 12:17 PM
#9
Re: Recommend a C or C++ code to send a basic email using the SMTP protocol
Originally Posted by Computer_Science
Great and thank you, I was using Cygwin when I encountered the fd_types.h error. I switched to Microsoft Visual Studio 14.0 cl compiler and now get the error of "fatal error C1034: cstdio: no include path set". Not sure how to fix this error within Visual Studios?
Did you try to google for fatal error C1034: cstdio: no include path set?
https://www.google.ch/search?q=%22fa...clude+path+set
Victor Nijegorodov
-
April 27th, 2017, 01:58 PM
#10
Re: Recommend a C or C++ code to send a basic email using the SMTP protocol
Why are you compiling from the command line with VS (cl.exe) and not from within the VS IDE?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 27th, 2017, 05:13 PM
#11
Re: Recommend a C or C++ code to send a basic email using the SMTP protocol
Originally Posted by 2kaud
Why are you compiling from the command line with VS (cl.exe) and not from within the VS IDE?
Because I have my Netbeans IDE setup up with my visual studios compiler "cl.exe". How will the program compile differently in the VS IDE if both situations have the use of the "cl.exe"?
-
April 27th, 2017, 05:42 PM
#12
Re: Recommend a C or C++ code to send a basic email using the SMTP protocol
Originally Posted by VictorN
I did googled the error, and the only result with the same exact error was here: https://www.google.ch/url?sa=t&rct=j...ySTnig&cad=rjt and the solution uses the command line commands to compile the program without errors.
-
April 28th, 2017, 02:28 AM
#13
Re: Recommend a C or C++ code to send a basic email using the SMTP protocol
Originally Posted by Computer_Science
Because I have my Netbeans IDE setup up with my visual studios compiler "cl.exe". How will the program compile differently in the VS IDE if both situations have the use of the "cl.exe"?
Because VS IDE automatically sets up the required correct build environment and uses the correct options with cl to compile the program - whereas Netbeans IDE isn't.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 28th, 2017, 06:10 PM
#14
Re: Recommend a C or C++ code to send a basic email using the SMTP protocol
Originally Posted by 2kaud
Because VS IDE automatically sets up the required correct build environment and uses the correct options with cl to compile the program - whereas Netbeans IDE isn't.
I ultimately need this for a JNI implementation, will getting the code to automatically build in the VS IDE limit me to only working for VS IDE, or will I be able to also compile it with the JNI implementation with android? Basically could you know what corrections the IDE makes so that I can do the same with the JNI implementation?
-
May 1st, 2017, 03:16 PM
#15
Re: Recommend a C or C++ code to send a basic email using the SMTP protocol
Originally Posted by 2kaud
Because VS IDE automatically sets up the required correct build environment and uses the correct options with cl to compile the program - whereas Netbeans IDE isn't.
@2kaud do you know if getting the code to compile in VS IDE will also compile in a JNI implementation?
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|