Qt 实现简单邮件发送

[cpp]  view plain  copy
  1. #ifndef SMTP_H  
  2. #define SMTP_H  
  3.   
  4.   
  5. #include <QtCore>  
  6. #include <QCoreApplication>  
  7. #include <QObject>  
  8.   
  9. #include <QTcpSocket>  
  10. #include <QString>  
  11. #include <QTextStream>  
  12. #include <QDebug>  
  13. #include <QAbstractSocket>  
  14. #include <QDateTime>  
  15. #include <QDate>  
  16. #include <QLocale>  
  17. #include <QObject>  
  18. #include <QTcpSocket>  
  19.    
  20. class Smtp : public QObject  
  21. {  
  22.     Q_OBJECT  
  23.       
  24.     public:  
  25.         Smtp(QString smtphost, QString smtpusername, QString smtppass);  
  26.         ~Smtp();  
  27.         bool Send( const QString &to,const QString &subject, const QString &body );  
  28.         int linesend;  
  29.         QStringList ErrorMSG;  
  30.         bool ReadLiner();  
  31.         void ErrorCloseAll();  
  32.   
  33.     signals:  
  34.         void status( const QString &);  
  35.         void SendLine();  
  36.     public slots:  
  37.         bool PutSendLine();  
  38.     private:  
  39.         bool isconnect;  
  40.         QString smtphost;  
  41.         QString smtpusername;  
  42.         QString smtppass;  
  43.         QString message;  
  44.         QString output;  
  45.         QString RemoteServerName;  
  46.         QString mailstatus;  
  47.         QTextStream *t;  
  48.         QTcpSocket *smtpsocket;  
  49.         QString from;  
  50.         QString rcpt;  
  51.         QString response;  
  52.         QString SendLineAndGrab(QString senddata);  
  53.         int Timeout;  
  54.         QString encodeBase64( QString xml );  
  55.         QString decodeBase64( QString xml );  
  56.         int dateswap(QString form, uint unixtime );  
  57.         QString TimeStampMail();  
  58.       
  59.    
  60. };  
  61.   
  62.   
  63. #endif  
  64.   
  65.    
[cpp]  view plain  copy
  1. #include "smtp.h"  
  2.   
  3. Smtp::Smtp(QString smtphost, QString smtpusername, QString smtppass)  
  4. {  
  5.     this->smtphost = smtphost;  
  6.     this->smtpusername = smtpusername;  
  7.     this->smtppass = smtppass;  
  8. }  
  9.   
  10. bool Smtp::Send(  const QString &to, const QString &subject, const QString &body )  
  11. {  
  12.     qDebug()<<"####"<<Q_FUNC_INFO;  
  13.     bool res = false;  
  14.     int waittime = 5 * 1000;  
  15.     this->from = smtpusername;  
  16.     rcpt = to;  
  17.     ErrorMSG.clear();  
  18.     Timeout = waittime;  
  19.     linesend = 0;  
  20.     isconnect = false;  
  21.       
  22.   
  23.     QString tmp = "=?utf-8?B?"+ QByteArray().append(subject).toBase64()+"?=";  
  24.     message.append("Subject:" + tmp + "\n");  
  25.     message.append("To: " + to + "\n");  
  26.     message.append("From: "+smtpusername+" <" + smtpusername + ">\n");  
  27.   
  28.     message.append("Content-Type: text/html; charset=UTF8;\n");   /* or txt */  
  29.     message.append("Content-transfer-encoding: 7BIT\n\n\n\n");  
  30.     message.append(body);  
  31.     message.replace( tr( "\n" ), tr( "\r\n" ) );  
  32.     message.replace( tr( "\r\n.\r\n" ),tr( "\r\n..\r\n" ) );  
  33.       
  34.     smtpsocket = new QTcpSocket(this);  
  35.     connect( this, SIGNAL(SendLine()), this ,SLOT(PutSendLine()));  
  36.     if (smtphost.size() > 0)  
  37.     {  
  38.         smtpsocket->connectToHost(smtphost,25);  
  39.     } else {  
  40.         smtpsocket->connectToHost("localhost",25);  
  41.     }  
  42.     if (smtpsocket->waitForConnected(Timeout))  
  43.     {  
  44.         if (smtpsocket->waitForReadyRead(Timeout))  
  45.         {  
  46.             isconnect = true;  
  47.            return  ReadLiner();  
  48.         }  
  49.     }  
  50.     else  
  51.     {  
  52.          ErrorCloseAll();  
  53.     }  
  54.    return res;  
  55. }  
  56.   
  57. bool  Smtp::ReadLiner()  
  58. {  
  59.     bool res = false;  
  60.     if (isconnect)  
  61.     {  
  62.         QTextCodec *codecx;  
  63.         codecx = QTextCodec::codecForMib(106);  
  64.         t = new QTextStream( smtpsocket );  
  65.         t->setCodec(codecx);  
  66.           
  67.         int loops = 0;  
  68.         while (!t->atEnd())  
  69.         {  
  70.             loops++;  
  71.             response = t->readLine();  
  72.         }  
  73.         if (response.size() > 0)  
  74.         {  
  75.             RemoteServerName = response;  
  76.             mailstatus = response.left(3);  
  77.             if (mailstatus == "220")  
  78.             {  
  79.                 response="";  
  80.                 linesend = 1;  
  81.                 res = true;  
  82.             }  
  83.         }  
  84.         else  
  85.         {  
  86.             ErrorCloseAll();  
  87.         }  
  88.     }  
  89.     return res;  
  90. }  
  91.   
  92. Smtp::~Smtp()  
  93.   
  94. {  
  95.     delete smtpsocket;  
  96.     delete t;  
  97. }  
  98.   
  99. /* LINE SENDER  */  
  100. bool Smtp::PutSendLine()  
  101. {  
  102.     static bool res = true;  
  103.     int current = linesend;  
  104.     switch(current)  
  105.     {  
  106.         case 1:  
  107.             response = SendLineAndGrab("ehlo localhost");  
  108.             if (response.size() > 0)  
  109.             {  
  110.                 ErrorMSG.append(response);  
  111.                 linesend = 2;  
  112.                 emit SendLine();  
  113.             }  
  114.             else  
  115.             {  
  116.                 res=false;  
  117.             }  
  118.             response ="";  
  119.   
  120.             break;  
  121.         case 2:  
  122.             response = SendLineAndGrab("AUTH LOGIN");  
  123.             if (response.size() > 0)  
  124.             {  
  125.                 ErrorMSG.append(response);  
  126.                 linesend = 3;  
  127.                 emit SendLine();  
  128.             }  
  129.             else  
  130.             {  
  131.                 res= false;  
  132.             }  
  133.             response ="";  
  134.   
  135.             break;  
  136.         case 3:  
  137.             response = SendLineAndGrab(encodeBase64(smtpusername));   /* username send */  
  138.             if (response.size() > 0)  
  139.             {  
  140.                 ErrorMSG.append(response);  
  141.                 if (response.contains("334", Qt::CaseInsensitive))  
  142.                 {  
  143.                 linesend = 4;  
  144.                 emit SendLine();  
  145.                 }  
  146.             }  
  147.             else  
  148.             {  
  149.                 res=false;  
  150.             }  
  151.             response ="";  
  152.   
  153.             break;  
  154.         case 4:  
  155.             response = SendLineAndGrab(encodeBase64(smtppass));     /* pass send */  
  156.             if (response.size() > 0)  
  157.             {  
  158.                 ErrorMSG.append(response);  
  159.                 if (response.contains("235", Qt::CaseInsensitive))  
  160.                 {  
  161.                     linesend = 5;  
  162.                     emit SendLine();  
  163.                 }  
  164.                 else  
  165.                 {  
  166.                     res= false;  
  167.                 }  
  168.             }  
  169.             else  
  170.             {  
  171.                 res= false;  
  172.             }  
  173.             response ="";  
  174.   
  175.             break;  
  176.         case 5:  
  177.         response = SendLineAndGrab(tr("MAIL FROM: %1").arg(smtpusername));  
  178.             if (response.size() > 0)  
  179.             {  
  180.   
  181.                 linesend = 6;  
  182.                 emit SendLine();  
  183.             }  
  184.   
  185.             break;  
  186.         case 6:  
  187.             response = SendLineAndGrab("RCPT TO: "+rcpt);  
  188.             if (response.size() > 0)  
  189.             {  
  190.                 ErrorMSG.append(response);  
  191.                 response ="";  
  192.                 response = SendLineAndGrab("DATA");  
  193.                 if (!response.contains("not", Qt::CaseInsensitive))  
  194.                 {  
  195.                     ErrorMSG.append(response);  
  196.                     response ="";  
  197.                     linesend = 7;  
  198.                     emit SendLine();  
  199.                 }  
  200.             }  
  201.             response ="";  
  202.   
  203.             break;  
  204.         case 7:  
  205.             response = SendLineAndGrab(message+"\r\n.");  
  206.             if (response.size() && response.contains("ok", Qt::CaseInsensitive) )  
  207.             {  
  208.                 ErrorMSG.append(response);  
  209.                 linesend = 8;  
  210.                 emit SendLine();  
  211.             }  
  212.             response ="";  
  213.   
  214.             break;  
  215.         case 8:  
  216.             SendLineAndGrab("QUIT");  
  217.   
  218.             break;  
  219.         default:  
  220.   
  221.             break;  
  222.     }  
  223.     return res;  
  224. }  
  225.   
  226. /* SENDER AND RECIVER  */  
  227. QString Smtp::SendLineAndGrab(QString senddata)  
  228. {  
  229.     QString incommingData = "";  
  230.   
  231.     if (isconnect)  
  232.     {  
  233.         int current = linesend;  
  234.         int loops = 0;  
  235.       
  236.         *t << senddata << "\r\n";  
  237.         t->flush();  
  238.         if (senddata != "QUIT") {  
  239.             if (smtpsocket->waitForReadyRead(Timeout))  
  240.             {  
  241.                 while (!t->atEnd())  
  242.                 {  
  243.                     loops++;  
  244.                     QString opera = t->readLine()+"\n";  
  245.                     incommingData = opera + incommingData;  
  246.                 }  
  247.             }  
  248.         } else  
  249.         {  
  250.             delete smtpsocket;  
  251.             delete t;  
  252.             isconnect = false;            
  253.             return incommingData;  
  254.         }  
  255.     }  
  256.     else  
  257.     {  
  258.          ErrorCloseAll();  
  259.     }  
  260.     return incommingData;  
  261. }  
  262.    
  263.   
  264. QString Smtp::encodeBase64( QString xml )  
  265. {  
  266.     QByteArray text;  
  267.     text.append(xml);  
  268.     return text.toBase64();  
  269. }  
  270.   
  271. QString Smtp::decodeBase64( QString xml )  
  272. {  
  273.     QByteArray xcode("");;  
  274.     xcode.append(xml);  
  275.     QByteArray precode(QByteArray::fromBase64(xcode));  
  276.     QString notetxt = precode.data();  
  277.     return notetxt;  
  278. }  
  279.   
  280.   
  281.   
  282. int Smtp::dateswap(QString form, uint unixtime )  
  283. {  
  284.     QDateTime fromunix;  
  285.     fromunix.setTime_t(unixtime);  
  286.     QString numeric = fromunix.toString((const QString)form);  
  287.     bool ok;  
  288.     return (int)numeric.toFloat(&ok);  
  289. }  
  290.   
  291.   
  292. QString Smtp::TimeStampMail()  
  293. {  
  294.     /* mail rtf Date format! http://www.faqs.org/rfcs/rfc788.html */  
  295.     QDateTime timer1( QDateTime::currentDateTime() );  
  296.       
  297.     uint unixtime = timer1.toTime_t();  
  298.     QDateTime fromunix;  
  299.     fromunix.setTime_t(unixtime);  
  300.        
  301.       
  302.     QStringList RTFdays = QStringList() << "giorno_NULL" << "Mon" << "Tue" << "Wed" << "Thu" << "Fri" << "Sat" << "Sun";  
  303.     QStringList RTFmonth = QStringList() << "mese_NULL" << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";  
  304.     QDate timeroad(dateswap("yyyy",unixtime),dateswap("M",unixtime),dateswap("d",unixtime));  
  305.   
  306.     QStringList rtfd_line;  
  307.     rtfd_line.clear();  
  308.     rtfd_line.append("Date: ");  
  309.     rtfd_line.append(RTFdays.at(timeroad.dayOfWeek()));  
  310.     rtfd_line.append(", ");  
  311.     rtfd_line.append(QString::number(dateswap("d",unixtime)));  
  312.     rtfd_line.append(" ");  
  313.     rtfd_line.append(RTFmonth.at(dateswap("M",unixtime)));  
  314.     rtfd_line.append(" ");  
  315.     rtfd_line.append(QString::number(dateswap("yyyy",unixtime)));  
  316.     rtfd_line.append(" ");  
  317.     rtfd_line.append(fromunix.toString("hh:mm:ss"));  
  318.     rtfd_line.append(" +0100");  
  319.     return QString(rtfd_line.join(""));  
  320. }  
  321.   
  322. void Smtp::ErrorCloseAll()  
  323. {  
  324.     delete t;  
  325.     smtpsocket->close();  
  326. }  

调用示例:

[cpp]  view plain  copy
  1. Smtp *sendmail = new Smtp("邮件服务器地址","账号","密码");  
  2.                     if(sendmail->Send(“收件人”,"主题",“内容”))  
  3.                     {  
  4.                         if(sendmail->PutSendLine())  
  5.                         {  
  6.                             qDebug() <<"发送成功.";                     
  7.                         }  
  8.                         else  
  9.                         {  
  10.                             qDebug() << "发送失败.";  
  11.                         }  
  12.                     }  


评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值