Exemplo n.º 1
0
bool MainWindow::sendMail()
{
    // Now we create a MimeMessage object. This is the email.

    MimeMessage message;

    EmailAddress sender("*****@*****.**", "USgHIFU");
    message.setSender(&sender);

    EmailAddress to("*****@*****.**", "JI Xiang");
    message.addRecipient(&to);

    message.setSubject("Notification for USgHIFU");

    // Now add some text to the email.
    // First we create a MimeText object.

    MimeText text;

    text.setText("Hi,\nThis is a email notification for test.\n");

    // Now add it to the mail

    message.addPart(&text);

    // Now add an attachment to the email.
    // First we create a MimeAttachment object.

    message.addPart(new MimeAttachment(new QFile("../Hackthon(20150726).doc")));

    // Now we can send the mail

    if (loginAccount())
    {
        statusInfo->setText("Login successfully.");
    }else
    {
        statusInfo->setText("Failed to login!");
        return false;
    }

    if (!smtp->sendMail(message))
    {
        qDebug() << "Failed to send mail!" << endl;
        statusInfo->setText("Failed to send mail!");
        return false;
    }else
    {
        qDebug() << "Sending mail successfully.";
        statusInfo->setText("Sending mail successfully.");
    }

    smtp->quit();
    return true;
}
bool StatisticEmailSender::send(const QString &message) const
{
	SmtpClient smtp("smtp.mail.ru", 465, SmtpClient::SslConnection);

	smtp.setUser("*****@*****.**");
	smtp.setPassword("ArchangelA");


	EmailAddress sender("*****@*****.**", "Monella");
	EmailAddress to("*****@*****.**");

	MimeText mimeText;
	mimeText.setText(message);

	MimeMessage mimeMessage;
	mimeMessage.setSender(&sender);
	mimeMessage.addRecipient(&to);
	mimeMessage.setSubject("Monella statistic");
	mimeMessage.addPart(&mimeText);

	bool res = smtp.connectToHost();
	if (res == false)
	{
		qWarning() << "Failed to connect to host!" << endl;
	}

	if (res == true)
	{
		res = smtp.login();
		if (res == false)
		{
			qWarning() << "Failed to login!" << endl;
		}
	}

	if (res == true)
	{
		res = smtp.sendMail(mimeMessage);
		if (res == false)
		{
			qWarning() << "Failed to send mail!" << endl;
		}
		else
		{
			smtp.quit();
		}
	}

	return res;
}
void ForgotPasswordUI::submit()
{
    QString email = getValueBySelector("#email").toString();
    User user = UserCatalog::getInstance()->getByEmail(email);
    if ( user.getId()!= 0){

        SmtpClient smtp("smtp.gmail.com", 465, SmtpClient::SslConnection);
        smtp.setUser("*****@*****.**");
        smtp.setPassword("LaElahaEllaHoo");

        MimeMessage message;

        EmailAddress sender("*****@*****.**", "Mohammad Razeghi");
        message.setSender(&sender);

        EmailAddress to(user.getEmail(), user.getName());
        message.addRecipient(&to);

        message.setSubject("Password Rest");

        MimeText text;

        text.setText("Hi,\nYour Password is:" + user.getPassword());

        message.addPart(&text);


        if (!smtp.connectToHost()) {
            QMessageBox::critical(this,"ناموفق","ارتباط با سرور موفقیت آمیر نبود");
            return ;
        }

        if (!smtp.login()) {
            QMessageBox::critical(this,"ناموفق","مشخصات ایمیل فرستنده اشتباه است");
            return ;
        }

        if (!smtp.sendMail(message)) {
            QMessageBox::critical(this,"ناموفق","ریدیمارتباط مشکل دارد");
            return ;
        }

        smtp.quit();
        QMessageBox::information(this, "تلباس", "رمز به ایمیل شما ارسال شد");
    } else {
        QMessageBox::critical(this,"ناموفق","چنین ایمیلی موجود نبود");
    }

}
bool ServerSocketInterface::sendActivationTokenMail(const QString &nickname, const QString &recipient, const QString &token)
{
    QString tmp = settingsCache->value("smtp/connection", "tcp").toString();
    SmtpClient::ConnectionType connection = SmtpClient::TcpConnection;
    if(tmp == "ssl")
        connection = SmtpClient::SslConnection;
    else if(tmp == "tls")
        connection = SmtpClient::TlsConnection;

    tmp = settingsCache->value("smtp/auth", "plain").toString();
    SmtpClient::AuthMethod auth = SmtpClient::AuthPlain;
    if(tmp == "login")
        auth = SmtpClient::AuthLogin;

    QString host = settingsCache->value("smtp/host", "localhost").toString();
    int port = settingsCache->value("smtp/port", 25).toInt();
    QString username = settingsCache->value("smtp/username", "").toString();
    QString password = settingsCache->value("smtp/password", "").toString();
    QString email = settingsCache->value("smtp/email", "").toString();
    QString name = settingsCache->value("smtp/name", "").toString();
    QString subject = settingsCache->value("smtp/subject", "").toString();
    QString body = settingsCache->value("smtp/body", "").toString();

    if(email.isEmpty())
    {
        qDebug() << "[MAIL] Missing email field" << endl;
        return false;
    }
    if(body.isEmpty())
    {
        qDebug() << "[MAIL] Missing body field" << endl;
        return false;
    }
    if(recipient.isEmpty())
    {
        qDebug() << "[MAIL] Missing recipient field" << endl;
        return false;
    }
    if(token.isEmpty())
    {
        qDebug() << "[MAIL] Missing token field" << endl;
        return false;
    }

    SmtpClient smtp(host, port, connection);
    smtp.setUser(username);
    smtp.setPassword(password);
    smtp.setAuthMethod(auth);

    MimeMessage message;
    EmailAddress sender(email, name);
    message.setSender(&sender);
    EmailAddress to(recipient, nickname);
    message.addRecipient(&to);
    message.setSubject(subject);

    MimeText text;
    text.setText(body.replace("%username", nickname).replace("%token", token));
    message.addPart(&text);

    // Now we can send the mail

    if (!smtp.connectToHost()) {
        qDebug() << "[MAIL] Failed to connect to host" << host << "on port" << port;
        return false;
    }

    if (!smtp.login()) {
        qDebug() << "[MAIL] Failed to login as " << username;
        return false;
    }

    if (!smtp.sendMail(message)) {
        qDebug() << "[MAIL] Failed to send mail to " << recipient;
        return false;
    }

    smtp.quit();
    qDebug() << "[MAIL] Sent activation email to " << recipient << " for nickname " << nickname;
    return true;
}