Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. SMTP: all are not working?

SMTP: all are not working?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    includeh10
    wrote on last edited by
    #1

    I developed a SMTP tool several years ago (using win 98) and it worked well. Now I need to send email in my app, so I test the SMTP tool again - it is not working now. I downloaded several samples from CP articles, all of them are not working. I am wondering why? Is my OS (win2k) problem or anti-virus software blocked mail or something else? Any suggestion?

    Z B I 3 Replies Last reply
    0
    • I includeh10

      I developed a SMTP tool several years ago (using win 98) and it worked well. Now I need to send email in my app, so I test the SMTP tool again - it is not working now. I downloaded several samples from CP articles, all of them are not working. I am wondering why? Is my OS (win2k) problem or anti-virus software blocked mail or something else? Any suggestion?

      Z Offline
      Z Offline
      zhu_lin
      wrote on last edited by
      #2

      check whether your SMTP server is open. and what's your smtp function return value?

      it's my pleasure to make friend with you.

      1 Reply Last reply
      0
      • I includeh10

        I developed a SMTP tool several years ago (using win 98) and it worked well. Now I need to send email in my app, so I test the SMTP tool again - it is not working now. I downloaded several samples from CP articles, all of them are not working. I am wondering why? Is my OS (win2k) problem or anti-virus software blocked mail or something else? Any suggestion?

        B Offline
        B Offline
        Bacon Ultimate Cheeseburger
        wrote on last edited by
        #3

        includeh10 wrote:

        anti-virus software blocked mail

        Anti-virus and firewall software is one of the most common causes of the problem you are experiencing. It's also possible that your ISP is blocking port 25 (some do, some don't) in an effort to reduce spam on their network. Without more information such as what operation it's failing on (connect, send, etc.) it's hard to tell. Have you tried disabling your anti-virus and/or firewall and then running your app?

        I am a lean mean ground beef machine!!!

        I 1 Reply Last reply
        0
        • B Bacon Ultimate Cheeseburger

          includeh10 wrote:

          anti-virus software blocked mail

          Anti-virus and firewall software is one of the most common causes of the problem you are experiencing. It's also possible that your ISP is blocking port 25 (some do, some don't) in an effort to reduce spam on their network. Without more information such as what operation it's failing on (connect, send, etc.) it's hard to tell. Have you tried disabling your anti-virus and/or firewall and then running your app?

          I am a lean mean ground beef machine!!!

          I Offline
          I Offline
          includeh10
          wrote on last edited by
          #4

          Here is info: 1. none of commands return SOCKET_ERROR. 2. for QUIT command, ::recv() returns zero - this should be OK 3. ::gethostbyname("www.mycompany.com") - I use my company's domain, is this problem? Thanks.

          B 1 Reply Last reply
          0
          • I includeh10

            I developed a SMTP tool several years ago (using win 98) and it worked well. Now I need to send email in my app, so I test the SMTP tool again - it is not working now. I downloaded several samples from CP articles, all of them are not working. I am wondering why? Is my OS (win2k) problem or anti-virus software blocked mail or something else? Any suggestion?

            I Offline
            I Offline
            includeh10
            wrote on last edited by
            #5

            Here are all test code, are there errors?

            #define CHECK(iStatus) \
            if(iStatus==SOCKET_ERROR) return 0;\
            if(iStatus==0) return 0

            #define EOL "\r\n"

            BOOL TestSend()
            {
            const char*pszServer="www.mycompany";
            const char*pszSmtpEmail ="myemail@hotmail.com";
            const char*pszSenderEmail ="myemail@hotmail.com";
            const char*pszSubject ="my subject";
            const char*pszReceiver ="myemail@hotmail.com";
            const char*pszBody ="the body";
            const char*pszSenderName ="myemail@hotmail.com";

            char	sz\[1024\];
            char	wa\[1024\];
            char	szBuffer\[4096\];
            char	waTime\[270\];
            
            int iRet;
            //-------------------------
            HOSTENT\* pHost=::gethostbyname(pszServer);
            if(pHost==0) 	return 0;
            SOCKET hSock=socket(PF\_INET,SOCK\_STREAM,0); 
            if(hSock==INVALID\_SOCKET) return 0;
            
            SERVENT\*pServer=::getservbyname("mail",0);
            
            const int iPort=pServer?pServer->s\_port:IPPORT\_SMTP;
            
            SOCKADDR\_IN    sockAddr;
            sockAddr.sin\_family	=AF\_INET;
            sockAddr.sin\_port	=iPort;
            sockAddr.sin\_addr	=\*((LPIN\_ADDR)\*pHost->h\_addr\_list);
            
            if(::connect(hSock,(PSOCKADDR) &sockAddr,sizeof(sockAddr)))
            {
            	return 0;
            }
            iRet=::recv(hSock,szBuffer,sizeof(szBuffer),0);
            // Receive initial response from SMTP server.
            CHECK(iRet);
            
            // Send HELO 
            wsprintf(sz,"HELO %s%s","microsoft \[111.122.1.12\]",EOL);
            CHECK(::send(hSock,sz,strlen(sz),0));
            CHECK(::recv(hSock,szBuffer,sizeof(szBuffer),0));
            
            // Send MAIL FROM:
            wsprintf(sz,"MAIL FROM: <%s>%s",pszSmtpEmail,EOL);
            CHECK(::send(hSock,sz,strlen(sz),0));
            CHECK(::recv(hSock,szBuffer,sizeof(szBuffer),0));
            
            // Send RCPT TO: 
            wsprintf(sz,"RCPT To: <%s>%s",pszReceiver,EOL);
            CHECK(::send(hSock,sz,strlen(sz),0));
            CHECK(::recv(hSock,szBuffer,sizeof(szBuffer),0));
            
            // Send DATA.
            wsprintf(sz,"DATA%s",EOL);
            CHECK(::send(hSock,sz,strlen(sz),0));
            CHECK(::recv(hSock,szBuffer,sizeof(szBuffer),0));
            
            ///szHead
            strcpy(waTime,T("Date: "));
            ::GetDateFormat(0x409,0,0,T("ddd,dd MMM yyyy"),wa,200);
            strcat(waTime,wa);
            strcat(waTime,T(" "));
            ::GetTimeFormat(0x409,0,0,T("HH:mm:ss"),wa,200);
            strcat(waTime,wa);
            
            char szHead\[350\];
            
            strcpy(szHead,"From: ");
            strcat(szHead,pszSenderName );
            strcat(szHead,"<");
            strcat(szHead,pszSenderEmail);
            strcat(szHead,">");
            strcat(szHead,"\\r\\n");
            strcat(szHead,"To: ");
            strcat(szHead,pszReceiver);
            strcat(szHead,"\\r\\n");
            strcat(szHead,"Subject: ");
            strcat(szHead,pszSubject );
            strcat(szHead,"\\r\\n");
            strcat(szHead,waTime);
            strcat(szHead,"\\r\\n");
            strcat(szHead,"X-Mailer: Outlook Express 1.00\\r\\nMIME-Ver
            
            B 2 Replies Last reply
            0
            • I includeh10

              Here is info: 1. none of commands return SOCKET_ERROR. 2. for QUIT command, ::recv() returns zero - this should be OK 3. ::gethostbyname("www.mycompany.com") - I use my company's domain, is this problem? Thanks.

              B Offline
              B Offline
              Bacon Ultimate Cheeseburger
              wrote on last edited by
              #6

              If your SMTP client does not handle authentication the first thing I would check is to make sure that the server you are connecting to does not require it. Also make sure that the server allows relaying from the location you are connecting from (it probably does if it's an internal company server but it's always best to make sure). If that doesn't provide any answers I would load up a packet sniffer and monitor the transmissions between your application and the SMTP server. This should give you a better idea of how well the server is responding to your client. It's possible the server is returning an error that your client is not handling (hard to say without seeing the code). If neither of those provide you with any answers you might want to try installing a bare bones SMTP server on a test machine and seeing if your client (and the other examples you have downloaded) works with it.

              I am a lean mean ground beef machine!!!

              1 Reply Last reply
              0
              • I includeh10

                Here are all test code, are there errors?

                #define CHECK(iStatus) \
                if(iStatus==SOCKET_ERROR) return 0;\
                if(iStatus==0) return 0

                #define EOL "\r\n"

                BOOL TestSend()
                {
                const char*pszServer="www.mycompany";
                const char*pszSmtpEmail ="myemail@hotmail.com";
                const char*pszSenderEmail ="myemail@hotmail.com";
                const char*pszSubject ="my subject";
                const char*pszReceiver ="myemail@hotmail.com";
                const char*pszBody ="the body";
                const char*pszSenderName ="myemail@hotmail.com";

                char	sz\[1024\];
                char	wa\[1024\];
                char	szBuffer\[4096\];
                char	waTime\[270\];
                
                int iRet;
                //-------------------------
                HOSTENT\* pHost=::gethostbyname(pszServer);
                if(pHost==0) 	return 0;
                SOCKET hSock=socket(PF\_INET,SOCK\_STREAM,0); 
                if(hSock==INVALID\_SOCKET) return 0;
                
                SERVENT\*pServer=::getservbyname("mail",0);
                
                const int iPort=pServer?pServer->s\_port:IPPORT\_SMTP;
                
                SOCKADDR\_IN    sockAddr;
                sockAddr.sin\_family	=AF\_INET;
                sockAddr.sin\_port	=iPort;
                sockAddr.sin\_addr	=\*((LPIN\_ADDR)\*pHost->h\_addr\_list);
                
                if(::connect(hSock,(PSOCKADDR) &sockAddr,sizeof(sockAddr)))
                {
                	return 0;
                }
                iRet=::recv(hSock,szBuffer,sizeof(szBuffer),0);
                // Receive initial response from SMTP server.
                CHECK(iRet);
                
                // Send HELO 
                wsprintf(sz,"HELO %s%s","microsoft \[111.122.1.12\]",EOL);
                CHECK(::send(hSock,sz,strlen(sz),0));
                CHECK(::recv(hSock,szBuffer,sizeof(szBuffer),0));
                
                // Send MAIL FROM:
                wsprintf(sz,"MAIL FROM: <%s>%s",pszSmtpEmail,EOL);
                CHECK(::send(hSock,sz,strlen(sz),0));
                CHECK(::recv(hSock,szBuffer,sizeof(szBuffer),0));
                
                // Send RCPT TO: 
                wsprintf(sz,"RCPT To: <%s>%s",pszReceiver,EOL);
                CHECK(::send(hSock,sz,strlen(sz),0));
                CHECK(::recv(hSock,szBuffer,sizeof(szBuffer),0));
                
                // Send DATA.
                wsprintf(sz,"DATA%s",EOL);
                CHECK(::send(hSock,sz,strlen(sz),0));
                CHECK(::recv(hSock,szBuffer,sizeof(szBuffer),0));
                
                ///szHead
                strcpy(waTime,T("Date: "));
                ::GetDateFormat(0x409,0,0,T("ddd,dd MMM yyyy"),wa,200);
                strcat(waTime,wa);
                strcat(waTime,T(" "));
                ::GetTimeFormat(0x409,0,0,T("HH:mm:ss"),wa,200);
                strcat(waTime,wa);
                
                char szHead\[350\];
                
                strcpy(szHead,"From: ");
                strcat(szHead,pszSenderName );
                strcat(szHead,"<");
                strcat(szHead,pszSenderEmail);
                strcat(szHead,">");
                strcat(szHead,"\\r\\n");
                strcat(szHead,"To: ");
                strcat(szHead,pszReceiver);
                strcat(szHead,"\\r\\n");
                strcat(szHead,"Subject: ");
                strcat(szHead,pszSubject );
                strcat(szHead,"\\r\\n");
                strcat(szHead,waTime);
                strcat(szHead,"\\r\\n");
                strcat(szHead,"X-Mailer: Outlook Express 1.00\\r\\nMIME-Ver
                
                B Offline
                B Offline
                Bacon Ultimate Cheeseburger
                wrote on last edited by
                #7

                For future reference please wrap your code postings in a <pre></pre> code block so that it's easier to read.

                I am a lean mean ground beef machine!!!

                1 Reply Last reply
                0
                • I includeh10

                  Here are all test code, are there errors?

                  #define CHECK(iStatus) \
                  if(iStatus==SOCKET_ERROR) return 0;\
                  if(iStatus==0) return 0

                  #define EOL "\r\n"

                  BOOL TestSend()
                  {
                  const char*pszServer="www.mycompany";
                  const char*pszSmtpEmail ="myemail@hotmail.com";
                  const char*pszSenderEmail ="myemail@hotmail.com";
                  const char*pszSubject ="my subject";
                  const char*pszReceiver ="myemail@hotmail.com";
                  const char*pszBody ="the body";
                  const char*pszSenderName ="myemail@hotmail.com";

                  char	sz\[1024\];
                  char	wa\[1024\];
                  char	szBuffer\[4096\];
                  char	waTime\[270\];
                  
                  int iRet;
                  //-------------------------
                  HOSTENT\* pHost=::gethostbyname(pszServer);
                  if(pHost==0) 	return 0;
                  SOCKET hSock=socket(PF\_INET,SOCK\_STREAM,0); 
                  if(hSock==INVALID\_SOCKET) return 0;
                  
                  SERVENT\*pServer=::getservbyname("mail",0);
                  
                  const int iPort=pServer?pServer->s\_port:IPPORT\_SMTP;
                  
                  SOCKADDR\_IN    sockAddr;
                  sockAddr.sin\_family	=AF\_INET;
                  sockAddr.sin\_port	=iPort;
                  sockAddr.sin\_addr	=\*((LPIN\_ADDR)\*pHost->h\_addr\_list);
                  
                  if(::connect(hSock,(PSOCKADDR) &sockAddr,sizeof(sockAddr)))
                  {
                  	return 0;
                  }
                  iRet=::recv(hSock,szBuffer,sizeof(szBuffer),0);
                  // Receive initial response from SMTP server.
                  CHECK(iRet);
                  
                  // Send HELO 
                  wsprintf(sz,"HELO %s%s","microsoft \[111.122.1.12\]",EOL);
                  CHECK(::send(hSock,sz,strlen(sz),0));
                  CHECK(::recv(hSock,szBuffer,sizeof(szBuffer),0));
                  
                  // Send MAIL FROM:
                  wsprintf(sz,"MAIL FROM: <%s>%s",pszSmtpEmail,EOL);
                  CHECK(::send(hSock,sz,strlen(sz),0));
                  CHECK(::recv(hSock,szBuffer,sizeof(szBuffer),0));
                  
                  // Send RCPT TO: 
                  wsprintf(sz,"RCPT To: <%s>%s",pszReceiver,EOL);
                  CHECK(::send(hSock,sz,strlen(sz),0));
                  CHECK(::recv(hSock,szBuffer,sizeof(szBuffer),0));
                  
                  // Send DATA.
                  wsprintf(sz,"DATA%s",EOL);
                  CHECK(::send(hSock,sz,strlen(sz),0));
                  CHECK(::recv(hSock,szBuffer,sizeof(szBuffer),0));
                  
                  ///szHead
                  strcpy(waTime,T("Date: "));
                  ::GetDateFormat(0x409,0,0,T("ddd,dd MMM yyyy"),wa,200);
                  strcat(waTime,wa);
                  strcat(waTime,T(" "));
                  ::GetTimeFormat(0x409,0,0,T("HH:mm:ss"),wa,200);
                  strcat(waTime,wa);
                  
                  char szHead\[350\];
                  
                  strcpy(szHead,"From: ");
                  strcat(szHead,pszSenderName );
                  strcat(szHead,"<");
                  strcat(szHead,pszSenderEmail);
                  strcat(szHead,">");
                  strcat(szHead,"\\r\\n");
                  strcat(szHead,"To: ");
                  strcat(szHead,pszReceiver);
                  strcat(szHead,"\\r\\n");
                  strcat(szHead,"Subject: ");
                  strcat(szHead,pszSubject );
                  strcat(szHead,"\\r\\n");
                  strcat(szHead,waTime);
                  strcat(szHead,"\\r\\n");
                  strcat(szHead,"X-Mailer: Outlook Express 1.00\\r\\nMIME-Ver
                  
                  B Offline
                  B Offline
                  Bacon Ultimate Cheeseburger
                  wrote on last edited by
                  #8

                  Disregard my last post about recv()...my meds are making me way too fuzzy :) Ok, I was able to compile and run it and everything worked fine after a couple of changes. The first problem is with the CHECK() macro:

                  #define CHECK(iStatus) \
                  if(iStatus==SOCKET_ERROR) return 0;\
                  if(iStatus==0) return 0

                  DO NOT DO THIS! When this macro is expanded by the preprocessor the send or recv operation passed as iStatus will be executed twice:

                  if(::send(hSock,sz,strlen(sz),0)==SOCKET_ERROR) return 0;
                  if(::send(hSock,sz,strlen(sz),0)==0) return 0

                  Instead you might try something like this (until you get everything fixed):

                  #define CHECK(iStatus) \
                  { \
                  int result = iStatus; \
                  if(result==SOCKET_ERROR) return 0; \
                  if(result==0) return 0; \
                  }

                  Every email client I tried did not like the date format included in the message header. The following change should work:

                  ::GetDateFormat(0x409,0,0,T("dd MMM yyyy"),wa,200);

                  I am a lean mean ground beef machine!!!

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups