How to send Email from my application?
-
@Long-Vu-Ngoc it is simple.sendind emails is through the SMTP protocol
1.Go a read about the smtp protocol and commands
2.Create a Tcp socket and connect to the smtp server
3.Write hello to the server and see if the connection is successful.
4. In your mail account you should grant access of your application
5. Use Switch structure to Switch between the differences responses of the server, in order to now which info you should send( title, body, attachements etc...)this is an exemple of code i used for tests.However it is not complete.Hope that it helps.
main.cpp
#include <QCoreApplication>
#include <QDebug>
#include <QString>
#include <iostream>
#include <QHostAddress>
#include <QTextStream>
#include "smtp.h"QString command;
QString password;
QString email;
QString host;
qint16 port;
using namespace std;
int main(int argc, char *argv[])
{QCoreApplication a(argc, argv); smtp client; command =""; qDebug()<<"----------------------"; qDebug()<<"MailSender 2.0\r\n"; qDebug()<<"----------------------"; qDebug()<<"serverName: stmp.mail.yahoo.com"; qDebug()<<"port: 25"; qDebug()<<"CommandList -->"; qDebug()<<"HELO"; qDebug () <<"EHLO"; qDebug()<<"AUTH LOGIN"; qDebug()<<"MAIL FROM"; qDebug () <<"RCPT TO"; qDebug () <<"TURN"; qDebug () <<"ATRN"; qDebug () <<"SIZE"; qDebug () <<"ETRN"; qDebug () <<"PIPELINING"; qDebug () <<"CHUNKING"; qDebug () <<"DATA"; qDebug () <<"DSN"; qDebug () <<"RSET"; qDebug () <<"VRFY"; qDebug () <<"HELP"; qDebug () <<"QUIT"; qDebug()<<"----------------------"; client.socket->connectToHost("smtp.mail.yahoo.com", 25); qDebug()<<"connecting"; return a.exec();
}
smtp.h#ifndef SMTP_H
#define SMTP_H#include <QObject>
#include <QTcpSocket>class smtp : public QObject
{
Q_OBJECTpublic:
explicit smtp(QObject *parent = 0);
~smtp();
QTcpSocket *socket;
QString last;
int state;
int c;
enum states{
HELLO,
AUTH_LOGIN,
USER,
PASSWORD};
protected:private slots:
void stateChanged(QAbstractSocket::SocketState socketState);
void errorReceived(QAbstractSocket::SocketError socketError);void disconnected(); void connected(); void readyread(); void enteraCommand(QString instruction);
};
#endif // SMTP_H
smtp.cpp
#include "smtp.h"
#include <QTextStream>
#include <QString>
#include <QTimer>
#include <QTime>
#include <stdio.h>
#include <iostream>
#include <QTime>
#include <QHostAddress>using namespace std;
QString c;
smtp::smtp(QObject *parent) :
QObject(parent)
{
socket = new QTcpSocket(this);
connect(socket,SIGNAL(connected()), this, SLOT(connected()));
connect(socket,SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(socket,SIGNAL(readyRead()), this, SLOT(readyread()));
connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)),this, SLOT(stateChanged(QAbstractSocket::SocketState)));
connect(socket,SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(errorReceived(QAbstractSocket::SocketError)));
last = "";
state = HELLO;
c =0;}
smtp::~smtp()
{}
void smtp::connected()
{
qDebug()<<"connected";
socket->write("HELO yahoo.com\r\n");}
void smtp::disconnected()
{
qDebug()<<"disconnected";
qDebug()<<socket->errorString();qDebug()<<"1= Try again"; QString response; QTextStream stream(stdin); stream >> response; if(response == "1") { socket->connectToHost("smtp.mail.yahoo.com", 25); qDebug() <<"connecting to smtp.mail.yahoo.com"; } else { qDebug()<<"invalid command"; }
}
void smtp::readyread()
{
while (!socket->atEnd()) {
QString line = socket->readLine();
qDebug()<< line;
if(line.startsWith("220"))
{
QString instruction = "enter the command AUTH LOGIN";
enteraCommand(instruction);
}
else if(line.startsWith("250"))
{
qDebug()<<line;
enteraCommand("entrez une commande");
}
else
if(line.startsWith("334"))
{
if(c == 1)
{
enteraCommand("password");
}
else
{
enteraCommand("email");
}
}
else
{
qDebug()<<line;
enteraCommand("entrez une commande");
}}
}void smtp::stateChanged(QAbstractSocket::SocketState socketState)
{
qDebug()<<socketState;
}
void smtp::errorReceived(QAbstractSocket::SocketError socketError)
{
qDebug()<< socketError;}
void smtp::enteraCommand(QString instruction)
{
QString command;
qDebug()<<instruction;
QTextStream stream(stdin);
stream >> command;
if(instruction == "password" || instruction == "email")
{
socket->write(command.toLatin1().toBase64());
}else
{
socket->write(command.toLatin1());
}}
smtp.ui
<?xml version='1.0'?>
<ui version="4.0">
<author/>
<comment/>
<exportmacro/>
<class>smtp</class>
<widget name="smtp" class="QWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>640</width>
<height>480</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
</widget>
<pixmapfunction/>
<connections/>
</ui>QT += core network
QT -= guiCONFIG += c++11
TARGET = mailSender_conso
CONFIG += console
CONFIG -= app_bundleTEMPLATE = app
SOURCES += main.cpp
smtp.cppHEADERS +=
smtp.h -
@Ronel_qtmaster
Hi there. Your code may be fine, but would benefit from enclosing inside forum's Code tags (icon</>
when posting, or```
(3 backticks) above & below code block.The problem for the OP is that (a) he needs to have some external email account to do this and (b) your code may work with
smtp.mail.yahoo.com
but, say,gmail
's one long since stopped users from doing this and require user to be logged in or use some other form of authentication before they allow it these days. It's only fair to bring this to OP's attention. -