MailKit not connecting in Unity project

126 Views Asked by At

I am using https://github.com/jstedfast/MailKit for my Unity game project I could import all needed packages and dependencies to my project successfully. I did this for other NuGet packages such as AWSSDK for .NET and I am sure this could not be a problem. enter image description here

This is the tutorial I followed https://blog.christian-schou.dk/send-emails-with-asp-net-core-with-mailkit/

public async Task<bool> SendAsync()
{
    try
    {
        // Initialize a new instance of the MimeKit.MimeMessage class
        var mail = new MimeMessage();

        mail.From.Add(new MailboxAddress("Sender Name", "[email protected]"));
        mail.To.Add(new MailboxAddress("Recipient Name", "[email protected]"));
        mail.Subject = "Hello from Mailkit!";
        mail.Body = new TextPart("plain")
        {
            Text = "This is the body of the email."
        };

        using var smtp = new SmtpClient();

        {
            await smtp.ConnectAsync("smtp.gmail.com", 465, SecureSocketOptions.SslOnConnect);
            //await smtp.ConnectAsync("smtp.gmail.com", 587, SecureSocketOptions.StartTls);
        }
        await smtp.AuthenticateAsync("[email protected]", "app-password-from-google-account");
        await smtp.SendAsync(mail);
        await smtp.DisconnectAsync(true);

        return true;

    }
    catch (Exception ex)
    {
        Debug.Log(ex.Message);
        return false;
    }
}

But tried this again and again and I got this error continuously

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.

UnityEngine.Debug:Log (object)
MailkitSmtpTester/<SendAsync>d__3:MoveNext () (at 
Assets/Scenes/MailkitSmtpTester.cs:86)
UnityEngine.UnitySynchronizationContext:ExecuteTasks ()`

I tried to do some trouble shooting

  • Check your internet connection: Ensure that you have a stable internet connection. Try accessing other websites or services to verify if your connection is working properly.
  • Verify the SMTP server settings: Double-check the SMTP server settings you are using in your Mailkit code. Make sure you have the correct server address, port number, and authentication credentials.
  • Check firewall and antivirus settings: Sometimes, firewalls or antivirus software can block outgoing connections. Temporarily disable any firewall or antivirus software and test if the issue persists. If the error disappears, you may need to configure your security software to allow the Mailkit application to establish a connection.
  • Test with a different network: If possible, try running your code on a different network or internet connection. This will help determine if the issue is specific to your current network configuration.
  • Verify server availability: Ensure that the SMTP server you are trying to connect to is up and running. You can check the server status by pinging the server or contacting your email service provider.
  • Check for any IP blocking: It's possible that your IP address has been blocked by the SMTP server due to suspicious activity. Contact your email service provider to check if your IP address is blocked and request removal if necessary.

So I turned off firewall and allow all connection from 465 and 587 port in inbound and outbound of my VPS But still got the same error What did I do wrong?

0

There are 0 best solutions below