![]() |
| | #1 | |
| Registered User Join Date: Sep 2006
Posts: 230
| sending message to GMAIL using SMTP I was trying to send a message to my gmail account using a program I found on the web. I had to modify it and ask for help on a forum to get it to work. I was successfully receiving the emails and everything looked fine. Then after about 2 days, it stopped working. So I checked the log file and this is what it contained: Quote:
Is there any way to fix this? Here's the code: Code: #define cmailserver "gmail-smtp-in.l.google.com"
#define cemailto "me1@gmail.com"
#define cemailfrom "me2@gmail.com"
#define SMTPLog "SMTP log.txt"
#define cemailsubject "test"
#define waittime 30
int MailIt(char *mailserver, char *emailto, char *emailfrom,
char *emailsubject, char *emailmessage, long maxlen)
{
SOCKET sockfd;
WSADATA wsaData;
FILE *smtpfile;
#define bufsize 300
int bytes_sent; /* Sock FD */
int err;
struct hostent *host; /* info from gethostbyname */
struct sockaddr_in dest_addr; /* Host Address */
char line[maxlen+100];
char *Rec_Buf = (char*) malloc(bufsize+1);
smtpfile=fopen(SMTPLog,"a+");
if (WSAStartup(0x202,&wsaData) == SOCKET_ERROR)
{
fputs("WSAStartup failed",smtpfile);
WSACleanup();
return -1;
}
if ( (host=gethostbyname(mailserver)) == NULL)
{
perror("gethostbyname");
exit(1);
}
memset(&dest_addr,0,sizeof(dest_addr));
memcpy(&(dest_addr.sin_addr),host->h_addr,host->h_length);
/* Prepare dest_addr */
dest_addr.sin_family= host->h_addrtype; /* AF_INET from gethostbyname */
dest_addr.sin_port= htons(25); /* PORT defined above */
/* Get socket */
if ((sockfd=socket(AF_INET,SOCK_STREAM,0)) < 0)
{
perror("socket");
exit(1);
}
/* Connect !*/
fputs("Connecting....\n",smtpfile);
if (connect(sockfd, (struct sockaddr *)&dest_addr,sizeof(dest_addr)) == -1)
{
perror("connect");
exit(1);
}
Sleep(waittime);
err=recv(sockfd,Rec_Buf,bufsize,0);Rec_Buf[err] = '\0';
fputs(Rec_Buf,smtpfile);
strcpy(line,"helo me.somepalace.com\n");
fputs(line,smtpfile);
bytes_sent=send(sockfd,line,strlen(line),0);
Sleep(waittime);
err=recv(sockfd,Rec_Buf,bufsize,0);Rec_Buf[err] = '\0';
fputs(Rec_Buf,smtpfile);
strcpy(line,"MAIL FROM:<");
strncat(line,emailfrom,strlen(emailfrom));
strncat(line,">\n",3);
fputs(line,smtpfile);
bytes_sent=send(sockfd,line,strlen(line),0);
Sleep(waittime);
err=recv(sockfd,Rec_Buf,bufsize,0);Rec_Buf[err] = '\0';
fputs(Rec_Buf,smtpfile);
strcpy(line,"RCPT TO:<");
strncat(line,emailto,strlen(emailto));
strncat(line,">\n",3);
fputs(line,smtpfile);
bytes_sent=send(sockfd,line,strlen(line),0);
Sleep(waittime);
err=recv(sockfd,Rec_Buf,bufsize,0);Rec_Buf[err] = '\0';
fputs(Rec_Buf,smtpfile);
strcpy(line,"DATA\n");
fputs(line,smtpfile);
bytes_sent=send(sockfd,line,strlen(line),0);
Sleep(waittime);
err=recv(sockfd,Rec_Buf,bufsize,0);Rec_Buf[err] = '\0';
fputs(Rec_Buf,smtpfile);
Sleep(waittime);
strcpy(line,"To:");
strcat(line,emailto);
strcat(line,"\n");
strcat(line,"From:");
strcat(line,emailfrom);
strcat(line,"\n");
strcat(line,"Subject:");
strcat(line,emailsubject);
//strcat(line,"\n");
//strcat(line,emailmessage);
strcat(line,"\r\n.\r\n");
fputs(line,smtpfile);
bytes_sent=send(sockfd,line,strlen(line),0);
Sleep(waittime);
err=recv(sockfd,Rec_Buf,bufsize,0);Rec_Buf[err] = '\0';
fputs(Rec_Buf,smtpfile);
strcpy(line,"quit\n");
fputs(line,smtpfile);
bytes_sent=send(sockfd,line,strlen(line),0);
Sleep(waittime);
err=recv(sockfd,Rec_Buf,bufsize,0);Rec_Buf[err] = '\0';
fputs(Rec_Buf,smtpfile);
fclose(smtpfile);
#ifdef WIN32
closesocket(sockfd);
WSACleanup();
#else
close(sockfd);
#endif
}
__________________ I might not be a pro, but I'm usually right Last edited by Abda92; 02-20-2008 at 11:32 AM. | |
| Abda92 is offline |
| | #2 |
| CSharpener Join Date: Oct 2006
Posts: 5,336
| Do you have a mail client configured to send mails using your gmail account? Have you tried to send a mail from the client and from the program and compare the Ethereal traces?
__________________ If I have eight hours for cutting wood, I spend six sharpening my axe. |
| vart is offline |
| | #3 |
| Registered User Join Date: Sep 2006
Posts: 230
| Well, I'm not really sure what you mean. But if you mean something like Microsoft Outlook, no, I didn't. However I searched the internet on how to send emails using SMTP on linux (I used cygwin) without a program just to make sure it wasn't the problem, and I was reassured that the problem was with my IP. I read a lot of posts talking about dynamic IPs and that they were blocked by some - if not most - email providers. What does this mean? And I'm sorry, I don't understand what Ethereal traces...
__________________ I might not be a pro, but I'm usually right |
| Abda92 is offline |
| | #4 |
| CSharpener Join Date: Oct 2006
Posts: 5,336
| If problem is with the IP I do not see how you plan to solve it using the program The only solution I see - find an anonimous SMTP server and using it for outgoing mails Ethereal - program that can catch IP traffic on your computer and show you all IP packets incoming/outgoing with a lot of additional features like parsing a lot of IP protocols, filtering the streams based on different conditions like port, packet protocol, source or destination ip... I know there is version for Windows and suppose there are versions for other OSes as well. If not - should be analogs available fro unix.
__________________ If I have eight hours for cutting wood, I spend six sharpening my axe. |
| vart is offline |
| | #5 |
| Registered User Join Date: Sep 2006
Posts: 230
| Thanks, but I don't know what to search for ![]() Can I use any SMTP server to send to Gmail? Would I only need to replace "gmail-smtp-in.l.google.com" with the server? Like I said I don't know much about how to use SMTP. All I know is the general idea. And I knew I can't fix the IP problem within the program, but I just wanted to know what it means and find out if I can fix it up with my ISP. Thanks again
__________________ I might not be a pro, but I'm usually right |
| Abda92 is offline |
| | #6 |
| CSharpener Join Date: Oct 2006
Posts: 5,336
| I know 2 types of SMTP servers. Regular - will check user name and password before processing message. If you access gmail.com server with correct user credentials - It should net decline your message ( MAybe you have spoiled the server with to much spam and it added the IP to the black list...)And there are anonimous SMTP pservers, that will just transfer the received message to the destination address without checking any credentials. List of available servers should be looked in the internet.
__________________ If I have eight hours for cutting wood, I spend six sharpening my axe. |
| vart is offline |
| | #7 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,372
| Moved to Networking/Device Communication forum.
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way |
| laserlight is offline |
| | #8 |
| Registered User Join Date: Sep 2006
Posts: 230
| Thanks for helping vart. And to laserlight, thanks for placing the thread in the right forum, even though it took me some time to find it! Back to the topic, I don't mind using the 1st method, that is, the one where you access the server with correct user credentials. But the problem is, I don't know how ![]() I really have no idea whatsoever how to communicate with the server. Like I said, I found this function and modified it as far as I can go. If you can point me out to a function that will allow me to input my username and password or whatever I need to send the email I'd use it happily. But I've searched a lot and I couldn't find anything I could understand. Thanks again.
__________________ I might not be a pro, but I'm usually right |
| Abda92 is offline |
| | #9 |
| Registered User Join Date: Sep 2006
Posts: 230
| YES!!! I finally found my ISP's SMTP server and it's sending the mail. Only problem is, the message is blank ![]() I had this problem before when I used the public server, but I fixed it by starting the message with '\n'. What's even weirder is that it actually sent 3 successive emails one time when I ran it, but didn't work after I compiled it again! (and I don't remember exactly what I did )I personally think the problem is with Gmail. Any ideas?
__________________ I might not be a pro, but I'm usually right Last edited by Abda92; 02-25-2008 at 05:38 AM. |
| Abda92 is offline |
| | #10 |
| Registered User Join Date: Oct 2001
Posts: 2,110
| Have you tried testing it on a local server? |
| robwhit is offline |
| | #11 |
| Registered User Join Date: Sep 2006
Posts: 230
| Umm... I don't know what you mean by a local server. But I got the text to show in the email ![]() Only problem is, I can't send mail on the run. So I'm trying to work out how to send using gmail's SMTP server (smtp-gmail-in.l.google.com) I read the whole RFC (no. 821) and all I found about verifying was the command VRFY. I tried that, and it worked once, but then I tried it again multiple times and it didn't ![]() Is that possible? That the server accepts it once *by mistake* but rejects it later? Or did I type the right command once but never again? Just to clarify things, it was rejecting, then it sent once, then it rejected again. I'm getting the same error: "The IP you're using to send email is not authorized..." I've learned a lot about SOCKETs just from this function, but I still need to know the general SMTP procedure to work it out...
__________________ I might not be a pro, but I'm usually right |
| Abda92 is offline |
| | #12 |
| Registered User Join Date: Sep 2006
Posts: 230
| Oh my God, I was reading the wrong RFC! After reading the newer one and searching a little on Google I finally found out what I need: TLS I think I need to do something like STARTTLS after the EHLO command, but it's not working All I get is "Command not recognized" or something like that.So, any way I can use TLS from telnet? If I can get that to work, I think I can modify the function to implement it.
__________________ I might not be a pro, but I'm usually right |
| Abda92 is offline |
| | #13 |
| Cat without Hat Join Date: Apr 2003
Posts: 8,492
| To use STARTTLS from telnet, you'd have to do encryption in your head. Have fun with that.
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law |
| CornedBee is offline |
| | #14 |
| Registered User Join Date: Sep 2006
Posts: 230
| Thanks for replying CornedBee, I just found that out before I read your post. The encryption is not a problem; I can test it with same account manually (you only need to encrypt the username/password once), then, after I get it working, the function will do it automatically. But I finally got the starttls command to work. I had to use smtp.gmail.com as a server. Now my problem is, I can't authenticate. This is input and output of TELNET (C=client, S=server): Code: cmd: TELNET smtp.gmail.com 25
// This clears out (comment)
S: 220 mx.google.com ESMTP 39sm1851524hui.5
C: ehlo
S: 250-mx.google.com at your service, [90.148.53.234]
250-SIZE 28311552
250-8BITTIME
250-STARTTLS
250 ENHANCEDSTATUSCODES
C: starttls
S: 220 2.0.0 Ready to start TLS
C: auth plain
Connection to host lost.
I'm really getting confused now. What I remember from the RFCs is that the connection must not deliberately be broken unless the QUIT command is given (There may be other occasions but I don't remember anything about AUTH failing). Thanks.
__________________ I might not be a pro, but I'm usually right |
| Abda92 is offline |
| | #15 |
| Cat without Hat Join Date: Apr 2003
Posts: 8,492
| The server may break the connection at any time for whatever reasons it wants. Any requirement to the contrary would put an absurd resource and security strain on the server, and no implementer would follow it. This is the SSMTP RFC: http://www.ietf.org/rfc/rfc2487.txt STARTTLS should be followed by a TLS handshake, not an AUTH command. The RFC doesn't say what happens if no TLS handshake follows, but clearly Google's server decides to abort the connection as a possible attack. Moreover, even after the handshake is done, you're not allowed to do an AUTH. You have to start over with an EHLO.
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law |
| CornedBee is offline |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Strange string behavior | jcafaro10 | C Programming | 2 | 04-07-2009 07:38 PM |
| socket message sending and receiving problem | black | C Programming | 5 | 01-15-2007 04:46 AM |
| Message class ** Need help befor 12am tonight** | TransformedBG | C++ Programming | 1 | 11-29-2006 11:03 PM |
| Dialog Box Problems | Morgul | Windows Programming | 21 | 05-31-2005 05:48 PM |
| Tab Controls - API | -KEN- | Windows Programming | 7 | 06-02-2002 09:44 AM |