CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2011

    Code hangs on using sendmail command of smtp ?

    I m trying to send a mail via c++ code. I downloaded EASendMail component installer in ma system.

    Then using this i wrote a code in vc++. I m sending a mail using a gmail id.I m able to send the mail correctly.
    The issue i m facing is that the code hangs for the time it takes to send the mail.when i get a notification tat the mail is sent , den the cursor becomes active.I send the receiver address to the function i m calling.

    using namespace EASendMailObjLib;
    using namespace std;

    int SendMail(LPTSTR RecieverID)
    ::CoInitialize( NULL );

    IMailPtr oSmtp = NULL;
    oSmtp.CreateInstance( "EASendMailObj.Mail");
    oSmtp->LicenseCode = L"TryIt";

    // Set your gmail email address
    oSmtp->FromAddr = L"mygmailid@gmail.com";

    // Add recipient email address

    oSmtp->AddRecipientEx( RecieverID, 0 );

    // Set email subject
    oSmtp->Subject = L"simple email from Visual C++ with gmail account";

    oSmtp->BodyText = L"Hi ";

    //adds attachment from local disk
    if(oSmtp->AddAttachment( L"images/imagename.bmp") != 0)
    printf("Failed to add attachment with error: %s\r\n", (const TCHAR*)oSmtp->GetLastErrDescription());


    // Gmail SMTP server address
    oSmtp->ServerAddr = L"smtp.gmail.com";

    // If you want to use direct SSL 465 port,
    // Please add this line, otherwise TLS will be used.
    // oSmtp->ServerPort = 465;

    // detect SSL/TLS automatically

    // Gmail user authentication should use your
    // Gmail email address as the user name.
    oSmtp->UserName = L"mygmailid@gmail.com";
    oSmtp->Password =L"passowrd123";
    printf("Start to send email via gmail account ...\r\n" );

    if( oSmtp->SendMail() == 0 )
    { printf("email was sent successfully!\r\n");

    { printf("failed to send email with the following error: %s\r\n",(const TCHAR*)oSmtp->GetLastErrDescription());

    if( oSmtp != NULL )

    return 0;

    I want the control to be active so that I can continue with other processes while the sending the mail happens on the background.
    Can anyone pls help me in this !!!
    Thankx a ton in advance.....


  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Hanover Germany

    Re: Code hangs on using sendmail command of smtp ?

    Please, edit your post changing the wrong written code tags to the correct ones (just use [] instead of <>)
    If you want to keep your main window responsible while sending e-mail and waiting for notification then create a worker thread and move the code for e-mail sending into it.
    Victor Nijegorodov

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.