Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

Qt C++ e-mail

Ostatnio zmodyfikowano 2016-04-29 18:35
Autor Wiadomość
Kefirek
Temat założony przez niniejszego użytkownika
Qt C++ e-mail
» 2016-04-19 20:43:48
Qt program wysyłający e-maila z załącznikiem.
W sumie od dwóch dni szukam czegoś u Wójka Googl'a.
Są przykładowe programy, ale nie działają(pisane we wcześniejszych wersjach Qt).
Może ktoś mnie naprowadzi jak to wykonać???????.
Temat początkowo wydawał sie prozaiczny, ale niestety po dwóch dniach szukania stwierdziłem, że nie będzie to proste.
Pozdrawiam
P-147444
Masterpc16
» 2016-04-20 21:23:01
Oj coś słabo szukałeś
pierwszy wynik czwarty post
http://bfy.tw/5N71.
P-147472
Kefirek
Temat założony przez niniejszego użytkownika
» 2016-04-20 22:05:27
A mi się wydaje, że nie zrozumiałeś mojego posta do końca.
Pozdrawiam 
P-147473
Gibas11
» 2016-04-20 22:37:19
Właściwie to nie napisałeś za bardzo z czym masz problem, bo jeżeli nie z samym wysyłaniem i nie z UI (bo tu wystarczą podstawy) to z czym?
P-147475
Kefirek
Temat założony przez niniejszego użytkownika
» 2016-04-21 19:34:48
Może nie problem, ale nie mogę znaleźć przykładowego kodu w C++ który użyłbym w Qt i by to działało.
Poprostu w necie jest mase info chodzi o kod, ale nie ma gotowca, który by działał.
Poniżej przykłąd: program do wysyłania e-maila pisany w Qt kompiluje się ale o wysłaniu e-maila można pomarzyć.
C/C++
#-------------------------------------------------
#
# Project created by QtCreator 2013-07-09T15:55:12
#
#-------------------------------------------------

QT += core gui network

greaterThan( QT_MAJOR_VERSION, 4 )
    : QT += widgets TARGET = smtp TEMPLATE = app SOURCES += main.cpp\
 mainwindow.cpp cpp\
 smtp.cpp HEADERS += mainwindow.h h\
 smtp.h FORMS += mainwindow.ui // #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include "smtp.h" #include <QtWidgets/QMessageBox> namespace Ui
{
    class MainWindow;
}

class MainWindow
    : public QMainWindow
{
    Q_OBJECT
   
public:
    explicit MainWindow( QWidget * parent = 0 );
    ~MainWindow();
   
private slots:
    void sendMail();
    void mailSent( QString );
   
private:
    Ui::MainWindow * ui;
   
};

#endif // MAINWINDOW_H
/////////////////////////////////////////////
/*
Copyright (c) 2013 Raivis Strogonovs

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/

#ifndef SMTP_H
#define SMTP_H


#include <QtNetwork/QAbstractSocket>
#include <QtNetwork/QSslSocket>
#include <QString>
#include <QTextStream>
#include <QDebug>
#include <QtWidgets/QMessageBox>
#include <QByteArray>



class Smtp
    : public QObject
{
    Q_OBJECT
   
   
public:
    Smtp( const QString & user, const QString & pass,
    const QString & host, int port = 465, int timeout = 30000 );
    ~Smtp();
   
    void sendMail( const QString & from, const QString & to,
    const QString & subject, const QString & body );
   
    signals:
    void status( const QString & );
   
private slots:
    void stateChanged( QAbstractSocket::SocketState socketState );
    void errorReceived( QAbstractSocket::SocketError socketError );
    void disconnected();
    void connected();
    void readyRead();
   
private:
    int timeout;
    QString message;
    QTextStream * t;
    QSslSocket * socket;
    QString from;
    QString rcpt;
    QString response;
    QString user;
    QString pass;
    QString host;
    int port;
    enum states { Tls, HandShake, Auth, User, Pass, Rcpt, Mail, Data, Init, Body, Quit, Close };
    int state;
   
};
#endif
/////////////////////////////////////////////////////////////////////////////////
#include "mainwindow.h"
#include <QApplication>

int main( int argc, char * argv[] )
{
    QApplication a( argc, argv );
    MainWindow w;
    w.show();
   
    return a.exec();
}
/////////////////////////////////////////////////////////////////////////////////
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow( QWidget * parent )
    : QMainWindow( parent )
     , ui( new Ui::MainWindow )
{
    ui->setupUi( this );
    connect( ui->sendBtn, SIGNAL( clicked() ), this, SLOT( sendMail() ) );
    connect( ui->exitBtn, SIGNAL( clicked() ), this, SLOT( close() ) );
}

void MainWindow::sendMail()
{
    Smtp * smtp = new Smtp( ui->uname->text(), ui->paswd->text(), ui->server->text(), ui->port->text().toInt() );
    connect( smtp, SIGNAL( status( QString ) ), this, SLOT( mailSent( QString ) ) );
   
   
    smtp->sendMail( ui->uname->text(), ui->rcpt->text(), ui->subject->text(), ui->msg->toPlainText() );
}

void MainWindow::mailSent( QString status )
{
    if( status == "Message sent" )
         QMessageBox::warning( 0, tr( "Qt Simple SMTP client" ), tr( "Message sent!\n\n" ) );
   
}

MainWindow::~MainWindow()
{
    delete ui;
}
/////////////////////////////////////////////////////////////////////////
/*
Copyright (c) 2013 Raivis Strogonovs

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/



#include "smtp.h"

Smtp::Smtp( const QString & user, const QString & pass, const QString & host, int port, int timeout )
{
    socket = new QSslSocket( this );
   
    connect( socket, SIGNAL( readyRead() ), this, SLOT( readyRead() ) );
    connect( socket, SIGNAL( connected() ), this, SLOT( connected() ) );
    connect( socket, SIGNAL( error( QAbstractSocket::SocketError ) ), this, SLOT( errorReceived( QAbstractSocket::SocketError ) ) );
    connect( socket, SIGNAL( stateChanged( QAbstractSocket::SocketState ) ), this, SLOT( stateChanged( QAbstractSocket::SocketState ) ) );
    connect( socket, SIGNAL( disconnected() ), this, SLOT( disconnected() ) );
   
   
    this->user = user;
    this->pass = pass;
   
    this->host = host;
    this->port = port;
    this->timeout = timeout;
   
   
}

void Smtp::sendMail( const QString & from, const QString & to, const QString & subject, const QString & body )
{
    message = "To: " + to + "\n";
    message.append( "From: " + from + "\n" );
    message.append( "Subject: " + subject + "\n" );
    message.append( body );
    message.replace( QString::fromLatin1( "\n" ), QString::fromLatin1( "\r\n" ) );
    message.replace( QString::fromLatin1( "\r\n.\r\n" ),
    QString::fromLatin1( "\r\n..\r\n" ) );
    this->from = from;
    rcpt = to;
    state = Init;
    socket->connectToHostEncrypted( host, port ); //"smtp.gmail.com" and 465 for gmail TLS
    if( !socket->waitForConnected( timeout ) ) {
        qDebug() << socket->errorString();
    }
   
    t = new QTextStream( socket );
   
   
   
}

Smtp::~Smtp()
{
    delete t;
    delete socket;
}
void Smtp::stateChanged( QAbstractSocket::SocketState socketState )
{
   
    qDebug() << "stateChanged " << socketState;
}

void Smtp::errorReceived( QAbstractSocket::SocketError socketError )
{
    qDebug() << "error " << socketError;
}

void Smtp::disconnected()
{
   
    qDebug() << "disconneted";
    qDebug() << "error " << socket->errorString();
}

void Smtp::connected()
{
    qDebug() << "Connected ";
}

void Smtp::readyRead()
{
   
    qDebug() << "readyRead";
    // SMTP is line-oriented
   
    QString responseLine;
    do
    {
        responseLine = socket->readLine();
        response += responseLine;
    }
    while( socket->canReadLine() && responseLine[ 3 ] != ' ' );
   
    responseLine.truncate( 3 );
   
    qDebug() << "Server response code:" << responseLine;
    qDebug() << "Server response: " << response;
   
    if( state == Init && responseLine == "220" )
    {
        // banner was okay, let's go on
        * t << "EHLO localhost" << "\r\n";
        t->flush();
       
        state = HandShake;
    }
    //No need, because I'm using socket->startClienEncryption() which makes the SSL handshake for you
    else if( state == Tls && responseLine == "250" )
    {
        // Trying AUTH
        qDebug() << "STarting Tls";
        * t << "STARTTLS" << "\r\n";
        t->flush();
        state = HandShake;
    }
    else if( state == HandShake && responseLine == "250" )
    {
        socket->startClientEncryption();
        if( !socket->waitForEncrypted( timeout ) )
        {
            qDebug() << socket->errorString();
            state = Close;
        }
       
       
        //Send EHLO once again but now encrypted
       
        * t << "EHLO localhost" << "\r\n";
        t->flush();
        state = Auth;
    }
    else if( state == Auth && responseLine == "250" )
    {
        // Trying AUTH
        qDebug() << "Auth";
        * t << "AUTH LOGIN" << "\r\n";
        t->flush();
        state = User;
    }
    else if( state == User && responseLine == "334" )
    {
        //Trying User       
        qDebug() << "Username";
        //GMAIL is using XOAUTH2 protocol, which basically means that password and username has to be sent in base64 coding
        //https://developers.google.com/gmail/xoauth2_protocol
        * t << QByteArray().append( user ).toBase64() << "\r\n";
        t->flush();
       
        state = Pass;
    }
    else if( state == Pass && responseLine == "334" )
    {
        //Trying pass
        qDebug() << "Pass";
        * t << QByteArray().append( pass ).toBase64() << "\r\n";
        t->flush();
       
        state = Mail;
    }
    else if( state == Mail && responseLine == "235" )
    {
        // HELO response was okay (well, it has to be)
       
        //Apperantly for Google it is mandatory to have MAIL FROM and RCPT email formated the following way -> <email@gmail.com>
        qDebug() << "MAIL FROM:<" << from << ">";
        * t << "MAIL FROM:<" << from << ">\r\n";
        t->flush();
        state = Rcpt;
    }
    else if( state == Rcpt && responseLine == "250" )
    {
        //Apperantly for Google it is mandatory to have MAIL FROM and RCPT email formated the following way -> <email@gmail.com>
        * t << "RCPT TO:<" << rcpt << ">\r\n"; //r
        t->flush();
        state = Data;
    }
    else if( state == Data && responseLine == "250" )
    {
       
        * t << "DATA\r\n";
        t->flush();
        state = Body;
    }
    else if( state == Body && responseLine == "354" )
    {
       
        * t << message << "\r\n.\r\n";
        t->flush();
        state = Quit;
    }
    else if( state == Quit && responseLine == "250" )
    {
       
        * t << "QUIT\r\n";
        t->flush();
        // here, we just close.
        state = Close;
        emit status( tr( "Message sent" ) );
    }
    else if( state == Close )
    {
        deleteLater();
        return;
    }
    else
    {
        // something broke.
        QMessageBox::warning( 0, tr( "Qt Simple SMTP client" ), tr( "Unexpected reply from SMTP server:\n\n" ) + response );
        state = Close;
        emit status( tr( "Failed to send message" ) );
    }
    response = "";
}
//////////////////////////////////////////////////////////////////////////////
Chodzi mi o jak najmniejszy, najprostszy, ale przykład działającego kodu do analizy i to nie ważne czy na gmaila wysyłam czy na wp.
Pozdrawiam
P-147504
Masterpc16
» 2016-04-22 18:52:13
Czyli jakbym napisał Ci program w wersji konsolowej i przesłał wszystkie pliki to powinieneś bez problemu przerobić go na Qt? Podaj swojego e-maila coś spróbuję sklecić.
P-147544
Kefirek
Temat założony przez niniejszego użytkownika
» 2016-04-22 19:00:21
Z przeróbką nie ma problemu.
piernikowski.s@wp.pl

dzięki i pozdrawiam
P-147545
Kefirek
Temat założony przez niniejszego użytkownika
» 2016-04-24 10:06:46
Witam

Poniższy kod pochodzi z Qt plik .pro
C/C++
QT += core

QT -= gui

TARGET = demo1
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app


SOURCES += +=\
 demo1.cpp

# Location of SMTP Library
SMTP_LIBRARY_LOCATION = $ $ PWD /../../../ build / SMTPEmail - Desktop - Debug

win32: CONFIG( release, debug | release )
    : LIBS += - L $ $ SMTP_LIBRARY_LOCATION / release / - lSMTPEmail else
    : win32
    : CONFIG( debug, debug | release )
    : LIBS += - L $ $ SMTP_LIBRARY_LOCATION / debug / - lSMTPEmail else
    : unix
        : LIBS += - L $ $ SMTP_LIBRARY_LOCATION - lSMTPEmail INCLUDEPATH += $ $ SMTP_LIBRARY_LOCATION DEPENDPATH += $ $ SMTP_LIBRARY_LOCATION

W wyniku kompilacji otrzymuje błąd:
:-1: błąd: cannot find -lSMTPEmail
collect2.exe:-1: błąd: error: ld returned 1 exit status

Mam Qt pod Mingw32.
Program nie może znaleźć -lSMTPEmail

Może ktoś wie jak to zrobić?
P-147580
« 1 » 2 3
  Strona 1 z 3 Następna strona