通过Qt发送电子邮件

时间:2015-12-02 09:24:17

标签: c++ qt email smtp

我试图通过qt发送电子邮件我使用了这段代码,但我发现了这个错误" 4 ----" 502 5.5.1无法识别的命令。 q9sm1859778wjo.9 - gsmtp \ n"并感谢您的帮助。 smrp.h

import android.graphics.Point;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    LinearLayout buttonContainer, indicatorContainer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /* getting display size */
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = size.x;
        int height = size.y;

        /* set button size */
        int buttonWidthSize = (int) (width / 2.5);
        int buttonHeightSize = (int) (height / 5.0);

        buttonContainer = (LinearLayout)findViewById(R.id.buttonContainer);
        indicatorContainer = (LinearLayout)findViewById(R.id.indicatorContainer);



        for(int i = 1; i <= 10 ; i++){
            Button button = new Button(this);
            button.setText("button"+i);
            button.setWidth(buttonWidthSize);
            button.setHeight(buttonHeightSize);
            buttonContainer.addView(button, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        }

        // how it can be solved.
    }

smtp.cpp

#include <QtCore>
#include <QCoreApplication>
#include <QObject>

#include <QTcpSocket>
#include <QString>
#include <QTextStream>
#include <QDebug>
#include <QAbstractSocket>
#include <QDateTime>
#include <QDate>
#include <QLocale>
#include <QObject>
#include <QTcpSocket>

/*
  USAGE!
  Smtp *newMail  = new Smtp("remoteserver","username","pass");
  newMail->Send("from@address.com","to@address.com"," Your Subject","My body text");
  class emit ErrorCloseAll() / SuccessQuit()   or get QStringList ErrorMSG; to read error and data....

*/
class Smtp : public QObject
{
    Q_OBJECT

    public:
            Smtp(QString smtphost, QString smtpusername, QString smtppass);
            ~Smtp();
            void setConfig(QString smtphost, QString smtpusername, QString smtppass);
            void send( const QString &from, const QString &to,const QString &subject, const QString &body );
            int linesend;
            QStringList errorMSG;
            QStringList getErrorlist();
    signals:
            void status( const QString &);
            void connectorSuccess();
            void sendLine();
            void errorCloseAll();
            void successQuit();

    private slots:
            void disconnected();
            void connected();
            void readLiner();
            void putSendLine();
    private:
            bool isconnect;
            QString smtphost;
            QString smtpusername;
            QString smtppass;
            QString message;
            QString output;
            QString RemoteServerName;
            QString mailstatus;
            QTextStream *t;
            QTcpSocket *smtpsocket;
            QString from;
            QString rcpt;
            QString response;
            QString sendLineAndGrab(QString senddata);
            int Timeout;
            QString encodeBase64( QString xml );
            QString decodeBase64( QString xml );
            int dateswap(QString form, uint unixtime );
                QString TimeStampMail();


};
#endif

1 个答案:

答案 0 :(得分:0)

这与Qt无关,只是SMTP设置。

从您的代码中我看到您正在使用gmail和端口25.这是不正确的,将您的传出端口更改为465,请参阅here