Using Mailkit : "The SMTP server has unexpectedly disconnected." - c#

I am trying to use a free SMTP relay from SendGrid to send emails from my ASP.NET application. I can connect to the server, but when I try to authenticate, I get this error : "The SMTP server has unexpectedly disconnected."
using (var client = new SmtpClient())
{
client.ServerCertificateValidationCallback =
(sender, certificate, certChainType, errors) => true;
client.AuthenticationMechanisms.Remove("XOAUTH2");
// connection
client.Connect("smtp.host", 465, true);
client.Authenticate("UserName", "Password");//error occurs here
client.Send(email);
client.Disconnect(true);
}
Once again, I can connect without any problem, but when I try to authenticate, I get the previously mentionned error...
Any suggestions?
Cheers

You have to supply:
Username: is apikey (as a hard-coded value 'apikey').
Password: is the apikey you generated from the web, which is a big hashy-like string.
You can find this on their docs. But it was hard to find.

I solved my issue changing from SendGrid to gooogle's free SMTP service for all of their users. Simply follow the steps here
and you should be good to go!

Related

SMTP c# MailKit smtp.Authenticate fails

I am trying to send an e-mail using c# MailKit SMTP (code below).
It connects successfully (with Connect method) and then it fails on Authenticate method call with an error " Username and Password not accepted.". I know the user name and password are correct. I also tried "mail.optusnet.com.au" server on port 25 - same story.
What is wrong with my call to smtp.Connect method. I have spent whole day experimenting and got nowhere. Can somebody help please.
// send email
using var smtp = new SmtpClient();
smtp.Connect("smtp.gmail.com", 587, SecureSocketOptions.StartTls);
smtp.Authenticate("rejkid#gmail.com", "Password1");
smtp.Send(email);
smtp.Disconnect(true);
Google no longer allows your password to be used when authenticating via SMTP, POP3 or IMAP.
You either need to use OAuth2 or you need to enable 2FA on your Google account and then generate an app-specific password and use that.

Does Mailkit supports sending anonymous emails?

The Exchange host that I am trying to send emails from does not require authentication. In the past I have successfully achieved this using SmtpClient Class, but as Microsoft is recommending Mailkit, I prefer going for this one.
Does Mailkit always require a username and a password to connect? I have been searching for an answer everywhere but I cannot seem to find one.
This is my code so far:
private void Send(MimeMessage message)
{
using (var client = new SmtpClient(new ProtocolLogger(Console.OpenStandardOutput())))
{
client.Connect(Host, Port, SecureSocketOptions.None);
client.Send(message);
client.Disconnect(true);
}
}
And of course I am getting an error: 5.7.1 client was not authenticated
Thank you loads.
Does Mailkit always require a username and a password to connect? I have been searching for an answer everywhere but I cannot seem to find one.
MailKit does not require authentication - that's up to the server to require it or not.
If a server doesn't require authentication, your code just needs to not call client.Authenticate() (or AuthenticateAsync if you are using async code) just like you attempted to do in your example code snippet.
5.7.1 client was not authenticated is an error that came from your SMTP server when you attempted to send a message and suggests that your SMTP server is configured to require authentication.
None of the exception messages that MailKit throws start with an error code string (5.7.1 is an SMTP error code), so if an exception is thrown starting with an SMTP error code string, then it means it came from the server.

gmail not sending email anymore

I was using a gmail account to send email from an app,
I have set "allow less secure app", and the list on "Recent Security Events" is empty.
Up until today it was working, but today it started saying:
The SMTP server requires a secure connection or the client was not
authenticated. The server response was: 5.5.1 Authentication Required
I can still send email using the gmail website and using thunderbird it still works, but not in the app,
if I change in my code the username and password to a different gmail account it works, but not with this particular one.
this is the code I use to send email:
var client = new SmtpClient
{
Host = serverName,
Port = 587, // TLS Port
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(userName, pass)
};
using (var message = new MailMessage(new MailAddress(userName), new MailAddress(to))
{
Subject = subject,
Body = body
})
{
client.Send(message);
}
UPDATE:
all of a sudden it started to work (on my local, I have unite tests that send email), don't know why, I checked the deployed app it also started to send emails
UPDATE2:
seems to be a more global problem: https://productforums.google.com/forum/?nomobile=true&hl=en&utm_source=google-account&utm_medium=web#!topic/gmail/NETOwtLyWGI
In my searchings, I've found a few things to try:
Try changing password to more complex/secure password. On occasion it seems that Gmail starts to require more complex passwords without letting people know and/or throwing proper errors.
Remove "EnableSsl = true," line from code. (or set to false) (Link)
The fact that your code just stopped working randomly may suggest something along the lines of an expired certificate that is still being presented.
Question: When you say you are able to send email from that account via browser and via Thunderbird, are you sending those emails from the same machine as the C# application attempts to?

Sending an E-Mail via Google Mail

I'm trying to send an E-Mail via Gmail using MailKit. This is my code:
using MailKit.Net.Smtp;
using (var client = new SmtpClient())
{
client.Connect("smtp.gmail.com", 587, false);
//...
}
But no matter what I try I always get the following exception:
System.IO.IOException: The authentication or decryption has failed.
I already tried to enable "unsafe apps" in my Google account, but that did not help. So far I could not find any working example that shows how to send mail via Gmail.
Edit:
I just tried running the Code on WIndows (using .NET), and it worked perfectly. The E-Mail got sent right away. Only when I run it on Linux/OSX using Mono, it does not work.
I just found the answer!
Adding the following line of code to the beginning of my program solved it:
System.Net.ServicePointManager.ServerCertificateValidationCallback +=
(o, certificate, chain, errors) => true;
Now MailKit sends the E-Mails as it should. So does the .NET/Mono SMTP class.

Relay access denied on sending email

I want to send email via my desktop app
Here is my code
MailMessage m = new MailMessage("mahmoud#isysway.com", "gavoh3d#yahoo.com", "subject", "body");
m.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("out.tedata.net.eg");
smtp.Credentials = new System.Net.NetworkCredential("mahmoud#isysway.com", "password");
smtp.UseDefaultCredentials = true;
smtp.Send(m);
My problem is: this code throw an exception
Transaction failed. The server response was: 5.7.1 <gavoh3d#yahoo.com>: Relay access denied
I think because my internet provider need outgoing server (SMTP) authentication
I config it in Outlook as below
Now, How can I config it in my code
Thanks
I found this question because I have the same problem. I solved it using the myNetworks entry in /etc/postfix/main.cf
Add the IP from which your sending to the myNetworks line, I think PostFix expects a space to separate IP adresses, for my eyes it's better to use a comma, but if I use both it works.
This solution is not ideal, so I'll be gratefull for a better one!
Rob
It can be various issues:
are you able to send mail to Yahoo mail from your outlook? check this link If yes
Check IIS SMTP Virtual Server Relay Restrictions
Hope this help you.

Categories

Resources