bool SmtpClient::sendMail(MimeMessage& email) { try { // Send the MAIL command with the sender sendMessage("MAIL FROM: <" + email.getSender().getAddress() + ">"); waitForResponse(); if (responseCode != 250) return false; // Send RCPT command for each recipient for (int i = 0; i < email.getRecipients().size(); ++i) { sendMessage("RCPT TO: <" + email.getRecipients().at(i)->getAddress() + ">"); waitForResponse(); if (responseCode != 250) return false; } // Send DATA command sendMessage("DATA"); waitForResponse(); if (responseCode != 354) return false; sendMessage(email.toString()); // Send \r\n.\r\n to end the mail data sendMessage("."); waitForResponse(); if (responseCode != 250) return false; } catch (ResponseTimeoutException) { return false; } return true; }
bool SmtpClient::sendMail(MimeMessage& email) { try { // Send the MAIL command with the sender sendMessage("MAIL FROM: <" + email.getSender().getAddress() + ">"); waitForResponse(); if (responseCode != 250) return false; // Send RCPT command for each recipient QList<EmailAddress*>::const_iterator it, itEnd; // To (primary recipients) for (it = email.getRecipients().begin(), itEnd = email.getRecipients().end(); it != itEnd; ++it) { sendMessage("RCPT TO: <" + (*it)->getAddress() + ">"); waitForResponse(); if (responseCode != 250) return false; } // Cc (carbon copy) for (it = email.getRecipients(MimeMessage::Cc).begin(), itEnd = email.getRecipients(MimeMessage::Cc).end(); it != itEnd; ++it) { sendMessage("RCPT TO: <" + (*it)->getAddress() + ">"); waitForResponse(); if (responseCode != 250) return false; } // Bcc (blind carbon copy) for (it = email.getRecipients(MimeMessage::Bcc).begin(), itEnd = email.getRecipients(MimeMessage::Bcc).end(); it != itEnd; ++it) { sendMessage("RCPT TO: <" + (*it)->getAddress() + ">"); waitForResponse(); if (responseCode != 250) return false; } // Send DATA command sendMessage("DATA"); waitForResponse(); if (responseCode != 354) return false; sendMessage(email.toString()); // Send \r\n.\r\n to end the mail data sendMessage("."); waitForResponse(); if (responseCode != 250) return false; } catch (ResponseTimeoutException) { return false; } catch (SendMessageTimeoutException) { return false; } return true; }