# Sending

To send a file by AS2, a job hands it to an **AS2 task**. The task signs it with your key, encrypts it for the partner, posts it to the partner's address, and checks the partner's receipt.

The task's fields are described on its own page: [AS2 task](Task-AS2.md). This page is about the whole of sending: the job around the task, receipts that come later, and what goes wrong.

Sending needs an Enterprise licence, as in earlier versions.

## The job

A sending job has the same shape for every partner:

| Step | Task | Does |
|---|---|---|
| 1 | [Find](Task-Find.md) | lists the files that wait in the outgoing folder |
| 2 | [AS2](Task-AS2.md) | sends each, and waits for the receipts |
| 3 | [Jump](Task-Jump.md) | sorts the files by `{mdn_result}`: sent and acknowledged, or not |
| 4a | [Move](Task-Move.md) | acknowledged files go to an archive folder, with their receipts |
| 4b | [Email](Task-Email.md), [Move](Task-Move.md) | the others go to a folder for failures, and somebody is told |

There is no Copy task in it. The AS2 task is the transfer.

Run it on a [schedule](Jobs-Schedules.md), every few minutes, or start it from the upload [event](Users-Events.md) of the user who drops the files.

### Sorting by the receipt

Each file leaves the AS2 task with `{mdn_result}`: what the receipt said. For a file that was sent and acknowledged it is empty. In the Jump task, compare `{mdn_result}` with nothing: true leads to the archive, false to the failures.

Both exits of the AS2 task, the one for success and the one for failure, should lead to that Jump, so that each file is judged by its own receipt.

### Never lose a file

- Move a file out of the outgoing folder only **after** its receipt was good. A file that stays is tried again at the next run.
- A file that fails again and again must not be tried for ever: move it to the failures folder after the first failed run, and let a person look.
- Keep receipts with the sent files. They are your proof of delivery.

## What the task needs

| From | The task gets |
|---|---|
| you | **From Partner**: your AS2 name. **Signing**: your key pair. **Sign Message** and **Encrypt Message**: a new task starts with sha1 and aes_128; set sha-256 and aes_256 unless the partner cannot. |
| the partner | **To Partner**: their AS2 name. **Recipient URL**: their address. **Encryption**: their certificate. The algorithms. |

See [Trading partners](AS2-Partners.md) and [AS2 certificates](AS2-Certificates.md).

## Receipts at once, or later

A partner can answer in the same connection, or later, in a connection of their own.

**At once** is simple: when the send ends, the task has the receipt.

**Later** (**Async MDN** in the task) needs a way back:

| Needed | Is |
|---|---|
| **Response URL** in the task | the address of this server that the partner posts the receipt to |
| a user for the receipts | a user of the User Manager with one folder, whom the partner signs in as for the receipt, in the way of [version 1 receiving](AS2-Receiving.md#the-as2-name-carries-the-login). It can be the same user the partner sends files to. |
| **MDN Wait Timeout** | how long the task waits for the receipt: 60 seconds in a task made in the designer. After it: "Timeout reached while waiting for MDN responses." |

The task waits for the later receipt inside the run. A receipt that comes after the task gave up is stored in the user's folder as a file, but no task learns of it, and the file counts as failed. For partners whose receipts take long, set the timeout generously: a minute is short.

> [!WARNING]
> A new AS2 task comes with a **Response URL** of `https://www.yourserver.com/`, which is a placeholder. With **Async MDN** on, the partner would send the receipt there. Replace it with your server's public address before you turn **Async MDN** on.

Ask for receipts at once unless the partner cannot. It is simpler and nothing can go missing in between.

## What is checked on a receipt

The task compares the checksum in the receipt with the checksum of what it sent. If they match, the file arrived unchanged.

## Sending with version 2

The same task can run on the new engine. A task whose setting `as2_engine` is `v2` is carried out by version 2; every field keeps its meaning.

| | Version 1 | Version 2 |
|---|---|---|
| the file | read into memory, several times its size | streamed from where it lies |
| a send that fails | is tried again, up to six times in all, each as a new message. A partner who got the first one stores the file twice. | is tried again as the same message, which the partner can recognise |
| the receipt | the checksum is compared | the checksum, the signature, and what the receipt says about the outcome |
| a receipt that comes later | must arrive while the task waits | is matched to its send whenever it comes, as long as the record of the send is kept: 30 days |
| weak algorithms | Md5 and the rc4 kinds can be chosen | never sent |
| in [FIPS mode](FIPS-Mode.md) | the task fails, and says why | works |

The task form has no field for the engine. It is set by the import tool, per task, and can be set back: [Moving from AS2 version 1](AS2-Upgrade-from-v1.md). A version 1 task says in its log on every run that version 2 is available; setting `as2_engine` to `v1` keeps a task on version 1 without that line.

Version 2 must be turned on for the server before a task can use it.

## When a send fails

| The task says | Look at |
|---|---|
| a connection error | the **Recipient URL**; the partner's firewall, which often lets in only known addresses: give them yours |
| a TLS error | the partner's HTTPS certificate is not trusted by the server. **Accept Any Cert** proves it, for a test; the cure is to trust their certificate properly. |
| 401, 403 | the partner wants a login for the connection, or does not know your address |
| a receipt that says decryption failed | you encrypted for a certificate that is not the one they decrypt with |
| a receipt that says the signature could not be checked | they do not have the certificate of the key you sign with, or have an old one |
| a receipt that names an unknown partner | the AS2 names: **From Partner** and **To Partner**, letter for letter |
| a checksum that does not match | the **MIC Algorithm**, or a partner that computes it over other bytes. Try with **Use compression** off. |
| "Timeout reached while waiting for MDN responses." | the receipt came too late, went to the wrong address, or the receipt user could not be signed in |

Turn on **Debug** for the job and read its log: the task writes what it sent and what came back.
