Step 1. Let $s > 0$ and $\beta \in \mathbb{C}$. Then using the gaussian integral $\int_{-\infty}^{\infty} e^{-sx^2} \, dx = \sqrt{\pi/s}$, we have
\begin{align*}
\int_{-\infty}^{\infty} e^{-s(x-\beta)^2} \, dx
&= \int_{-\infty}^{\infty} \bigg( e^{-sx^2} + \int_{0}^{1} \overbrace{ 2s\beta(x-\beta t) e^{-s(x-\beta t)^2} }^{= \frac{\partial}{\partial t} e^{-s(x-\beta t)^2}} \, dt \bigg) \, dx \\
&= \sqrt{\frac{\pi}{s}} -\beta \int_{0}^{1} \int_{-\infty}^{\infty} (-2s)(x-\beta t) e^{-s(x-\beta t)^2} \, dx dt \quad {\small(\because\text{Fubini})}\\
&= \sqrt{\frac{\pi}{s}} -\beta \int_{0}^{1} \left[ e^{-s(x-\beta t)^2} \right]_{x=-\infty}^{x=\infty} \, dt
= \bbox[border:1px dashed green,6px]{ \sqrt{\frac{\pi}{s}} }.
\end{align*}
Then plugging $\beta=\pm\mathrm{i}/2s$, we check that
$$ \int_{-\infty}^{\infty} e^{-sx^2}\cos(x) \, dx = \sqrt{\frac{\pi}{s}} e^{-1/4s}. $$
Step 2. Using the previous step,
\begin{align*}
I := \int_{-\infty}^{\infty} \frac{\cos x}{x^2+1} \, dx
&= \int_{-\infty}^{\infty} \cos x \left( \int_{0}^{\infty} e^{-(x^2+1)s} \, ds \right) \, dx \\
&= \int_{0}^{\infty} \left( \int_{-\infty}^{\infty} e^{-sx^2}\cos(x) \, dx \right) e^{-s} \, ds \quad {\small(\because\text{Fubini})}\\
&= \int_{0}^{\infty} \sqrt{\frac{\pi}{s}} e^{-\left( s + \frac{1}{4s}\right)} \, ds \\
&= \int_{0}^{\infty} \sqrt{2\pi} e^{-\frac{1}{2}\left( t^2 + \frac{1}{t^2}\right)} \, dt. \quad {\small(2s=t^2)}
\end{align*}
Finally here is a very slick way of computing the last integral. Applying the substitution $t\mapsto 1/t$ shows that
$$ \int_{0}^{\infty} e^{-\frac{1}{2}\left( t^2 + \frac{1}{t^2}\right)} \, dt = \int_{0}^{\infty} \frac{1}{t^2} e^{-\frac{1}{2}\left( t^2 + \frac{1}{t^2}\right)} \, dt. $$
So averaging,
$$
I = \sqrt{\frac{\pi}{2}} \int_{0}^{\infty} \left(1 + \frac{1}{t^2}\right) e^{-\frac{1}{2}\left( t - \frac{1}{t}\right)^2 - 1} \, dt.
$$
Finally, applying the substitution $u = t - \frac{1}{t}$ proves
$$
I
= \sqrt{\frac{\pi}{2}} \int_{-\infty}^{\infty} e^{-\frac{u^2}{2} - 1} \, du
= \frac{\pi}{e}. $$