Questo sito utilizza cookies, anche di terze parti, per mostrare pubblicità e servizi in linea con il tuo account. Leggi l'informativa sui cookies.
Username: Password: oppure
C/C++ - [QT4 - QT Creator]Inviare e-mail con autenticazione
Forum - C/C++ - [QT4 - QT Creator]Inviare e-mail con autenticazione

Avatar
Il_maca (Ex-Member)
Pro


Messaggi: 163
Iscritto: 28/01/2009

Segnala al moderatore
Postato alle 19:28
Venerdì, 01/04/2011
Salve a tutti, in primis vorrei dire che ho modificato il thread in quanto mi sono accorto di aver dimenticato alcune regole (mea culpa). Ora passo al problema: in sostanza sto creando una specie di gestionale, e mi servirebbe sapere se con la libreria qt4, o con eventualmente una terza libreria, posso inviare delle e-mail attraverso il protocollo smtp. L'email dovrebbe essere inviata con autenticazione.

Vorrei evitare (per il momento) di dovermi studiare i socket e implementare del codice utilizzando direttamente questi, perché è un progetto scolastico che non prevede nella valutazione questo tipo di classi. E' ovvio che comunque è ben accetta qualsiasi tipo di documentazione a riguardo (che mi studierò con un pò più calma in futuro)

EDIT: googlando ho trovato un post (di un forum francese) in cui forse ho trovato la soluzione ai miei problemi (link: http://forum.qtfr.org/viewtopic.php?id=7736 post #16). Riporto il codice:
mailsender.cpp

Codice sorgente - presumibilmente C++

  1. //mailsender.cpp
  2. #include "mailsender.h"
  3. #include <QString>
  4. #include <QSslSocket>
  5. #include <QTextStream>
  6. #include <QByteArray>
  7. #include <QDateTime>
  8. #include <QTextCodec>
  9. #include <QFile>
  10. #include <QFileInfo>
  11. #include <QHostInfo>
  12. #include <time.h>
  13. #include <QCryptographicHash>
  14.  
  15.  
  16. static int dateswap(QString form, uint unixtime )
  17. {
  18.     QDateTime fromunix;
  19.     fromunix.setTime_t(unixtime);
  20.     QString numeric = fromunix.toString((const QString)form);
  21.     bool ok;
  22.     return (int)numeric.toFloat(&ok);
  23. }
  24.  
  25. static QString encodeBase64( QString xml )
  26. {
  27.     QByteArray text;
  28.     text.append(xml);
  29.     return text.toBase64();
  30. }
  31.  
  32.  
  33. static QString TimeStampMail()
  34. {
  35.     /* mail rtf Date format! http://www.faqs.org/rfcs/rfc788.html */
  36.     uint unixtime = (uint)time( NULL );
  37.     QDateTime fromunix;
  38.     fromunix.setTime_t(unixtime);
  39.     QStringList RTFdays = QStringList() << "giorno_NULL" << "Mon" << "Tue" << "Wed" << "Thu" << "Fri" << "Sat" << "Sun";
  40.     QStringList RTFmonth = QStringList() << "mese_NULL" << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
  41.     QDate timeroad(dateswap("yyyy",unixtime),dateswap("M",unixtime),dateswap("d",unixtime));
  42.     QStringList rtfd_line;
  43.     rtfd_line.clear();
  44.     rtfd_line.append("Date: ");
  45.     rtfd_line.append(RTFdays.at(timeroad.dayOfWeek()));
  46.     rtfd_line.append(", ");
  47.     rtfd_line.append(QString::number(dateswap("d",unixtime)));
  48.     rtfd_line.append(" ");
  49.     rtfd_line.append(RTFmonth.at(dateswap("M",unixtime)));
  50.     rtfd_line.append(" ");
  51.     rtfd_line.append(QString::number(dateswap("yyyy",unixtime)));
  52.     rtfd_line.append(" ");
  53.     rtfd_line.append(fromunix.toString("hh:mm:ss"));
  54.     rtfd_line.append("");
  55.  
  56.     return QString(rtfd_line.join(""));
  57. }
  58.  
  59. static QString createBoundary()
  60. {
  61.     QByteArray hash = QCryptographicHash::hash(QString(QString::number(qrand())).toUtf8(),QCryptographicHash::Md5);
  62.     QString boundary = hash.toHex();
  63.     boundary.truncate(26);
  64.     boundary.prepend("----=_NextPart_");
  65.     return boundary;
  66. }
  67.  
  68.  
  69. MailSender::MailSender(const QString &smtpServer, const QString &from, const QStringList &to, const QString &subject, const QString &body)
  70. {
  71.     setSmtpServer(smtpServer);
  72.     setPort(25);
  73.     setTimeout(30000);
  74.     setFrom(from);
  75.     setTo(to);
  76.     setSubject(subject);
  77.     setBody(body);
  78.     setPriority (normal);
  79.     setContentType(text);
  80.     setEncoding(_8bit);
  81.     setISO(utf8);
  82.     setSsl(false);
  83. }
  84.  
  85. MailSender::~MailSender()
  86. {
  87.     if(_socket) {
  88.         delete _socket;
  89.     }
  90. }
  91.  
  92. void MailSender::setFrom(const QString &from)
  93. {
  94.     _from = from;
  95.     _fromName = from;
  96.     _replyTo = from;
  97. }
  98.  
  99. void MailSender::setISO(ISO iso)
  100. {
  101.     switch(iso) {
  102.       case iso88591: _iso = "iso-8859-1"; _bodyCodec = "ISO 8859-1"; break;
  103.       case utf8:     _iso = "utf-8"; _bodyCodec = "UTF-8"; break;
  104.     }
  105. }
  106.  
  107. void MailSender::setEncoding(Encoding encoding)
  108. {
  109.     switch(encoding) {
  110.       case _7bit:     _encoding = "7bit"; break;
  111.       case _8bit:     _encoding = "8bit"; break;
  112.       case base64:    _encoding = "base64"; break;
  113.     }
  114. }
  115.  
  116. QString MailSender::contentType()
  117. {
  118.     switch(_contentType) {
  119.       case html:            return "text/html";
  120.       case multipartmixed:  return "multipart/mixed";
  121.       case text:
  122.       default:              return "text/plain";
  123.     }
  124. }
  125.  
  126. QString MailSender::mailData()
  127. {
  128.     QString data;
  129.  
  130.     QString boundary1 = createBoundary();
  131.     QString boundary2 = createBoundary();
  132.  
  133.     QByteArray hash = QCryptographicHash::hash(QString(QString::number(qrand())).toUtf8(),QCryptographicHash::Md5);
  134.     QString id = hash.toHex();
  135.     data.append("Message-ID: " + id + "@" + QHostInfo::localHostName() + "\n");
  136.  
  137.     data.append("From: \"" + _fromName + "\"");
  138.     data.append(" <" + _from + ">\n");
  139.  
  140.     if ( _to.count() > 0 ) {
  141.         data.append("To: ");
  142.         for ( int i = 0; i < _to.count(); i++ ) {
  143.             data.append("<" + _to.at(i) + ">" + ",");
  144.         }
  145.         data.append("\n");
  146.     }
  147.  
  148.     if ( _cc.count() > 0 ) {
  149.         data.append("Cc: ");
  150.         for ( int i = 0; i < _cc.count(); i++ ) {
  151.             data.append(_cc.at(i) + ",");
  152.             if(i < _cc.count()-1) {
  153.                 data.append(",");
  154.             }
  155.         }
  156.         data.append("\n");
  157.     }
  158.  
  159.     data.append("Subject: " + _subject + "\n");
  160.     data.append(TimeStampMail() + "\n");
  161.  
  162.     data.append("MIME-Version: 1.0\n");
  163.     data.append("Content-Type: Multipart/Mixed; boundary=\"" + boundary1 + "\"\n");
  164.  
  165.     switch(_priority) {
  166.       case low:
  167.         data.append("X-Priority: 5\n");
  168.         data.append("Priority: Non-Urgent\n");
  169.         data.append("X-MSMail-Priority: Low\n");
  170.         data.append("Importance: low\n");
  171.         break;
  172.       case high:
  173.         data.append("X-Priority: 1\n");
  174.         data.append("Priority: Urgent\n");
  175.         data.append("X-MSMail-Priority: High\n");
  176.         data.append("Importance: high\n");
  177.         break;
  178.       default:
  179.         data.append("X-Priority: 3\n");
  180.         data.append("    X-MSMail-Priority: Normal\n");
  181.     }
  182.  
  183.     data.append("X-Mailer: QT4\r\n");  
  184.  
  185.     if ( ! _confirmTo.isEmpty() ) {
  186.         data.append("Disposition-Notification-To: " + _confirmTo + "\n");
  187.     }
  188.  
  189.     if ( ! _replyTo.isEmpty() && _confirmTo != _from ) {
  190.         data.append("Reply-to: " + _replyTo + "\n");
  191.         data.append("Return-Path: <" + _replyTo + ">\n");
  192.     }
  193.  
  194.     data.append("\n");
  195.  
  196.     data.append("This is a multi-part message in MIME format.\r\n\n");
  197.     data.append("--" + boundary1 + "\n");
  198.     data.append("Content-Type: multipart/alternative; boundary=\"" + boundary2 + "\"\n\n\n");
  199.     data.append("--" + boundary2 + "\n");
  200.  
  201.     data.append("Content-Type: " + contentType() + ";\n");
  202.     data.append("  charset=" + _iso + "\r\n");
  203.     data.append("Content-Transfer-Encoding: " + _encoding + "\r\n");
  204.     data.append("\r\n\n");
  205.  
  206.     if ( _contentType == html ) {
  207.         data.append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\r\n");
  208.         data.append("<HTML><HEAD>\r\n");
  209.         data.append("<META HTTP-EQUIV=\"CONTENT-TYPE\" CONTENT=\"text/html; charset=" + _iso + "\">\r\n");
  210.         data.append("<META content=\"MSHTML 6.00.2900.2802\" name=GENERATOR>\r\n");
  211.         data.append("<STYLE></STYLE>\r\n");
  212.         data.append("</head>\r\n");
  213.         data.append("<body bgColor=#ffffff>\r\n");
  214.     }
  215.  
  216.     QByteArray encodedBody(_body.toLatin1()); // = array;
  217.     QTextCodec *codec = QTextCodec::codecForName(_bodyCodec.toLatin1());
  218.     data.append(codec->toUnicode(encodedBody) + "\r\n");
  219.  
  220.     if ( _contentType == html ) {
  221.         data.append("<DIV> </DIV></body></html>\r\n\n");
  222.         data.append("--" + boundary2 + "--\r\n\n");
  223.     }
  224.  
  225.     if ( _attachments.count() > 0 ) {
  226.         for ( int i = 0; i < _attachments.count(); i++) {
  227.             data.append("--" + boundary1 + "\n");
  228.             QFileInfo fileinfo(_attachments.value(i));
  229.             QString type = getMimeType(fileinfo.suffix());
  230.             data.append("Content-Type: " + type + ";\n");
  231.             data.append("  name=" + fileinfo.fileName() + "\n");
  232.  
  233.             QString tt = fileinfo.fileName();
  234.  
  235.             data.append("Content-Transfer-Encoding: base64\n");
  236.             data.append("Content-Disposition: attachment\n");
  237.             data.append("  filename=" + fileinfo.fileName() + "\n\n");
  238.             QString encodedFile;
  239.             QString f =_attachments.value(i);
  240.             QFile file(_attachments.value(i));
  241.             file.open(QIODevice::ReadOnly);
  242.             QDataStream in(&file);    
  243.             quint8 a;
  244.             char c;
  245.             QString b;
  246.             while ( ! in.atEnd() ) {
  247.                 in >> a;
  248.                 c = a;    
  249.                 b.append(c);
  250.             }
  251.             encodedFile = encodeBase64(b);
  252.             data.append(encodedFile);
  253.             data.append("\r\n\n");
  254.         }
  255.         data.append("--" + boundary1 + "--\r\n\n");
  256.     }
  257.  
  258.     _lastMailData = data;
  259.     return data;
  260. }
  261.  
  262. QString MailSender::getMimeType(QString ext)
  263. {
  264. //texte
  265.     if (ext == "txt")            return "text/plain";
  266.     if (ext == "htm" || ext == "html")    return "text/html";
  267.     if (ext == "css")            return "text/css";
  268. //Images
  269.     if (ext == "png")            return "image/png";
  270.     if (ext == "gif")            return "image/gif";
  271.     if (ext == "jpg" || ext == "jpeg")    return "image/jpeg";
  272.     if (ext == "bmp")            return "image/bmp";
  273.     if (ext == "tif")            return "image/tiff";
  274. //Archives
  275.     if (ext == "bz2")            return "application/x-bzip";
  276.     if (ext == "gz")            return "application/x-gzip";
  277.     if (ext == "tar" )            return "application/x-tar";
  278.     if (ext == "zip" )            return "application/zip";
  279. //Audio
  280.     if ( ext == "aif" || ext == "aiff")    return "audio/aiff";
  281.     if ( ext == "mid" || ext == "midi")    return "audio/mid";
  282.     if ( ext == "mp3")            return "audio/mpeg";
  283.     if ( ext == "ogg")            return "audio/ogg";
  284.     if ( ext == "wav")            return "audio/wav";
  285.     if ( ext == "wma")            return "audio/x-ms-wma";
  286. //Video
  287.     if ( ext == "asf" || ext == "asx")    return "video/x-ms-asf";
  288.     if ( ext == "avi")            return "video/avi";
  289.     if ( ext == "mpg" || ext == "mpeg")    return "video/mpeg";
  290.     if ( ext == "wmv")            return "video/x-ms-wmv";
  291.     if ( ext == "wmx")            return "video/x-ms-wmx";
  292. //XML
  293.     if ( ext == "xml")            return "text/xml";
  294.     if ( ext == "xsl")            return "text/xsl";
  295. //Microsoft
  296.     if ( ext == "doc" || ext == "rtf")    return "application/msword";
  297.     if ( ext == "xls")            return "application/excel";
  298.     if ( ext == "ppt" || ext == "pps")    return "application/vnd.ms-powerpoint";
  299. //Adobe
  300.     if ( ext == "pdf")            return "application/pdf";
  301.     if ( ext == "ai" || ext == "eps")    return "application/postscript";
  302.     if ( ext == "psd")            return "image/psd";
  303. //Macromedia
  304.     if ( ext == "swf")            return "application/x-shockwave-flash";
  305. //Real
  306.     if ( ext == "ra")            return "audio/vnd.rn-realaudio";
  307.     if ( ext == "ram")            return "audio/x-pn-realaudio";
  308.     if ( ext == "rm")            return "application/vnd.rn-realmedia";
  309.     if ( ext == "rv")            return "video/vnd.rn-realvideo";
  310. //Other
  311.     if ( ext == "exe")            return "application/x-msdownload";
  312.     if ( ext == "pls")            return "audio/scpls";
  313.     if ( ext == "m3u")            return "audio/x-mpegurl";
  314.  
  315.     return "text/plain"; // default
  316. }
  317.  
  318. void MailSender::errorReceived(QAbstractSocket::SocketError socketError)
  319. {
  320.     QString msg;
  321.  
  322.     switch(socketError) {
  323.         case QAbstractSocket::ConnectionRefusedError: msg = "ConnectionRefusedError"; break;
  324.         case QAbstractSocket::RemoteHostClosedError: msg = "RemoteHostClosedError"; break;
  325.         case QAbstractSocket::HostNotFoundError: msg = "HostNotFoundError"; break;
  326.         case QAbstractSocket::SocketAccessError: msg = "SocketAccessError"; break;
  327.         case QAbstractSocket::SocketResourceError: msg = "SocketResourceError"; break;
  328.         case QAbstractSocket::SocketTimeoutError: msg = "SocketTimeoutError"; break;
  329.         case QAbstractSocket::DatagramTooLargeError: msg = "DatagramTooLargeError"; break;
  330.         case QAbstractSocket::NetworkError: msg = "NetworkError"; break;
  331.         case QAbstractSocket::AddressInUseError: msg = "AddressInUseError"; break;
  332.         case QAbstractSocket::SocketAddressNotAvailableError: msg = "SocketAddressNotAvailableError"; break;
  333.         case QAbstractSocket::UnsupportedSocketOperationError: msg = "UnsupportedSocketOperationError"; break;
  334.         case QAbstractSocket::ProxyAuthenticationRequiredError: msg = "ProxyAuthenticationRequiredError"; break;
  335.         default: msg = "Unknown Error";
  336.     }
  337.  
  338.     error("Socket error [" + msg + "]");
  339. }
  340.  
  341.  
  342. bool MailSender::send()
  343. {
  344.     _lastError = "";
  345.  
  346.     if(_socket) {
  347.         delete _socket;
  348.     }
  349.  
  350.     //_socket = _ssl ? new QSslSocket(this) : new QTcpSocket(this);
  351.  _socket=new QTcpSocket(this);
  352.     connect( _socket, SIGNAL( error( QAbstractSocket::SocketError) ), this, SLOT( errorReceived( QAbstractSocket::SocketError ) ) );
  353.     connect( _socket, SIGNAL( proxyAuthenticationRequired(const QNetworkProxy & , QAuthenticator *) ), this, SLOT(proxyAuthentication(const QNetworkProxy &, QAuthenticator * ) ) );
  354.  
  355.     bool auth = ! _login.isEmpty();
  356.  
  357.     _socket->connectToHost( _smtpServer, _port );
  358.  
  359.     if( !_socket->waitForConnected( _timeout ) ) {
  360.         error("Time out connecting host");
  361.         return false;
  362.     }
  363.  
  364.     if(!read("220")) {
  365.         return false;
  366.     }
  367.  
  368.     if ( !sendCommand("EHLO there", "250") ) {
  369.         if ( !sendCommand("HELO there", "250") ) {
  370.             return false;
  371.         }
  372.     }
  373.  
  374.    /* if(_ssl) {
  375.         if ( !sendCommand("STARTTLS", "220") ) {
  376.             return false;
  377.         }
  378.         QSslSocket *pssl = qobject_cast<QSslSocket *>(_socket);
  379.         if(pssl == 0) {
  380.             error("internal error casting to QSslSocket");
  381.             return false;
  382.         }
  383.         pssl->startClientEncryption ();
  384.     }*/
  385.  
  386.  
  387.     if ( auth ) {
  388.         if( !sendCommand("AUTH LOGIN", "334") ) {
  389.             return false;
  390.         }
  391.         if( !sendCommand(encodeBase64(_login), "334") ) {
  392.             return false;
  393.         }
  394.         if( !sendCommand(encodeBase64(_password), "235") ) {
  395.             return false;
  396.         }
  397.     }
  398.  
  399.     if( !sendCommand(QString::fromLatin1("MAIL FROM:<") +_from + QString::fromLatin1(">"), "250") ) {
  400.         return false;
  401.     }
  402.  
  403.     QStringList recipients = _to + _cc + _bcc;
  404.     for (int i=0; i< recipients.count(); i++) {
  405.         if( !sendCommand(QString::fromLatin1("RCPT TO:<") + recipients.at(i) + QString::fromLatin1(">"), "250") ) {
  406.             return false;
  407.         }
  408.     }
  409.  
  410.     if( !sendCommand(QString::fromLatin1("DATA"), "354") ) {
  411.         return false;
  412.     }
  413.     if( !sendCommand(mailData() + QString::fromLatin1("\r\n."), "250") ) {
  414.         return false;
  415.     }
  416.     if( !sendCommand(QString::fromLatin1("QUIT"), "221") ) {
  417.         return false;
  418.     }
  419.  
  420.     _socket->disconnectFromHost();
  421.     return true;
  422. }
  423.  
  424. bool MailSender::read(const QString &waitfor)
  425. {
  426.     if( ! _socket->waitForReadyRead( _timeout ) ) {
  427.         error("Read timeout");
  428.         return false;
  429.     }
  430.  
  431.     if( !_socket->canReadLine() ) {
  432.         error("Can't read");
  433.         return false;
  434.     }
  435.  
  436.     QString responseLine;
  437.  
  438.     do {
  439.         responseLine = _socket->readLine();
  440.     } while( _socket->canReadLine() && responseLine[3] != ' ' );
  441.  
  442.     _lastResponse = responseLine;
  443.  
  444.     QString prefix = responseLine.left(3);
  445.     bool isOk = (prefix == waitfor);
  446.     if(!isOk) {
  447.         error("waiting for " + waitfor + ", received " + prefix);
  448.     }
  449.  
  450.     return isOk;
  451. }
  452.  
  453.  
  454. bool MailSender::sendCommand(const QString &cmd, const QString &waitfor)
  455. {
  456.     QTextStream t(_socket);
  457.     t << cmd + "\r\n";
  458.     t.flush();
  459.  
  460.     _lastCmd = cmd;
  461.  
  462.     return read(waitfor);
  463. }
  464.  
  465. void MailSender::error(const QString &msg)
  466. {
  467.     _lastError = msg;
  468. }
  469.  
  470.  
  471. void MailSender::proxyAuthentication(const QNetworkProxy &, QAuthenticator * authenticator)
  472. {
  473.     *authenticator = _authenticator;
  474. }
  475.  
  476. void MailSender::setProxyAuthenticator(const QAuthenticator &authenticator)
  477. {
  478.     _authenticator = authenticator;
  479. }



mailsender.h
Codice sorgente - presumibilmente C++

  1. #include <QObject>
  2. #include <QTcpSocket>
  3. #include <QPointer>
  4. #include <QAuthenticator>
  5.  
  6. class MailSender : public QObject
  7. {
  8.     Q_OBJECT
  9.  
  10. public:
  11.  
  12. enum Priority {high, normal, low};
  13. enum ContentType {text, html, multipartmixed};
  14. enum Encoding {_7bit, _8bit, base64};
  15. enum ISO {utf8, iso88591};
  16.  
  17.     MailSender(const QString &smtpServer, const QString &from, const QStringList &to, const QString &subject, const QString &body);
  18.     ~MailSender();
  19.     bool send();
  20.     QString lastError() {return _lastError;}
  21.     QString lastCmd() {return _lastCmd;}
  22.     QString lastResponse() {return _lastResponse;}
  23.     QString lastMailData() {return _lastMailData;}
  24.  
  25.     void setSmtpServer (const QString &smtpServer)     {_smtpServer = smtpServer;}
  26.     void setPort (int port)                            {_port = port;}
  27.     void setTimeout (int timeout)                    {_timeout = timeout;}
  28.     void setLogin (const QString &login, const QString &passwd)        {_login = login; _password = passwd;}
  29.     void setSsl(bool ssl)                           {_ssl = ssl;}
  30.     void setCc (const QStringList &cc)                 {_cc = cc;}
  31.     void setBcc (const QStringList &bcc)             {_bcc = bcc;}
  32.     void setAttachments (const QStringList &attachments)    {_attachments = attachments;}
  33.     void setReplyTo (const QString &replyTo)         {_replyTo = replyTo;}
  34.     void setPriority (Priority priority)             {_priority = priority;}
  35.     void setFrom (const QString &from);
  36.     void setTo (const QStringList &to)                 {_to = to;}
  37.     void setSubject (const QString &subject)         {_subject = subject;}
  38.     void setBody (const QString &body)                 {_body = body;}
  39.     void setFromName (const QString &fromName)      {_fromName = fromName;}
  40.     void setContentType(ContentType contentType)    {_contentType = contentType;}
  41.     void setISO(ISO iso);
  42.     void setEncoding(Encoding encoding);
  43.     void setProxyAuthenticator(const QAuthenticator &authenticator);
  44.  
  45. private slots:
  46.     void errorReceived(QAbstractSocket::SocketError socketError);
  47.     void proxyAuthentication(const QNetworkProxy & proxy, QAuthenticator * authenticator);
  48.  
  49. private:
  50.  
  51.     QString getMimeType(QString ext);
  52.     QString mailData();
  53.     QString contentType();
  54.     bool read(const QString &waitfor);
  55.     bool sendCommand(const QString &cmd, const QString &waitfor);
  56.     void error(const QString &msg);
  57.  
  58.     QString        _smtpServer;
  59.     int         _port;
  60.     int         _timeout;
  61.     QString     _login;
  62.     QString     _password;
  63.     QPointer<QTcpSocket>  _socket;
  64.     bool        _ssl;
  65.     QAuthenticator _authenticator;
  66.     QString     _lastError;
  67.     QString     _lastCmd;
  68.     QString     _lastResponse;
  69.     QString     _lastMailData;
  70.  
  71.     QString        _from;
  72.     QStringList    _to;
  73.     QString        _subject;
  74.     QString        _body;
  75.     QStringList _cc;
  76.     QStringList _bcc;
  77.     QStringList _attachments;
  78.     QString        _fromName;
  79.     QString        _replyTo;
  80.     Priority    _priority;
  81.     ContentType _contentType;
  82.     QString        _encoding;
  83.     QString        _iso;
  84.     QString        _bodyCodec;
  85.     QString        _confirmTo;
  86. };
  87.  
  88. #endif



Uso della classe
Codice sorgente - presumibilmente C/C++

  1. MailSender test1(serveur_mail, from,liste_diffusion , "Titre de mon message", message);
  2.                 test1.setPriority(MailSender::high);
  3.                 send(test1)



Ho importato nel progetto i due file, e ho richiamato la funzione in questo modo:
Codice sorgente - presumibilmente C/C++

  1. QString server="smtp.gmail.com", from="prova1@gmail.com", oggetto="ciaooo", messaggio="ciaoo";
  2.     QStringList to;
  3.     to<< "maryano-campa@hotmail.it";
  4.     MailSender test1(server, from, to, oggetto, messaggio);
  5.     test1.send();



Ma ricevo i seguenti errori:
Codice sorgente - presumibilmente Delphi

  1. Running build steps for project M_Invoice...
  2. Configuration unchanged, skipping qmake step.
  3. Starting: "C:\Programmi\QTSDK\mingw\bin\mingw32-make.exe" -w
  4. mingw32-make: Entering directory `C:/Documents and Settings/Administrator/Desktop/Prove c++/M_Invoice-build-desktop'
  5. C:/Programmi/QTSDK/mingw/bin/mingw32-make -f Makefile.Release
  6. mingw32-make[1]: Entering directory `C:/Documents and Settings/Administrator/Desktop/Prove c++/M_Invoice-build-desktop'
  7. g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -Wl,-s -mthreads -Wl -Wl,-subsystem,windows -o release\M_Invoice.exe release/main.o release/mainwindow.o release/login.o release/registrazione.o release/moc_mainwindow.o release/moc_login.o release/moc_registrazione.o release/moc_mailsender.o  -L"c:\Programmi\QTSDK\Desktop\Qt\4.7.2\mingw\lib" -lmingw32 -lqtmain release\Icon_res.o C:\Programmi\QTSDK\mingw\lib\libws2_32.a -lQtGui4 -lQtNetwork4 -lQtCore4 -LC:\OpenSSL-Win32_full\lib
  8. mingw32-make[1]: Leaving directory `C:/Documents and Settings/Administrator/Desktop/Prove c++/M_Invoice-build-desktop'
  9. mingw32-make: Leaving directory `C:/Documents and Settings/Administrator/Desktop/Prove c++/M_Invoice-build-desktop'
  10. release/registrazione.o:registrazione.cpp:(.text+0x23a): undefined reference to `MailSender::MailSender(QString&, QString&, QStringList&, QString&, QString&)'
  11. release/registrazione.o:registrazione.cpp:(.text+0x248): undefined reference to `MailSender::send()'
  12. release/registrazione.o:registrazione.cpp:(.text+0x256): undefined reference to `MailSender::~MailSender()'
  13. release/registrazione.o:registrazione.cpp:(.text+0x396): undefined reference to `MailSender::~MailSender()'
  14. release/registrazione.o:registrazione.cpp:(.text+0x9a6): undefined reference to `MailSender::MailSender(QString&, QString&, QStringList&, QString&, QString&)'
  15. release/registrazione.o:registrazione.cpp:(.text+0x9b4): undefined reference to `MailSender::send()'
  16. release/registrazione.o:registrazione.cpp:(.text+0x9c2): undefined reference to `MailSender::~MailSender()'
  17. release/registrazione.o:registrazione.cpp:(.text+0xb02): undefined reference to `MailSender::~MailSender()'
  18. release/moc_mailsender.o:moc_mailsender.cpp:(.text+0x72): undefined reference to `MailSender::errorReceived(QAbstractSocket::SocketError)'
  19. release/moc_mailsender.o:moc_mailsender.cpp:(.text+0xa0): undefined reference to `MailSender::proxyAuthentication(QNetworkProxy const&, QAuthenticator*)'
  20. release/moc_mailsender.o:moc_mailsender.cpp:(.rdata$_ZTV10MailSender[vtable for MailSender]+0x14): undefined reference to `MailSender::~MailSender()'
  21. release/moc_mailsender.o:moc_mailsender.cpp:(.rdata$_ZTV10MailSender[vtable for MailSender]+0x18): undefined reference to `MailSender::~MailSender()'
  22. collect2: ld returned 1 exit status
  23. mingw32-make[1]: *** [release\M_Invoice.exe] Error 1
  24. mingw32-make: *** [release] Error 2
  25. The process "C:\Programmi\QTSDK\mingw\bin\mingw32-make.exe" exited with code 2.
  26. Error while building project M_Invoice (target: Desktop)
  27. When executing build step 'Make'



Dov'è che sbaglio??:asd: Help me

Ultima modifica effettuata da Il_maca il 21/04/2011 alle 8:41


Il computer non è una macchina inteligente nelle mani di gente stupida; ma è una macchina stupida nelle mani di gente intelligente (o almeno si spera).
PM