Bug 33937 - SendMailAsync task never completes
Summary: SendMailAsync task never completes
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: System (show other bugs)
Version: 4.0.0
Hardware: Other Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2015-09-14 14:16 UTC by nippius
Modified: 2016-04-25 23:01 UTC (History)
2 users (show)

See Also:
Is this bug a regression?: ---
Last known good build:

Test case for this issue. (1.27 KB, text/plain)
2015-09-14 14:16 UTC, nippius

Description nippius 2015-09-14 14:16:48 UTC
Created attachment 12887 [details]
Test case for this issue.

Calling the function SendMailAsync() to send a mail message, returns a Task that never completes.
When using microsoft .Net the Task completes as expected.
It dosen't matter if using the SendMailAsync(MailMessage msg) or SendMailAsync(String from, String to, String subject, String body);

How to test:

1 - Edit mail_test.cs and change credentials for smtp server.
2 - Compile and run with:
mcs mail_test.cs
mono mail_test.exe

Expected output:

Actual output:
<nothing, program waits forever for task to complete.>

Tested on:

- Linux mint: 
 Kernel: 3.13.0-37-generic #64-Ubuntu SMP Mon Sep 22 21:28:38 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
 Mono: Mono JIT compiler version 4.0.4 (Stable Tue Aug 25 23:11:51 UTC 2015)

- Raspberry pi running arch linux:
 kernel: Linux archpi 4.1.6-3-ARCH #1 SMP PREEMPT Tue Sep 8 19:52:33 MDT 2015 armv7l GNU/Linux
 Mono: Mono JIT compiler version 4.0.3 (Stable Wed Sep  2 13:04:42 MDT 2015)
Comment 1 nippius 2015-09-14 14:27:46 UTC
Forgot to add that the mail is actually sent. The correct test mail is received in my inbox as expected. It just the Task that isn´t marked as completed (and its not marked as faulted either.)
Comment 2 jan 2016-04-25 23:01:59 UTC
I'm experiencing the same issue.

Mono 4.2.3


Use code from Microsoft .NET SmtpClient: https://github.com/Microsoft/referencesource/blob/master/System/net/System/Net/mail/SmtpClient.cs#L720:L762


using System.Threading.Tasks;
using System.Net.Mail;
using System.ComponentModel;

namespace Fix
	public class AsyncSmtpClient : SmtpClient
		public AsyncSmtpClient(string host, int port) : base(host, port) {}

		new public Task SendMailAsync(string from, string recipients, string subject, string body)
			var message = new MailMessage(from, recipients, subject, body);
			return SendMailAsync(message);
		new public Task SendMailAsync(MailMessage message)
			// Create a TaskCompletionSource to represent the operation
			var tcs = new TaskCompletionSource<object>();

			// Register a handler that will transfer completion results to the TCS Task
			SendCompletedEventHandler handler = null;
			handler = (sender, e) => HandleCompletion(tcs, e, handler);
			SendCompleted += handler;

			// Start the async operation.
			try { SendAsync(message, tcs); }
				this.SendCompleted -= handler;

			// Return the task to represent the asynchronous operation
			return tcs.Task;

		private void HandleCompletion(TaskCompletionSource<object> tcs, AsyncCompletedEventArgs e, SendCompletedEventHandler handler)
			if (e.UserState == tcs)
				try { SendCompleted -= handler; }
					if (e.Error != null) tcs.TrySetException(e.Error);
					else if (e.Cancelled) tcs.TrySetCanceled();
					else tcs.TrySetResult(null);

Note You need to log in before you can comment on or make changes to this bug.