Send SMTP Mail Message Error:- System.Net.Sockets.SocketException: A connection attempt failed

I am trying to run a simple “Send SMTP Mail Message” to send an email using my Gmail account, here is the simple workflow (all of it)

I have set the properties appropriately as you can see here

  • Set Port and Server
  • Using App Password for Gmail account, set both normal and secure password.
  • Secure Connection Set to “Auto”


When I run this, I get this error, what do I do?

System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
   at System.Threading.Tasks.ValueTask.ValueTaskSourceAsTask.<>c.<.cctor>b__4_0(Object state)
--- End of stack trace from previous location ---
   at System.Threading.Tasks.TaskToAsyncResult.End(IAsyncResult asyncResult)
   at MailKit.Net.SocketUtils.ConnectAsync(String host, Int32 port, IPEndPoint localEndPoint, Boolean doAsync, CancellationToken cancellationToken) in D:\src\MailKit\MailKit\Net\SocketUtils.cs:line 71
   at MailKit.Net.SocketUtils.ConnectAsync(String host, Int32 port, IPEndPoint localEndPoint, Int32 timeout, Boolean doAsync, CancellationToken cancellationToken) in D:\src\MailKit\MailKit\Net\SocketUtils.cs:line 100
   at MailKit.MailService.ConnectNetwork(String host, Int32 port, Boolean doAsync, CancellationToken cancellationToken) in D:\src\MailKit\MailKit\MailService.cs:line 594
   at MailKit.Net.Smtp.SmtpClient.ConnectAsync(String host, Int32 port, SecureSocketOptions options, Boolean doAsync, CancellationToken cancellationToken) in D:\src\MailKit\MailKit\Net\Smtp\SmtpClient.cs:line 1318
   at UiPath.Mail.MailServiceFactory.CreateMailKitService(EmailProtocol protocol, String server, Int32 port, UserSettings userSettings, SecureSocketOptions options, Int32 timeout, CancellationToken cancellationToken)
   at UiPath.Mail.MailServiceFactory.CreateSmtpClient(String server, Int32 port, UserSettings userSettings, SecureSocketOptions options, CancellationToken cancellationToken, Int32 timeout)
   at UiPath.Mail.MimeMailService.SendSMTPMail(MimeMessage mimemessage, UserSettings userSettings, ServerSettings serverSettings, SecureSocketEncryption secureConnection, CancellationToken cancellationToken, Boolean authenticateFirst)
   at UiPath.Mail.MimeMailService.SendSMTPMail(MailMessage mailMessage, UserSettings userSettings, Receiver receiver, ServerSettings serverSettings, SecureSocketEncryption secureConnection, List`1 attachments, CancellationToken ct, Boolean isNewMessage, String body)
   at UiPath.Mail.SMTP.Activities.SendMail.<>c__DisplayClass66_0.<SendMailTask>b__0()
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
   at UiPath.Mail.SMTP.Activities.SendMail.SendMailTask(AsyncCodeActivityContext context, Receiver receiver, MailMessage mailMessage, CancellationToken cancellationToken, Boolean isNewMessage, String body)
   at UiPath.Mail.Activities.Extensions.TaskExtensions.TimeoutAfter(Task task, Int32 timeoutMs)
   at UiPath.Mail.Activities.SendMailActivity.ExecuteAsync(AsyncCodeActivityContext context, CancellationToken cancellationToken)
   at UiPath.Shared.Activities.AsyncTaskCodeActivityImplementation.EndExecute(AsyncCodeActivityContext context, IAsyncResult result)
   at UiPath.Shared.Activities.AsyncTaskCodeActivity.EndExecute(AsyncCodeActivityContext context, IAsyncResult result)
   at UiPath.Shared.Activities.ContinuableAsyncCodeActivity.EndExecute(AsyncCodeActivityContext context, IAsyncResult result)
   at UiPath.Mail.Activities.BaseMailContinuableAsyncActivity.EndExecute(AsyncCodeActivityContext context, IAsyncResult result)
   at UiPath.Mail.SMTP.Activities.SendMail.EndExecute(AsyncCodeActivityContext context, IAsyncResult result)
   at System.Activities.AsyncCodeActivity.CompleteAsyncCodeActivityData.CompleteAsyncCodeActivityWorkItem.Execute(ActivityExecutor executor, BookmarkManager bookmarkManager)

@Harsha_S2,

Refer this thread for a workaround.

Also check this.

Thanks,
Ashok :slight_smile:

@ashokkarale

Both of these threads recommend turning on " Less secure app access" but my account uses 2-Factor Authentication. So Google Advises to use an Application-Specific Password which I have done.

Besides Google is removing the " Less secure app access" option on 30th Sept, 2024.

Thanks,
Harsha