|
-
April 10th, 2010, 10:26 PM
#1
Simple Email Sending
So this is my code, Everything seems to work minus the fact that no email actually gets sent, what is the reason for this?
Code:
#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <iostream>
#include <windows.h>
#include <winsock2.h>
using namespace std;
#pragma comment(lib, "ws2_32.lib")
#define CRLF "\r\n"
const int VERSION_MAJOR = 1;
const int VERSION_MINOR = 1;
using namespace std;
void ShowUsage(void)
{
cout << "Usage: SENDMAIL mailserv to_addr from_addr messagefile" << endl
<< "Example: SENDMAIL smtp.myisp.com [email protected] [email protected] message.txt" << endl;
system("pause");
exit(1);
}
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 ;
char szSmtpServerName[64] = "smtp.live.com";
char szToAddrA[64] = "[email protected]"; // First account setup for this test
char szFromAddr[64] = "[email protected]"; // Second account setup for this test
char szBuffer[4096] = "";
char szLine[255] = "";
char szMsgLine[255] = "";
SOCKET hServer;
WSADATA WSData;
LPHOSTENT lpHostEntry;
LPSERVENT lpServEntry;
SOCKADDR_IN SockAddr;
const char *whatIsThisShit = szFromAddr;
lstrcpy(szSmtpServerName, argv[1]);
lstrcpy(szFromAddr, argv[3]);
if(argc != 5)
ShowUsage();
argv[3] = szToAddrA;
//ifstream MsgFile(whatIsThisShit); // Use this if not using a file for the body text
ifstream MsgFile("body.txt");
if(WSAStartup(MAKEWORD(VERSION_MAJOR, VERSION_MINOR), &WSData))
{
cout << "Cannot find Winsock v" << VERSION_MAJOR << "." << VERSION_MINOR << " or later!" << endl;
system("pause");
return 1;
}
cout << "Found Winsock version " << VERSION_MAJOR << "." << VERSION_MINOR << "\n";
lpHostEntry = gethostbyname(szSmtpServerName);
if(!lpHostEntry)
{
cout << "Cannot find SMTP mail server " << szSmtpServerName << endl;
system("pause");
return 1;
}
cout << "Found SMTP server (" << lpHostEntry << ")\n";
hServer = socket(PF_INET, SOCK_STREAM, 0);
if(hServer == INVALID_SOCKET)
{
cout << "Cannot open mail server socket" << endl;
system("pause");
return 1;
}
cout << "Created TCP/IP mail server socket\n";
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;
cout << "Checked port\n";
SockAddr.sin_family = AF_INET;
SockAddr.sin_port = iProtocolPort;
SockAddr.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
cout << "Setup socket address structure\n";
if(connect(hServer, (PSOCKADDR) &SockAddr, sizeof(SockAddr)))
{
cout << "Error connecting to Server socket" << endl;
system("pause");
return 1;
}
cout << "connected to server\n";
Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() Reply");
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");
cout << "Creating message data\n";
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");
cout << " - added mail sender\n";
sprintf(szMsgLine, "RCPT TO:<%s>%s", szToAddrA, CRLF);
Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() RCPT TO");
Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() RCPT TO");
cout << " - added mail reciever\n";
// 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");
cout << " - added mail data\n";
MsgFile.getline(szLine, sizeof(szLine));
do
{
sprintf(szMsgLine, "%s%s", szLine, CRLF);
Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() message-line");
MsgFile.getline(szLine, sizeof(szLine));
} while(MsgFile.good());
cout << " - added mail body\n";
cout << "Attempting to send\n\n";
sprintf(szMsgLine, "QUIT%s", CRLF);
Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() QUIT");
Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() QUIT");
cout << "sent message\n";
//SENDMAIL("smtp.live.com", szToAddrA, szFromAddr, MsgFile);
cout << "Sent " << whatIsThisShit << " as email message to " << szToAddrA << endl;
closesocket(hServer);
WSACleanup();
system("pause");
return 0;
}
Last edited by xedon; April 11th, 2010 at 12:41 PM.
-
April 10th, 2010, 10:35 PM
#2
Re: Simple Email Sending
Sorry, only just found out about the [ code] tag from quoting somebody else.
How can I edit a post? (Don't tell me to not double post, because I don't know how to edit).
-
April 11th, 2010, 10:36 AM
#3
Re: Simple Email Sending
1. if you're logged-in, you should have an "edit" option at the bottom of your own posts.
2. In regard to your subject, there is no simple way to send email from an application. With spam so rampant and with so many SMTP options to allow for things like authentication,ssl,etc. and with most ISPs blocking standard mail ports, it is difficult to come up with a decent solution. There may be a way to come up with a specific solution depending on your application and needs.
-
April 11th, 2010, 11:37 AM
#4
Re: Simple Email Sending
Well, my needs = Sending a simple email with ether an attachment (txt file) or that text file to be read into the program and put into the body of the email (same results basically), from and to a hotmail account.
I got the source code from another website, and very heavily edited it for my needs and to make it compile/work...
I can easily auth myself (if I knew how it was coded) if that was a problem, and I know practically nothing about ssl.
Could it be just as simple as changing the ports until I find one that works?
(I must have not been logged in when I couldn't find the edit button)
-
April 11th, 2010, 02:02 PM
#5
Re: Simple Email Sending
Go through your code line by line with a debugger. Check what you are receiving from the mail server. You may also print the messages you are sending and receving to STDOUT, this may help.
Tell us where the program stops receiving or/and what exactly the server responds to you?
-
April 11th, 2010, 03:03 PM
#6
Re: Simple Email Sending
 Originally Posted by xedon
So this is my code, Everything seems to work minus the fact that no email actually gets sent, what is the reason for this?
The first thing I would do is get rid of anything that would cause a user-error during input. I'm referring to the stuff on the command-line. First, hard-code values you know actually work. Then when you get that working, worry about reading the data from the command-line. For example:
Code:
argv[3] = szToAddrA;
The behaviour is undefined, and could crash your program on exit. What if the runtime dynamically allocated that list of pointers, and then you came along and changed the pointer to a non-dynamic one? Do not change argv[].
Secondly, I have seen people literally waste hours, if not days, trying to get programs to work thinking that the problem is within their "critical" code, and all the while the problem is that they are not processing the input correctly. Most of the time, it's incorrectly reading a data file, and second on that list is not processing command-line parameters correctly.
Code:
int main(int argc, char *argv[])
{
//...
lstrcpy(szSmtpServerName, "a name you know works";
lstrcpy(szFromAddr, "another name you know");
Regards,
Paul McKenzie
-
April 11th, 2010, 03:56 PM
#7
Re: Simple Email Sending
Yeah, I did spend hours (and hours) trying to get this to work (I usually learn more that way than asking people to do it for me).
I am all new to sockets and things, so I don't know which servers actually work using this sort of method, do you know any that work (to date)?
-
April 11th, 2010, 07:03 PM
#8
Re: Simple Email Sending
 Originally Posted by xedon
I am all new to sockets and things, so I don't know which servers actually work using this sort of method, do you know any that work (to date)?
Do you have a program that you didn't write that sends mail now via SMTP? Any program -- a commercial app, shareware, freeware, whatever. If you do have a program, what is the mail send/receive set up?
Regards,
Paul McKenzie
-
April 11th, 2010, 07:20 PM
#9
Re: Simple Email Sending
I do not unfortunately 
Well, I use the hotmail website but that is it.
Last edited by xedon; April 11th, 2010 at 07:29 PM.
-
April 11th, 2010, 08:46 PM
#10
Re: Simple Email Sending
Does hotmail even allow SMTP access?
-
April 11th, 2010, 09:59 PM
#11
Re: Simple Email Sending
 Originally Posted by xedon
I do not unfortunately
Well, I use the hotmail website but that is it.
I don't know too much about hotmail.
So you're saying if you installed Eudora or Thunderbird, you can't set them up to receive/send mail? Do you have an ISP email account?
If I were you, I would forget about writing a program for now and get one of these email programs to work first. It makes no sense in you adding the complexity of trying to write a program if existing programs can't do what you're trying to do. Once you convince yourself that yes, a program that another program that uses SMTP will send mail, then and only then should you embark on trying to write a program you writing a program to do this.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; April 11th, 2010 at 10:03 PM.
-
April 12th, 2010, 04:26 AM
#12
Re: Simple Email Sending
I wanted to code it myself really (and learn about sockets etc), I already have outlook setup (but don't want the client to open) which was the whole purpose of writing this (as you can tell, I am still learning a lot). What does Eudora and Thunderbird do, guessing they are similar to Outlook and are just GUI email services, could they be edited in a way so I could use them to send messages (via C++ etc)?
-
April 12th, 2010, 04:44 AM
#13
Re: Simple Email Sending
 Originally Posted by xedon
I wanted to code it myself really (and learn about sockets etc), I already have outlook setup
I thought you said you didn't have a program that sends email. Does it actually send mail? If it does, what are the settings for that account that successfully sends email?
What does Eudora and Thunderbird do, guessing they are similar to Outlook and are just GUI email services,
The GUI is not what sends the mail. All these programs use the same API's to send mail.
could they be edited in a way so I could use them to send messages (via C++ etc)?
What I'm simply asking you is to check if what you're doing is even possible. You do that by seeing if any existing program does what you want to do. That's why I asked whether you have any existing program that works.
Again, what good is it to waste time trying to write a program, and you have no idea if your system is even set up correctly to send mail? If you can't send mail using a run-of-the-mill email program, then no piece of C++ code is going to send mail either.
Regards,
Paul McKenzie
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
|