any ideas? anyone have a working example i could look at?
any ideas? anyone have a working example i could look at?
Read Salem's post again.
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah
You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
The two new lines didn't make a difference. What it does is the program stops after writing "data". It seems like it's waiting for input. The program doesn't complete.
Here's what I mean: http://leeman_s.tripod.com/mail.JPG
Last edited by deltabird; 06-28-2003 at 06:14 PM.
see the line that says printf("%s", "\nWe are after quit command"); , the program never even gets to that line. It sort of stops mid-program and hangs there. If I take out the bit that says:Code:#include <stdio.h> #include <io.h>\ int rc; char buf[256]; //#define LINUX /* define this if you are on linux */ //#define WIN32 /* define this if you are on windows */ #ifdef WIN32 # include "io.h" # include "winsock2.h" /* WSAGetLastError, WSAStartUp */ # define snprintf _snprintf #endif #ifdef LINUX # include <netdb.h> /* gethostbyname */ # include <netinet/in.h> /* htons */ # include <sys/socket.h> #endif #pragma comment(lib, "wsock32.lib") static void sendmail_write(const int sock,const char *str,const char *arg) { char buf[4096]; if (arg != NULL) snprintf(buf, sizeof(buf), str, arg); else snprintf(buf, sizeof(buf), str); send(sock, buf, strlen(buf), 0); if((rc = recv(sock, buf, sizeof(buf) - 1, 0)) == -1) { perror("recv"); printf("%s", "ERROR"); } else { buf[rc] = 0; printf("%s", buf); } } static int sendmail(const char *from,const char *to,const char *subject,const char *body, const char *hostname,const int port) { struct hostent *host; struct sockaddr_in saddr_in; int sock = 0; #ifdef WIN32 WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { return -1; } #endif sock = socket(AF_INET, SOCK_STREAM, 0); host = gethostbyname(hostname); saddr_in.sin_family = AF_INET; saddr_in.sin_port = htons((u_short)port); saddr_in.sin_addr.s_addr = 0; memcpy((char*)&(saddr_in.sin_addr), host->h_addr, host->h_length); if(connect(sock, (struct sockaddr*)&saddr_in, sizeof(saddr_in)) == -1) { return -2; } sendmail_write(sock, "helo %s\r\n", from); // greeting sendmail_write(sock, "mail from: %s\r\n", from); // from sendmail_write(sock, "rcpt to: %s\r\n", to); // to sendmail_write(sock, "data\r\n", NULL); // begin data //printf("%s", "\nWe are after quit command"); // next comes mail headers sendmail_write(sock, "From: %s\r\n", from); sendmail_write(sock, "To: %s\r\n", to); sendmail_write(sock, "Subject: %s\r\n", subject); sendmail_write(sock, "Date: 6/6/6\r\n", NULL); sendmail_write(sock, "\r\n", NULL); sendmail_write(sock, "%s\r\n", body); // data sendmail_write(sock, ".\r\n", NULL); // end data //Sleep(5000); sendmail_write(sock, "\r\n.\r\n", NULL); // terminate printf("%s", "\nWe are after quit command"); //never even gets to this line #ifdef WIN32 closesocket(sock); #else close(sock); #endif return 0; } int main(int argc, char *argv[]) { int ret = sendmail( "[email protected]", /* from */ "[email protected]", /* to */ "Subject", /* subject */ "body", /* body */ "mx2.hotmail.com", /* hostname */ 25 /* port */ ); if (ret != 0) fprintf(stderr, "Failed to send mail (code: %i).\n", ret); else fprintf(stdout, "Mail successfully sent.\n"); return ret; }
....then it actually finishes, but the mail doesn't get sent still. But the program at least finishes (gets to the end). Any ideas? I mean, somebody HAS to have at least one working example right?Code:if((rc = recv(sock, buf, sizeof(buf) - 1, 0)) == -1) { perror("recv"); printf("%s", "ERROR"); } else { buf[rc] = 0; printf("%s", buf); }
I think the problem is that you are sending <crlf>.<crlf> twice. Try replacing the second instance with QUIT
Add a printf so you see what you are sending out.
If you are still having problems post the program output.
Also try these links:
http://ostrosoft.com/smtp_component/smtp_demo.asp
http://www.codeproject.com/useritems/smtp.asp
http://www.stormpages.com/bojanjurca/CSamples.htm
http://www.google.com/search?q=smtp+...+source+code+c
Good Luck!
Have Fun!
PS!!!!
You do not appear to be reading out the server welcome message. Do a recv call to remove this from the input buffer before sending anything.
still doesn't work. I tried one of thsoe examples and it had the same problem as my program. can somebody try running this code on their machine and tell me if it works or not? tell me what it does please.
here is the pic of what it does on mine: http://leeman_s.tripod.com/mail.JPG
Code:#include <stdio.h> #include <io.h> int rc; char buf[256]; //#define LINUX /* define this if you are on linux */ //#define WIN32 /* define this if you are on windows */ #ifdef WIN32 # include "io.h" # include "winsock2.h" /* WSAGetLastError, WSAStartUp */ # define snprintf _snprintf #endif #ifdef LINUX # include <netdb.h> /* gethostbyname */ # include <netinet/in.h> /* htons */ # include <sys/socket.h> #endif #pragma comment(lib, "wsock32.lib") static void sendmail_write(const int sock,const char *str,const char *arg) { char buf[4096]; if (arg != NULL) snprintf(buf, sizeof(buf), str, arg); else snprintf(buf, sizeof(buf), str); send(sock, buf, strlen(buf), 0); if((rc = recv(sock, buf, sizeof(buf) - 1, 0)) == -1) { perror("recv"); printf("%s", "ERROR"); } else { buf[rc] = 0; printf("%s", buf); } } static int sendmail(const char *from,const char *to,const char *subject,const char *body, const char *hostname,const int port) { struct hostent *host; struct sockaddr_in saddr_in; int sock = 0; #ifdef WIN32 WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { return -1; } #endif sock = socket(AF_INET, SOCK_STREAM, 0); host = gethostbyname(hostname); saddr_in.sin_family = AF_INET; saddr_in.sin_port = htons((u_short)port); saddr_in.sin_addr.s_addr = 0; memcpy((char*)&(saddr_in.sin_addr), host->h_addr, host->h_length); if(connect(sock, (struct sockaddr*)&saddr_in, sizeof(saddr_in)) == -1) { return -2; } sendmail_write(sock, "helo %s\r\n", from); // greeting sendmail_write(sock, "mail from: %s\r\n", from); // from sendmail_write(sock, "rcpt to: %s\r\n", to); // to sendmail_write(sock, "data\r\n", NULL); // begin data //printf("%s", "\nWe are after quit command"); // next comes mail headers sendmail_write(sock, "From: %s\r\n", from); sendmail_write(sock, "To: %s\r\n", to); sendmail_write(sock, "Subject: %s\r\n", subject); sendmail_write(sock, "Date: 6/6/6\r\n", NULL); sendmail_write(sock, "\r\n", NULL); sendmail_write(sock, "%s\r\n", body); // data sendmail_write(sock, ".\r\n", NULL); // end data sendmail_write(sock, "QUIT", NULL); // terminate printf("%s", "\nWe are after quit command"); //never even gets to this line #ifdef WIN32 closesocket(sock); #else close(sock); #endif return 0; } int main(int argc, char *argv[]) { int ret = sendmail( "[email protected]", /* from */ "[email protected]", /* to */ "Subject", /* subject */ "body", /* body */ "mx2.hotmail.com", /* hostname */ 25 /* port */ ); if (ret != 0) fprintf(stderr, "Failed to send mail (code: %i).\n", ret); else fprintf(stdout, "Mail successfully sent.\n"); return ret; }
If you look at the code project log file you will notice that there is no reply after the first two of these sends (so your function will wait for ever to recv data that is not coming!). Either combine them into one send or add an option to turn off the recv in your sendmail_write function.
Code:sendmail_write(sock, "\r\n", NULL); // no recv after this sendmail_write(sock, "%s\r\n", body); // no recv after this sendmail_write(sock, ".\r\n", NULL); // recv after this Possible fix: sendmail_write(sock, "\r\n%s\r\n.\r\n", body);
PS. Also no reply after following sends. Alter your function.
Code:sendmail_write(sock, "From: %s\r\n", from); sendmail_write(sock, "To: %s\r\n", to); sendmail_write(sock, "Subject: %s\r\n", subject); sendmail_write(sock, "Date: 6/6/6\r\n", NULL); FIX: sendmail_write(const int sock,const char *str,const char *arg, BOOL bGetReply)
Ok, the program finishes, but I still get no e-mail from it. I did what id id said (thanks), so the program completes, but I get no e-mail! Here's the new code:
Someone please try running it on their machine, put in your own address if you want and stuff and see if you get the e-mail. Thanks.Code:#include <stdio.h> #include <io.h> int rc; char buf[256]; //#define LINUX /* define this if you are on linux */ //#define WIN32 /* define this if you are on windows */ #ifdef WIN32 # include "io.h" # include "winsock2.h" /* WSAGetLastError, WSAStartUp */ # define snprintf _snprintf #endif #ifdef LINUX # include <netdb.h> /* gethostbyname */ # include <netinet/in.h> /* htons */ # include <sys/socket.h> #endif #pragma comment(lib, "wsock32.lib") static void sendmail_write(const int sock,const char *str,const char *arg, bool reply) { char buf[4096]; if (arg != NULL) snprintf(buf, sizeof(buf), str, arg); else snprintf(buf, sizeof(buf), str); send(sock, buf, strlen(buf), 0); if(reply) { if((rc = recv(sock, buf, sizeof(buf) - 1, 0)) == -1) { perror("recv"); printf("%s", "ERROR"); } else { buf[rc] = 0; printf("%s", buf); } } } static int sendmail(const char *from,const char *to,const char *subject,const char *body, const char *hostname,const int port) { struct hostent *host; struct sockaddr_in saddr_in; int sock = 0; #ifdef WIN32 WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { return -1; } #endif sock = socket(AF_INET, SOCK_STREAM, 0); host = gethostbyname(hostname); saddr_in.sin_family = AF_INET; saddr_in.sin_port = htons((u_short)port); saddr_in.sin_addr.s_addr = 0; memcpy((char*)&(saddr_in.sin_addr), host->h_addr, host->h_length); if(connect(sock, (struct sockaddr*)&saddr_in, sizeof(saddr_in)) == -1) { return -2; } sendmail_write(sock, "helo %s\r\n", from, true); // greeting sendmail_write(sock, "mail from: %s\r\n", from, true); // from sendmail_write(sock, "rcpt to: %s\r\n", to, true); // to sendmail_write(sock, "data\r\n", NULL, true); // begin data //printf("%s", "\nWe are after quit command"); // next comes mail headers sendmail_write(sock, "From: %s\r\n", from, false); sendmail_write(sock, "To: %s\r\n", to, false); sendmail_write(sock, "Subject: %s\r\n", subject, false); sendmail_write(sock, "Date: 6/6/6\r\n", NULL, false); sendmail_write(sock, "\r\n", NULL, false); sendmail_write(sock, "%s\r\n", body, false); // data sendmail_write(sock, ".\r\n", NULL, false); // end data sendmail_write(sock, "QUIT", NULL, false); // terminate //printf("%s", "\nWe are after quit command"); //never even gets to this line #ifdef WIN32 closesocket(sock); #else close(sock); #endif return 0; } int main(int argc, char *argv[]) { int ret = sendmail( "[email protected]", /* from */ "[email protected]", /* to */ "Subject", /* subject */ "body", /* body */ "mx2.hotmail.com", /* hostname */ 25 /* port */ ); if (ret != 0) fprintf(stderr, "Failed to send mail (code: %i).\n", ret); else fprintf(stdout, "Mail successfully sent.\n"); return ret; }
any more ideas? has anyone made a real working example of a program like this? can you compile and run it on your machine?
This last one should be true to get the reply.
Try a different from address (different from the to address).Code:sendmail_write(sock, ".\r\n", NULL, false);
Post the output again if still not working. Copy dos box by highlighting and pressing enter.
Solution pending...
Please wait.
The good news is that I got it working. Simply add the following directly below the connect statement:
You'll have a few emails :-)Code:char buf[4096]; //read out server welcome message if((rc = recv(sock, buf, sizeof(buf) - 1, 0)) == -1) { perror("recv"); printf("%s", "ERROR"); } else { buf[rc] = 0; printf("%s", buf); }
The bad news:
PS!!!!
You do not appear to be reading out the server welcome message. Do a recv call to remove this from the input buffer before sending anything.The first one solved the problem. The second one would have made the problem obvious (as it did for me when I did it).Add a printf so you see what you are sending out.
Anyway, glad we finally solved this.
Yes! Thank you id id! Thank you so much! It finally works! You are my god! wooohoo!
btw, you should register, id id