I am trying to send an email to MailTrap.io its a free smtp portal for checking the email but im getting the following error.
I am using MailKit to send the email
public async Task SendEmailAsync(string email, string subject, string message)
{
try
{
var emailMessage = new MimeMessage();
emailMessage.From.Add(new MailboxAddress("David", "davidnoreply@gmail.com"));
emailMessage.To.Add(new MailboxAddress("", email));
emailMessage.Subject = subject;
emailMessage.Body = new TextPart("HTML") { Text = message };
using (var client = new SmtpClient())
{
client.LocalDomain = "http://localhost:52101";
await client.ConnectAsync("smtp.mailtrap.io", 584, true);
client.Authenticate("16729fdfa173b4", "0e6de61c67b069");
client.Send(emailMessage);
//await client.emailMessage(emailMessage).ConfigureAwait(false);
//await client.DisconnectAsync(true).ConfigureAwait(false);
}
}
catch (Exception ex)
{
// TODO: handle exception
throw new InvalidOperationException(ex.Message);
}
}
But the following error presents itself I have tried on port 25 but then i get an error about ssl.
No connection could be made because the target machine actively refused it. 127.0.0.1:25
If I change it to port 25 I get the following.
An error occurred while attempting to establish an SSL or TLS connection.
This usually means that the SSL certificate presented by the server is not trusted by the system for one or more of
the following reasons:
1. The server is using a self-signed certificate which cannot be verified.
2. The local system is missing a Root or Intermediate certificate needed to verify the server's certificate.
3. A Certificate Authority CRL server for one or more of the certificates in the chain is temporarily unavailable.
4. The certificate presented by the server is expired or invalid.
Another possibility is that you are trying to connect to a port which does not support SSL/TLS.
It is also possible that the set of SSL/TLS protocols supported by the client and server do not match.
See https://github.com/jstedfast/MailKit/blob/master/FAQ.md#SslHandshakeException for possible solutions.
Edit 2
I removed the ssl but stil the same error above.
public async Task SendEmailAsync(string email, string subject, string message)
{
try
{
var emailMessage = new MimeMessage();
emailMessage.From.Add(new MailboxAddress("David", "david@gmail.com"));
emailMessage.To.Add(new MailboxAddress("", email));
emailMessage.Subject = subject;
emailMessage.Body = new TextPart("HTML") { Text = message };
using (var client = new SmtpClient())
{
await client.ConnectAsync("smtp.mailtrap.io", 25, true);
client.Authenticate("16729fdfa173b4", "0e6de61c67b069");
client.Send(emailMessage);
//await client.emailMessage(emailMessage).ConfigureAwait(false);
//await client.DisconnectAsync(true).ConfigureAwait(false);
}
}
catch (Exception ex)
{
// TODO: handle exception
throw new InvalidOperationException(ex.Message);
}
}