locked
Smtp Mail in Metro C++/CX App

    Question

  • Hi all,

        I need to send a email[one specific domain to any other domain] from metro cx app with out using charm mail window and  found that there is no namespace for mail in cx metro app. For these reasons i wrote a sample app to test smtp commands(HELO,MAIL FROM,RCPT To etc.) using stream sockets and this experiments works fine when i use "gmail-smtp-in.l.google.com" server as outgoing smtp server[Using this server recipient must be gmail account]. I tried these outgoing servers “smtp.live.com” and “smtp.gmail.com” port no:25, but in both case server respond with message “Must Issue a StartTLS command first” after sending “HELO” and “Mail From” command. So this means Server needs a secure connections, So How can Implement the secure socket connection in metro app? IS it possible to handle the TLS connection issues in Metro app? 

    When i try to use task<void> connectTask(sock->ConnectAsync(hostName , serverPort, Windows::Networking::Sockets::SocketProtectionLevel::Ssl)); in socket connection for the above issue, the application will crash, so how can handle the secure/encrypted socket connection for those mail servers? Please Help

    the sample code 

    // Initialize the Socket, DataReader.
    	task<void> initTask([this,serverHostName,serverPort,&result]() 
        {
            this->socket	 = ref new StreamSocket();
            this->readPacket = ref new DataReader(this->socket->InputStream);
            this->readPacket->InputStreamOptions = InputStreamOptions::Partial;
            result = ConnectSocket(this->socket, serverHostName, serverPort);
        });
        // Wait the task.
    	initTask.wait();
    
    //-----------------------------------------
    
    try
    		{
    
    			HostName^ hostName = ref new HostName(serverHostName);
    			task<void> connectTask(sock->ConnectAsync(hostName , serverPort));
    			connectTask.wait();
    			result = true;
    		}
    		catch (COMException^ e)
    		{ 
    		}
    //---------------------------------------------------------
    

    		{ 
    		}

    Thanks,

    Anil.S

    Friday, August 31, 2012 6:15 AM

Answers

  • Thanks, actually it was my mistake when pasting code here.[wait() not calling in UI thread].

    Now it is Working fine with "smtp.gmail.com" server [port 465] , When i changed the Smtp command sequence like this
    1. Connect the server [using SocketProtectionLevel::Ssl]
    2. EHLO 3. AUTH LOGIN, then send authentication details(username & password) in Base64 format.
    4. MAIL FROM: <>    5. RCPT TO: <> 6. DATA  7. (DOT). 8. QUIT.

    But "smtp.live.com" will not working with the above sequence and after connection there is no response from server.
    and also try with ports 25 & 587 without using  "SocketProtectionLevel::Ssl" and after connected to server try to Updrage the socket using UpgradeToSslAsync() but it fails.
    
    

    Anil.S

    • Marked as answer by AnilKumar.S Thursday, September 6, 2012 10:17 AM
    Thursday, September 6, 2012 10:16 AM

All replies

  • Hi,

     

    Wrote a sample app for sending mail, but it is not working fine with smtp.live.com & smtp.gmail.com[Working fine with "gmail-smtp-in.l.google.com" server]. These servers prompt that must issue a STARTTLS command first, so that i have put some changes in socket connection code, it causes crash after connecting these servers. Please help.

    task<void> connectTask(sock->ConnectAsync(hostName , serverPort, Windows::Networking::Sockets::SocketProtectionLevel::Ssl ));
    connectTask.wait();

    Thanks,

    Anil.S

    Friday, August 31, 2012 6:30 AM
  • See How to secure socket connections with TLS/SSL (Metro style apps using C#/VB/C++ and XAML)

    What is the exact error you are getting? In what context are you calling this? You cannot call task.wait on your UI thread, so if that is what you are doing the error is expected. Instead, use task continuations (i.e., task::then) as demonstrated in the linked article.

    You should also read Asynchronous programming in C++ and to help understand how to work with asynchronous calls.

    --Rob

    Friday, August 31, 2012 10:09 PM
    Owner
  • Thanks Rob, reply to you with more details. note that it is working fine with ""gmail-smtp-in.l.google.com" server.

    Sunday, September 2, 2012 5:51 PM
  • As Rob mentioned, task.wait will fail on the UI thread. Is the crash you mentioned due to this? What is the exception that caused the crash and if you have it, the callstack?

    David Lamb

    Wednesday, September 5, 2012 3:56 PM
    Moderator
  • Thanks, actually it was my mistake when pasting code here.[wait() not calling in UI thread].

    Now it is Working fine with "smtp.gmail.com" server [port 465] , When i changed the Smtp command sequence like this
    1. Connect the server [using SocketProtectionLevel::Ssl]
    2. EHLO 3. AUTH LOGIN, then send authentication details(username & password) in Base64 format.
    4. MAIL FROM: <>    5. RCPT TO: <> 6. DATA  7. (DOT). 8. QUIT.

    But "smtp.live.com" will not working with the above sequence and after connection there is no response from server.
    and also try with ports 25 & 587 without using  "SocketProtectionLevel::Ssl" and after connected to server try to Updrage the socket using UpgradeToSslAsync() but it fails.
    
    

    Anil.S

    • Marked as answer by AnilKumar.S Thursday, September 6, 2012 10:17 AM
    Thursday, September 6, 2012 10:16 AM