Kostya_I's complex analytic answer and Conrad's Fourier analytic proof in the comments complement each other nicely. The purpose of the present answer is to relate your question to the literature. As pointed out already, the RHS is periodic in $\theta$ so we may restrict to $|\theta| \le \pi$. For $\theta=\pi$, $\cos(k \theta)=(-1)^k$ and the identity in question is $$(\star)\, \pi \cot(\pi t) = \frac{1}{t}+\lim_{N\to \infty}\sum_{|n| \le N}\frac{1}{t+n}=\frac{1}{t}+2t\sum_{n =1}^{\infty}\frac{1}{t^2-n^2},$$ $t$ non-integer. This identity also holds for complex $t$. It is due to Euler and can be obtained by carefully differentiating the product formula for $\sin(\pi z)$, $$(\star \star)\, \sin(\pi z)= \pi z\prod_{n \ge 1}(1-z^2/n^2).$$ One can also derive this product formula from it. For $\theta=0$, this gives the identity $$(\star\star \star)\,\frac{\pi}{\sin(\pi t)} = \frac{1}{z}+2t\sum_{n =1}^{\infty}\frac{(-1)^n}{z^2-n^2}.$$ It again holds for $t$ non-integer, and extends to complex $t$. There are many proofs of $(\star)$, $(\star \star)$ and $(\star \star)$ in the literature (as well as of variations of these identities) and some of them, possibly all of them, extend to prove your identity when $|\theta|\le \pi$. I am going to give a partial review of these proofs. From "Fourier analysis. An Introduction" by Elias Stein and Rami Shakarchi (Princeton Lectures in Analysis. 1, Princeton University Press, 2003): 1. Page 90, exercise 9: For fixed non-integer $\alpha$, the Fourier expansion of $\frac{\pi}{\sin(\pi \alpha)} e^{i(\pi-x)\alpha}$ for $x \in [0,2\pi]$ is worked out to be $$\sum_{n \in \mathbb{Z}} \frac{e^{inx}}{n+\alpha}.$$ This related to Conrad's answer in the comments -- taking real parts gives your identity. Applying Parseval gives the identity $$(\star \star \star \star)\, \sum_{n\in \mathbb{Z}}\frac{1}{(n+\alpha)^2}=\frac{\pi^2}{\sin^2(\pi \alpha)}.$$ 2. Page 97, exercise 97(c): A proof of $(\star \star \star)$, the $\theta=0$ case of your identity, is given. It is proved as a consequence of Euler's identity $\sum_{n\ge 1}\frac{1}{n^2 - \alpha^2} = \frac{1}{2\alpha^2} - \frac{\pi}{2\alpha\tan(\alpha \pi)}$ (whose proof is given and follows from a suitable Fourier expansion). Indeed, applying Euler's identity with $\alpha$ and $\alpha/2$ and taking a linear combination gives $(\star \star \star)$. 3. Page 165, exercise 15: Poisson summation applied to a (shift of) $g(t)=(1-|t|)\mathbf{1}_{|t|\le 1}$ gives $(\star \star \star \star)$. A consequence, follows by integration is also given: $$(\star^5)\, \lim_{N \to \infty}\sum_{|n| \le N}\frac{1}{n+\alpha} = \frac{\pi}{\tan(\pi \alpha)}.$$ From the book "Complex analysis" by the same authors: 4. Page 105, exercise 12: A proof of $(\star \star \star \star)$ is given as a consequence of integrating $f(z)=\pi \cot(\pi \alpha)/(\alpha+z)^2$ over a circle of growing radius and using Cauchy's residue theorem. 5. Page 129, exercise 7(b,c): A proof of $(\star \star \star \star)$ is given, by applying Poisson summation to $f(z) = (\tau+z)^{-2}$ ($\Im \tau>0$). 6. Page 142: A proof of $(\star)$ is given by proving that both sides, as functions of complex variable, satisfy 3 properties which determine a function uniquely. Kostya_I's answer can be seen as an extension of it. Bonus: 7. There is a real-analytic variant of the last proof of $(\star)$, attributed to Gustav Herglotz, and appearing in Chapter 26 of M. Aigner and G. Ziegler's "Proofs from THE BOOK", 6th edition. In the complex analytic proof, 3 properties are established for each side of the identity: i) Each side is a meromorphic function with simple poles at the integers, and no other singularities. ii) Each side is periodic with period $1$. iii) The residue of each side at $z=0$ is $1$. In the real-analytic proof, the following properties are established instead: i) Each is defined for $t \in \mathbb{R}\setminus \mathbb{Z}$ and is continuous there. ii) Each side is periodic with period $1$. iii) Each side is an odd function. iv) Both sides satisfy the functional equation $f(x/2)+f((x+1)/2) = 2f(x)$.