sending message to GMAIL using SMTP

This is a discussion on sending message to GMAIL using SMTP within the Networking/Device Communication forums, part of the General Programming Boards category; Hi, I was trying to send a message to my gmail account using a program I found on the web. ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    230

    sending message to GMAIL using SMTP

    Hi,
    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:
    Connecting....
    220 mx.google.com ESMTP l12si10852221fgb.8

    helo me.somepalace.com
    250 mx.google.com at your service

    MAIL FROM:<me1@gmail.com>
    250 2.1.0 OK

    RCPT TO:<me2@gmail.com>
    250 2.1.5 OK

    DATA
    354 Go ahead

    To:me1@gmail.com
    From:me2@gmail.com
    Subject:Logged

    .

    550-5.7.1 [87.109.73.118] The IP you're using to send email is not authorized

    550-5.7.1 to send email directly to our servers. Please use

    550 5.7.1 the SMTP relay at your service provider instead. l12si10852221fgb.8

    quit
    221 2.0.0 mx.google.com closing connection l12si10852221fgb.8
    Gmail is obviously not allowing me to send emails using my IP. But I don't know much about networking. All I know is how to setup my home network...
    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'm sorry if the code wasn't written very well, but I can't fix it because I don't understand it
    Last edited by Abda92; 02-20-2008 at 10:32 AM.
    I might not be a pro, but I'm usually right

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #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

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #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

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,433
    Moved to Networking/Device Communication forum.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #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

  9. #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?
    Last edited by Abda92; 02-25-2008 at 04:38 AM.
    I might not be a pro, but I'm usually right

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Have you tried testing it on a local server?

  11. #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

  12. #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

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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

  14. #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 don't understand why the connection is broken. I tried AUTH LOGIN too, but yielded the same results...

    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

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange string behavior
    By jcafaro10 in forum C Programming
    Replies: 2
    Last Post: 04-07-2009, 07:38 PM
  2. socket message sending and receiving problem
    By black in forum C Programming
    Replies: 5
    Last Post: 01-15-2007, 03:46 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Dialog Box Problems
    By Morgul in forum Windows Programming
    Replies: 21
    Last Post: 05-31-2005, 05:48 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21