smtp_transport_send_to_sync (CamelTransport *transport,
                             CamelMimeMessage *message,
                             CamelAddress *from,
                             CamelAddress *recipients,
			     gboolean *out_sent_message_saved,
                             GCancellable *cancellable,
                             GError **error)
{
	CamelSmtpTransport *smtp_transport = CAMEL_SMTP_TRANSPORT (transport);
	CamelInternetAddress *cia;
	CamelStreamBuffer *istream;
	CamelStream *ostream;
	gboolean has_8bit_parts = FALSE;
	const gchar *addr;
	gint i, len;

	smtp_debug_print_server_name (CAMEL_SERVICE (transport), "Sending with");

	istream = smtp_ref_istream (smtp_transport);
	ostream = smtp_ref_ostream (smtp_transport);

	if (!smtp_transport->connected || !istream || !ostream) {
		g_clear_object (&istream);
		g_clear_object (&ostream);
		g_set_error (
			error, CAMEL_SERVICE_ERROR,
			CAMEL_SERVICE_ERROR_NOT_CONNECTED,
			_("Cannot send message: service not connected."));
		return FALSE;
	}

	if (!camel_internet_address_get (CAMEL_INTERNET_ADDRESS (from), 0, NULL, &addr)) {
		g_clear_object (&istream);
		g_clear_object (&ostream);
		g_set_error (
			error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
			_("Cannot send message: sender address not valid."));
		return FALSE;
	}

	camel_operation_push_message (cancellable, _("Sending message"));

	/* find out if the message has 8bit mime parts; quoted-printable are reencoded to 8bit too */
	camel_mime_message_foreach_part (message, message_has_8bit_or_qp_part_cb, &has_8bit_parts);

	/* If the connection needs a ReSET, then do so */
	if (smtp_transport->need_rset &&
	    !smtp_rset (smtp_transport, istream, ostream, cancellable, error)) {
		camel_operation_pop_message (cancellable);
		g_clear_object (&istream);
		g_clear_object (&ostream);
		return FALSE;
	}
	smtp_transport->need_rset = FALSE;

	/* rfc1652 (8BITMIME) requires that you notify the ESMTP daemon that
	 * you'll be sending an 8bit mime message at "MAIL FROM:" time. */
	if (!smtp_mail (
		smtp_transport, istream, ostream, addr, has_8bit_parts, cancellable, error)) {
		camel_operation_pop_message (cancellable);
		g_clear_object (&istream);
		g_clear_object (&ostream);
		return FALSE;
	}

	len = camel_address_length (recipients);
	if (len == 0) {
		g_set_error (
			error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
			_("Cannot send message: no recipients defined."));
		camel_operation_pop_message (cancellable);
		smtp_transport->need_rset = TRUE;
		g_clear_object (&istream);
		g_clear_object (&ostream);
		return FALSE;
	}

	cia = CAMEL_INTERNET_ADDRESS (recipients);
	for (i = 0; i < len; i++) {
		gchar *enc;

		if (!camel_internet_address_get (cia, i, NULL, &addr)) {
			g_set_error (
				error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
				_("Cannot send message: "
				"one or more invalid recipients"));
			camel_operation_pop_message (cancellable);
			smtp_transport->need_rset = TRUE;
			g_clear_object (&istream);
			g_clear_object (&ostream);
			return FALSE;
		}

		enc = camel_internet_address_encode_address (NULL, NULL, addr);
		if (!smtp_rcpt (smtp_transport, istream, ostream, enc, cancellable, error)) {
			g_free (enc);
			camel_operation_pop_message (cancellable);
			smtp_transport->need_rset = TRUE;
			g_clear_object (&istream);
			g_clear_object (&ostream);
			return FALSE;
		}
		g_free (enc);
	}

	if (!smtp_data (smtp_transport, istream, ostream, message, cancellable, error)) {
		camel_operation_pop_message (cancellable);
		smtp_transport->need_rset = TRUE;
		g_clear_object (&istream);
		g_clear_object (&ostream);
		return FALSE;
	}

	camel_operation_pop_message (cancellable);
	g_clear_object (&istream);
	g_clear_object (&ostream);

	return TRUE;
}