Thread: Send email via c++

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    8

    Send email via c++

    hy, can somevone tell me how to send email using smtp server in c++????

    please...

  2. #2
    Banned
    Join Date
    Nov 2007
    Posts
    678
    i would like to add to the question. i have a yahoo account.
    how to write a program in C++ that can send e-mails using that Yahoo account?

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    8
    i found this:


    Code:
    #define _CRTDBG_MAP_ALLOC
    #include <windows.h>
    #include <stdio.h>
    #include <crtdbg.h>
    
    #include "C:/ck2000/components/ChilkatLib/Package/Include/CkSettings.h"
    #include "C:/ck2000/components/ChilkatLib/Package/Include/CkEmail.h"
    #include "C:/ck2000/components/ChilkatLib/Package/Include/CkEmailBundle.h"
    #include "C:/ck2000/components/ChilkatLib/Package/Include/CkMailMan.h"
    #include "C:/ck2000/components/ChilkatLib/Package/Include/CkString.h"
    #include "C:/ck2000/components/ChilkatLib/Package/Include/CkByteData.h"
    
    void EmailExample(void)
        {
        CkMailMan mailman;
    
        // Any string passed to UnlockComponent begins the 30-day trial.
        bool unlocked = mailman.UnlockComponent("30-day trial");
        if (!unlocked)
    	{
    	printf("Failed to unlock component\n");
    	return;
    	}
    
        mailman.put_SmtpHost("mail.earthlink.net");
        
        // Send an email with a file attachment.
        CkEmail email;
    
        email.put_Body("This is the body");
    
        email.AddTo("Chilkat Support","[email protected]");
        
        email.put_FromAddress("[email protected]");
    
        email.put_Subject("This is a test");
    
        // Add a file attachment.
        if (!email.AddFileAttachment("dudeCPP.gif",0))
    	{
    	email.SaveLastError("errors.xml");
    	return;
    	}
    
        // Send the email
        if (!mailman.SendEmail(&email))
    	{
    	mailman.SaveLastError("errors.xml");
    	}
    
        }
    
    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    {
        freopen("stdout.txt","w",stdout);
    
        EmailExample();
    
        CkSettings::cleanupMemory();
    
        _CrtDumpMemoryLeaks();
    
        return 0;
    }
    but it doens't work...where is problem?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    How the heck are we supposed to know!? Describe the problem and post any compile errors and where (on what line) they are.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Did you also find the ChilkatLib that it appears to reference?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    8
    if found all error except one...

    crtdbg.h - it is missing, how can i install that?
    Last edited by davidboja; 11-30-2007 at 11:20 AM.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    903
    Obviously you need to download the API ...

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    8
    hm...i didn't find link to download!

    can you give me link?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    crtdbg.h is part of Visual Studio tools, and I suspect you can actually live well without that, if you just remove the functions that uses it, which is particular "_CrtDumpMemoryLeaks();".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    Download this: http://sourceforge.net/projects/poco, read the documentation, and use it. POCO will be very usefull to you, since it adds some other API&#180;s to work with XML, Net, etc... Chilkat isn&#180;t nice, it is payed and i don&#180;t like it...

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    8
    it doens't work....program start...and email doesn't came!

  12. #12
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Junk classes. Use the pure!
    Code:
    // Yuri Zarechny for News2News (c) 2006 
    
    #include "stdafx.h" 
    //#include < windows.h > 
    #include < winsock2.h >  //link ws2_32.lib 
    #include "conio.h" 
    
    struct _sendmail { 
        LPSTR host; 
        LPSTR IP; 
        LPSTR sender; 
        LPSTR recip; 
        LPSTR subject; 
        LPSTR body; 
        SOCKET hSocket; 
    } sendmail; 
    
    BOOL InitSock(LPSTR Server) 
    { 
        WSADATA wsd; 
        if (WSAStartup(MAKEWORD(2,2), &wsd) > 0) 
        { 
            printf("Winsock init error: &#37;d\n", WSAGetLastError()); 
            return false; 
        } 
        return true; 
    } 
    
    LPSTR GetIP(LPSTR h) 
    { 
        in_addr ia; 
        hostent* hbn; 
        unsigned int addr; 
    
        if (isalpha(h[0])) hbn = gethostbyname(h); 
        else 
        { 
            addr = inet_addr(h); 
            hbn = gethostbyaddr((char *)&addr, 4, AF_INET); 
        } 
    
        if (hbn==0) return ""; 
        ia.S_un.S_addr=*(DWORD *)hbn->h_addr_list[0]; 
        return inet_ntoa(ia); 
    } 
    
    BOOL ValidateEnvelope(LPSTR h, LPSTR r, LPSTR s, LPSTR *ip) 
    { 
        if (h=="" || r=="" || s=="") 
        { 
            printf("Invalid host or sender/recipient email address\n"); 
            return false; 
        } 
        *ip=GetIP(h); 
        if (*ip=="") 
        { 
            printf("Can not resolve host name %s to ip address\n", h); 
            return false; 
        } 
        return true; 
    } 
    
    BOOL ConnectTo() 
    { 
        const u_short SMTP_PORT=25; 
        sockaddr_in sa; 
        sa.sin_family=AF_INET; 
        sa.sin_addr.S_un.S_addr=inet_addr(sendmail.IP); 
        sa.sin_port=htons(SMTP_PORT); 
        return (connect(sendmail.hSocket, (SOCKADDR *)&sa, sizeof(sa))==0); 
    } 
    
    BOOL snd(LPSTR txt, bool resp) 
    { 
        char tx[4096]; 
        sprintf(tx, "%s\r\n", txt); 
    
        if (send(sendmail.hSocket, tx, strlen(tx),0)==SOCKET_ERROR) 
        { 
            printf("%s%s\n", tx, " - send operation failed."); 
            return false; 
        } 
        if (!resp) return true; 
    
        while (true) 
        { 
            char rcv[1024]; 
            char outp[255]=""; 
    
            HANDLE hEvent=WSACreateEvent(); 
            WSAEventSelect(sendmail.hSocket,hEvent,FD_READ); 
            DWORD nWait=WSAWaitForMultipleEvents(1, &hEvent, 0, 1000, 0); 
            WSACloseEvent(hEvent); 
            if (nWait!=0) return false; 
            recv(sendmail.hSocket, rcv, sizeof(rcv),0); 
    
            strncpy(outp, rcv, strcspn(rcv, "\n")+1); 
            printf("%s\n", outp); 
        } 
        return true; 
    } 
    
    BOOL SendMail() 
    { 
        if (!ValidateEnvelope(sendmail.host, sendmail.recip, 
            sendmail.sender, &sendmail.IP)) return false; 
    
        char tmp[255]; 
        if (sendmail.sender=="" || sendmail.recip=="" || 
            sendmail.body=="" || sendmail.subject=="") return false; 
    
        sendmail.hSocket=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 
        if (sendmail.hSocket==SOCKET_ERROR) return false; 
    
        if (!ConnectTo()) 
        { 
            printf("Unable to connect to the server.\n"); 
            return false; 
        } 
        snd("HELO", true); 
    
        sprintf(tmp, "%s%s%s", "MAIL FROM:<", sendmail.sender, ">"); 
        snd(tmp, true); 
        sprintf(tmp, "%s%s%s", "RCPT TO:<", sendmail.recip, ">"); 
        snd(tmp, true); 
    
        snd("DATA", true); 
        sprintf(tmp, "%s%s", "From: ", sendmail.sender); 
        snd(tmp, false); 
        sprintf(tmp, "%s%s", "To: ", sendmail.recip); 
        snd(tmp, false); 
        sprintf(tmp, "%s%s", "Subject: ", sendmail.subject); 
        snd(tmp, false); 
        snd("", false); 
        snd(sendmail.body ,false); 
        snd(".", true); 
    
        snd("NOOP", true); 
        snd("QUIT", true); 
        closesocket(sendmail.hSocket); 
    
        return true; 
    } 
    
    void _tmain() { 
        // provide valid host name and email addresses 
        sendmail.host = "smtp.myisp.ca"; 
        sendmail.sender = "[email protected]"; 
        sendmail.recip = "[email protected]"; 
    
        sendmail.subject = "Testing Winsock SMTP functionality"; 
        sendmail.body = "Test message:\n\n" 
            "Windows Sockets (Winsock) provides a general-purpose networking " 
            "application programming interface (API) based on the socket " 
            "interface from the University of California at Berkeley.\n"; 
    
        if (InitSock(sendmail.host)) 
        { 
            SendMail(); 
            WSACleanup(); 
        } 
    
        printf("\nPress ENTER to quit"); 
        getch(); 
        return; 
    }

  13. #13
    Registered User
    Join Date
    Nov 2007
    Posts
    8
    tnx...but when i run this email doesn't come...??????

  14. #14
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    Maybe your SMTP requires authentication... Use POCO, that itīs implementation support authentication.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to send email using c
    By moussa in forum C++ Programming
    Replies: 7
    Last Post: 05-28-2008, 08:19 PM
  2. Spam Filters. ISP based email is dying.
    By Mario F. in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 03-05-2008, 12:05 PM
  3. Send email
    By Abda92 in forum Windows Programming
    Replies: 4
    Last Post: 12-29-2007, 09:09 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. How to Send Email
    By sampatel in forum C Programming
    Replies: 1
    Last Post: 11-23-2002, 02:07 PM