Does Mailkit supports sending anonymous emails? - c#

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.

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.

Can I send SMTP email through Office365 shared mailbox?

We are thinking about moving to O365; however, we developed software that uses our current Exchange server to send email both to external users as well as to a support box when errors occur.
I've been testing this to ensure that the code we have in place will continue to work with O365 but so far, I have not been very successful.
I have tried using .Net's SmtpClient as well as MailKit's SmtpClient and neither one seems to work. I keep getting error (this is the error from MailKit -- the .Net error is similar)
"AuthenticationInvalidCredentials: 5.7.3 Authentication unsuccessful [*.prod.exchangelabs.com]"
I can use the credentials that I have in my code to log into OWA -- so I know the credentials are valid. Is it not possible to send email via O356? Is there any special configuration that has to happen in Exchange to make this possible?
Here is what I've tried so far:
MailKit
var msg = new MimeMessage();
msg.From.Add(new MailboxAddress("Support","support#mydomain.com"));
msg.To.Add(new MailboxAddress("Me","me#mydomain.com"));
msg.To.Add(new MailboxAddress("External User","euser#externaldomain.com"));
msg.Subject = "Test";
msg.Body = new TextPart("plain"){
Text = "Here is a message for you"
};
using(var client = new SmtpClient()){
client.ServerCertificateValidationCallback = (s,c,h,e) => true;
client.AuthenticationMechanisms.Remove("XOAUTH2"); //Not sure what this does. Have tried with and without
client.Connect("smtp.office365.com", 587, MailKit.Security.SecureSocketOptions.StartTls);
client.Authenticate(new NetworkCredential("support#mydomain.com", "supportPwd"));
client.Send(msg);
client.Disconnect(true);
}
The .Net SmtpClient code looked very similar to the MailKit code.
Is there a way to send through O365 with a licensed user? (code above)
Are there any special settings required in Exchange or on the licensed user to make this work? (If the answer to 1 is yes)
Is it possible to send email through a shared mailbox for which the credentialed user has Send As rights?
Update
I'm still getting the same error message. We do have MFA enabled for our domain users. However, we have a policy that does not require MFA for users when they are signing in from a trusted location (our org's IP). I also listed our IP as a Trusted IP. In my mind, MFA shouldn't be the issue here.
I know the credentials are correct. I copied them from the code and pasted them in to the login screen when signing into M365 -- and I got in just fine.
What am I doing wrong?
Yes, you can.
Usersettings:
Server-settings :
https://support.office.com/en-us/article/POP-IMAP-and-SMTP-settings-for-Outlook-com-d088b986-291d-42b8-9564-9c414e2aa040
SMTP server name smtp.office365.com
SMTP port 587
SMTP encryption method STARTTLS
No, you cannot. You need a licenced user to send mail via SMTP.
https://answers.microsoft.com/en-us/msoffice/forum/msoffice_o365admin/set-up-smtp-relay-with-shared-mailbox/d7b98214-9564-432c-b098-525a98c529fb
A customer of ours has a newsletter system set up with TYPO3 and we had to create a new mailbox for this. However, a light one will suffice: instead of a Office 365 Business Premium we only assigned a Office 365 F1 licence.
Edit: also found this: Can Office365 shared mailbox use SMTP?
For anyone who is having similar issues, I found that my problem was a Conditional Access Policy. Microsoft provides a Baseline Policy: Block Legacy Authentication -- which had been turned on in our AAD.
In looking at the Policy, it is designed to BLOCK any authentication mechanisms that don't require MFA. This includes things like POP and SMTP. Once I disabled this policy, the code listed above worked just fine.
For me only disabling "Security defaults" helped.

Using Mailkit : "The SMTP server has unexpectedly disconnected."

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!

c# POP3 client implementation

I'm trying to implement a POP3 client in C#, and the authentication using AUTH PLAIN is killing me. I'm using my account on pop3.live.com for testing, but can't get past beyond the authentication. I've tried connecting via SSL over port 995 and regular 110 (then switching to TLS), but can't manage to get authenticated.
I'm using TcpClient class to establish a connection and get a +OK response from the server. When doing a non-SSL connection I check the capabilities with the command CAPA, and get the response STLS. So I send the command STLS, switch to SslStream, send another CAPA command to check for authentication mechanisms and get that SASL PLAIN is supported (which was not in the regular connection).
Now I don't know what to send with AUTH PLAIN - I've tried sending base64 "account\0account#hotmail.com\0password", "\0account#hotmail.com\0password", "\0account\0password" ... all possible combinations, but either get "Protocol error" when sending the credential in line with AUTH PLAIN, or "Unauthorized ..." when sending the credentials after sending AUTH PLAIN (returns "+ " as expected).
Am I missing something? Can anyone provide a example how to authenticate when using SSL/TLS?
Thanks!
You need to use "\0account#hotmail.com\0password"

Validating TLS email reception

I am trying to convert our current email agent to send email with TLS. We use C# and I just used the following changes.
SmtpClient sclient = new SmtpClient();
sclient.EnableSsl = true;
and a callback method to validate server certificate.
On Testing the mail was sent/received successully, but both I and the receiving end cannot be 100% sure the the email was received encrypted. (I tried to use Fiddler but its not capturing the email)
Based on this http://luxsci.com/blog/how-you-can-tell-if-an-email-was-sent-using-tls-encryption.html, and the header as below
with ESMTP id s7JKErN9002462
(version=TLSv1/SSLv3 cipher=RC4-MD5 bits=128 verify=NO);
Can we safely assume that the mail communication indeed was encrypted? or Should I make any other code changes so that I can be sure that the email is received or it failed? (I think this cannot be certain as it depends on the smtp host) ?
In the end you can always check the TLS connection using network sniffer software such as WireShark.
Of course if you only leave a connection open to the SSL port of the server, and you receive the server certificate, you can be pretty certain the mail did not appear from the blue sky anyway.
You can safely assume that if you are able to connect and send, that the tunnel you're delivering the mail to is secure.
The SmtpClient code is solid and you can trust it. If it fails to connect securely after you've asked it to, it throws an exception, so you'll know something is not like you were expecting.

Categories

Resources