is there anyway to Send Email to someone in Qt?


  • Moderators

    @opengpu said in is there anyway to Send Email to someone in Qt?:

    I want to send email at my Qt app

    same difference, the previously mentioned clients should also be able to request from the sever, if new e-mails are available and forward it to your code.


  • Qt Champions 2018

    POP3 is to receive emails and is obsolete.
    IMAP is to receive emails.
    SMTP is to send emails.

    Most servers both private corporate or services (like gmail) provide SMTP.
    If that protocol is unavailable the most common reason is that your company is using a very restrictive instance of Microsoft Exchange. In that case you can use EWS with C++/CLI or manually by sending SOAP messages.

    The main takeaway here is: find out if the email server allows for SMTP and if it does use that



  • @opengpu
    You seem to be making things pretty complicated for yourself, possibly because you have not read up on SMTP/POP3/IMAP.

    Your question is about sending emails, from your Qt app. For that all you need is SMTP client code in your app, and knowledge of which SMTP server you wish it to connect to send the email, e.g smtp.gmail.com if you have a Gmail account.

    POP3/IMAP are for retrieving emails from a server. You have never asked for that. Unless you now need that facility too, forget about them. You need SMTP client only.



  • thanks for all. i google that protocol, and now i got it.



  • @opengpu
    BTW, I don't know if this will help you, but....

    In my case the users of my software specify & use there own SMTP server to connect to for sending email. Here is what my interafce looks like for them to specify the necessaries:

    0_1556184717892_smtpclient.png

    I don't know whether in your case you want the user to supply this information for their own SMTP email server, or whether you are supplying the server and hard-coding the values. Either way it shows the sort of parameters you will need to have in order to connect to the SMTP server.

    I program in Python/PyQt, not C++. So the SMTP client code I use is provided by a Python library, which won't be of any use to you. But either of the simple SMTP clients you are proposing to use will have a similar interface/parameters.



  • @JonB thank you so much!
    actually i want to use email instead of instantMsg. i send from PC client , and the official email-client of some company like google will recieve that email and alert the user.



  • and at common situation , email is fast enough when recieved after send?


  • Lifetime Qt Champion

    @opengpu
    Well in most cases, email comes very fast.
    But if your app is very, very, very far away, it might take a few minutes to get to
    destination.



  • @mrjj Are you talking about sending emails to Alpha Centauri? :)


  • Lifetime Qt Champion

    @JonB said

    Alpha Centauri

    Haha. that might take some time... ;)
    Ok maybe i went too far with "very" :)



  • @raven-worx
    after cmake and compile error in vs:
    simple-mail-master\build\src\simplemail-qt5_autogen\include_Debug\EWIEGA46WW/moc_sender.cpp(197): error C2491: 'SimpleMail::Sender::staticMetaObject': definition of dllimport static data member not allowed

    vs2017, x64,
    Qt\5.12.3\msvc2017_64\lib\cmake\Qt5\Qt5Config.cmake


  • Moderators

    @opengpu
    i haven't written nor used those 3rd party libs, i just pointed them out.



  • thank you



  • anyone ever used libcurl to send email to smtp server?how many milliseconds it need for sending mail one time?is it fast enough to send email by libcurl in mainThread?

    i tried this one, it works. but it's slow, as it needs about 3000 milliseconds for sending email one time.
    https://github.com/bluetiger9/SmtpClient-for-Qt


  • Moderators

    @opengpu said in is there anyway to Send Email to someone in Qt?:

    as it needs about 3000 milliseconds for sending email one time.

    you mean 3000 ms till you've sent the email contents to the server?
    or 3000 ms till you received it in the receivers mailbox?



  • @raven-worx just the sent code complish



  •     // First we need to create an SmtpClient object
        // We will use the Gmail's smtp server (smtp.gmail.com, port 465, ssl)
    
        SmtpClient smtp("smtp.gmail.com", 465, SmtpClient::SslConnection);
    
        // We need to set the username (your email address) and password
        // for smtp authentification.
    
        smtp.setUser("your_email_address@host.com");
        smtp.setPassword("your_password");
    
        // Now we create a MimeMessage object. This is the email.
    
        MimeMessage message;
    
        EmailAddress sender("your_email_address@host.com", "Your Name");
        message.setSender(&sender);
    
        EmailAddress to("recipient@host.com", "Recipient's Name");
        message.addRecipient(&to);
    
        message.setSubject("SmtpClient for Qt - Demo");
    
        // Now add some text to the email.
        // First we create a MimeText object.
    
        MimeText text;
    
        text.setText("Hi,\nThis is a simple email message.\n");
    
        // Now add it to the mail
    
        message.addPart(&text);
    
        // Now we can send the mail
    
        if (!smtp.connectToHost()) {
            qDebug() << "Failed to connect to host!" << endl;
            return -1;
        }
    
        if (!smtp.login()) {
            qDebug() << "Failed to login!" << endl;
            return -2;
        }
    
        if (!smtp.sendMail(message)) {
            qDebug() << "Failed to send mail!" << endl;
            return -3;
        }
    
        smtp.quit();
    


  • the time before the 1st line between after the last line.


  • Moderators

    @opengpu
    does it help to keep the SmtpClient instance? instead of creating a new one everytime you send an email



  • @raven-worx said in is there anyway to Send Email to someone in Qt?:

    does

    seems not. i put all of this in another thread. so better. but the time of this func is still slow. the time is mainly cost on connectToHost, login, sendMail. and u can easily test it.


  • Moderators

    @opengpu said in is there anyway to Send Email to someone in Qt?:

    seems not. i put all of this in another thread. so better. but the time of this func is still slow. the time is mainly cost on connectToHost, login, sendMail. and u can easily test it.

    well it doesn't matter if you move it to another thread if you still create a new instance everytime you send an email.

    Also where does the SmtpClient class come from?


Log in to reply