Telnet Email problem

This is a discussion on Telnet Email problem within the C++ Programming forums, part of the General Programming Boards category; I'm trying to send an email using telnet in C++. I've done it before in regular command prompt, the log ...

  1. #1
    Registered User briceman92's Avatar
    Join Date
    Jun 2006
    Posts
    7

    Exclamation Telnet Email problem

    I'm trying to send an email using telnet in C++. I've done it before in regular command prompt, the log went like this:
    Code:
    telnet /f C:\log.txt mail.mycbsi.com 25
    220 pbrane.mycbsi.net ESMTP Postfix
    helo x
    250 pbrane.mycbsi.net
    mail from:me@myserver.com
    250 Ok
    rcpt to:briceman92@gmail.com
    250 Ok
    data
    354 Please start mail input.
    This is a test message
    .
    250 Mail queued for delivery.
    quit
    221 Closing connection. Good bye.
    
    C:\>exit
    And the email was sent, it worked. Now I'm trying to send the same email through C++:

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        system("telnet mail.mycbsi.com 25");
        system("helo x");
        system("mail from: <myemail@myserver.com>");
        system("rcpt to: <briceman92@gmail.com>");
        system("data");
        system("Test Message");
        system(".");
        system("quit");
        system("pause");
    }
    When I run the program, the only thing that comes up is:
    Code:
    Connecting to mail.mycbsi.com...
    ^That flashes quickly^ And then:
    Code:
    220 pbrane.mycbsi.net ESMTP Postfix
    Then nothing happens, it sits there. I think the problem is that the commands are executed too fast, and by the time telnet has connected, the commands are already run.

    I also have another problem. I'm trying to make it so you can type in the command to execute on the system. I've tried this:
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        char cmd[11];
        cout<<"Enter the command: ";
        cin>>cmd;
        system(cmd);
        system("pause");
    }
    but no luck, it doesn't work. Overall, I'm trying to make a program where whatever is input into the program is emailed to me. To do this, I need to be able to input a variable into the system.

    Thanks for any help in advance!

  2. #2
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103
    I'm no expert, but I think it's better to create a batch file intead of a C++ Project
    Be easy on me...only 14

  3. #3
    Registered User briceman92's Avatar
    Join Date
    Jun 2006
    Posts
    7
    Ok, I'm having an idea here, because I need it to be a program, I'm going to tell C++ to create a batch file with everything I need in it, and then have it execute it. When it's done, delete the batch file! Thanks though

  4. #4
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103
    well, this is just my opinion, perhaps lame,
    you can create a text file, with batch file commands,
    make your program execute the batch file,
    execute it,
    then delete,
    you'll need either fstream or io.h
    Be easy on me...only 14

  5. #5
    Registered User briceman92's Avatar
    Join Date
    Jun 2006
    Posts
    7
    sounds good, but I'm still in the dark about how to execute what the user has put in through command prompt.
    Code:
    system(var);
    doesn't work, any ideas?

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,666
    I'm a bit confused as to why C++ matters at all:

    1. Open notepad.
    2. Write the commands.
    3. Save as bat.
    4. ????
    5. PROFIT!

    But anyway, you need to pass a valid command to system in the form of a string. Variables are often not commands.

  7. #7
    Registered User briceman92's Avatar
    Join Date
    Jun 2006
    Posts
    7
    I need it to be in C++ because I want the user to be able to edit what is in the batch file. i.e.: the address that the email is being sent to, and the message.

    Ok, but how do I code that? I can make it a string, but I get an error message anyway I try to do it:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
        string cmd;
        cout<<"Enter the command: ";
        cin>>cmd;
        system(cmd); //or system('cmd'); error either way
        system("pause");
    }
    Last edited by briceman92; 06-30-2006 at 02:31 AM.

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,666
    The problem is that string is not just a simple character array like system expects. The c_str method can be used for situations like these.

    > system(cmd.c_str());
    fixed.

    For your benefit I use the terms string and character array interchangeably, but all library functions want character arrays.

  9. #9
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103
    Or you can always do Shell Execute
    Code:
    //prototype
    ShellExecute(NULL, "open", /*Your .bat here: */, NULL, NULL, SW_SHOWNORMAL);
    Be easy on me...only 14

  10. #10
    Registered User briceman92's Avatar
    Join Date
    Jun 2006
    Posts
    7

    Unhappy

    Good news and bad news. I don't get an error message, but when I run the program, after I put in the command and press enter, the program terminates.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
        string cmd;
        cout<<"Enter the command: ";
        cin>>cmd;
        system(cmd.c_str());
        system("pause");
    }

  11. #11
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    Your original attempt fails because telnet does not work the way you seem to think it does. Creating a batch file to do it will result in the same problem.

    When you start the telnet program, everything else stops and waits for the telnet program to finish before moving on. That means any other programs, including your program or the batch file you are trying to create, are waiting. When/if telnet ever ends, the rest of the commands will execute, but they won't go to telnet since that will have finished running already.

    If you take your original program
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        system("telnet mail.mycbsi.com 25");
        system("helo x");
        system("mail from: <myemail@myserver.com>");
        system("rcpt to: <briceman92@gmail.com>");
        system("data");
        system("Test Message");
        system(".");
        system("quit");
        system("pause");
    }
    and run it again, when the
    Code:
    220 pbrane.mycbsi.net ESMTP Postfix
    shows up, start typing your comamnds again then quit. What you'll see is that telnet responds to your commands, and then you will get
    Code:
    'helo' is not recognized as an internal or external command, operable program or batch file.
    (or whatever your dos box error is for the flavor of windows you are running) followed by the same error message for each command that follows until your C++ program is done.

    If you need to make a program to do the e-mail over a telnet session, you're going to have to learn some sockets programming.

    Also, using the system() function like that is asking for problems. It is a good habit to avoid getting into. The occasional system() call for debugging purposes when you are the only person running the program can sometimes prove useful, and there are other situations where the system() call might be warranted, but on the whole allowing another program to begin execution in the midst of your program running is opening things up for security vulnerabilities.
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

  12. #12
    Registered User briceman92's Avatar
    Join Date
    Jun 2006
    Posts
    7

    Lightbulb

    Wow, thanks. I tried the batch file, and it didn't work, as you said. I also did what you said in my origional attempt, and again, it did what you said. I will look in to socket programing, but I guess that all I really want is to be able to ask the user a question, and then email myself their response. Thanks for your help though.

  13. #13
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You could try open telnet throuh a pipe and pipe your commands to it. I don't know, never used telnet before. Should work if it's an executable.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  14. #14
    Registered User briceman92's Avatar
    Join Date
    Jun 2006
    Posts
    7

    Question

    Quote Originally Posted by jafet
    You could try open telnet throuh a pipe and pipe your commands to it. I don't know, never used telnet before. Should work if it's an executable.
    Pipe?

  15. #15
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    Quote Originally Posted by jafet
    You could try open telnet throuh a pipe and pipe your commands to it. I don't know, never used telnet before. Should work if it's an executable.
    Piping info into a program means to take some output and use it for some input. A common use is
    Code:
    type sometext.txt | more
    (Note: the | is the vertical bar on the backslash key) In this example, the type program "types" all the information from the text file, and then the dosbox uses that as input for the More program. Find a rather large text file and type the above, replacing sometext.txt with the name of the text file, and then do it again without the | more and see what the difference is.

    There are also other ways to pipe data, do a google search if you are interested.

    jafet's suggestion is to take the list of commands that you want to go to the telnet program and pipe them in from the command line. To do that, you would do
    Code:
    type commands.txt | telnet mail.mycbsi.com 25
    Assuming commands.txt are the commands you want to use, telnet would start and then process all the input from the commands.txt file. You could have your program write the commands to a file (filling in the particulars that will change each time, the answers to the questions the user will type in) and then type the commands.txt as above.

    There are quite a number of gotchas that would accompany this kind of attempt to use telnet, there aren't many ways to handle errors and you wouldn't learn very much from this method in my opinion. Learning socket programming isn't much easier, but in the long run you would learn more about programming and be able to do more than just this one limited case.
    Last edited by jEssYcAt; 06-30-2006 at 07:50 PM.
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  2. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  3. crashing problem in c++
    By Enki in forum C++ Programming
    Replies: 6
    Last Post: 11-08-2002, 12:21 AM
  4. email client with C++
    By mwagiru in forum C++ Programming
    Replies: 0
    Last Post: 02-14-2002, 12:17 AM

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