SMTP c# MailKit smtp.Authenticate fails - c#

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.

Related

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.

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.

Email not reaching a specific email domain from php or C#

I am currently using an Office 365 account to send email from various websites. I have implemented an smtp client in both php and C# to send the mail. I use GoDaddy and Rackspace to hosts my sites.
The mail reaches most address without issue, however, I have a business domain through Google, which ends with .co, and emails do not make it to my inbox there.
Here are the settings for the PHPMailer client:
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPSecure = "tls";
$mail->Host = 'smtp.office365.com';
$mail->SMTPAuth = true;
$mail->Username = '<myuser>';
$mail->Password = '<mypassword>';
$mail->Port = 587;
$mail->setFrom('noreply#<mymaildomain>.com', 'MyMail');
The settings send email find to yahoo, gmail, etc. However, when I try to send it to my .co email address, the client indicates the mail was sent, but it never shows up in the inbox.
I am wondering if it is an issue with a domain in Google or if the .co is the problem. Is there any special setting I need to make on the client for .co addresses?
UPDATE
I changed phpmailer to simply use php's mail function. The mail was sent through to my .co domain, but I still have no idea why it will not go through when an authenticated SMTP user sends it. I can go into my Office 365 account and send email to the .co address without issue. I just cannot push it from the server. My host server does not contain the smtp server.

C# SMTP mail sending usually fails due to lack of credentials?

It seems that when you use SmtpClient class and MailMessage to send emails, everything works fine. And it does send it. However, some servers, such as your business Exchange Server or Gmail.com or whatever services, sometimes reject these emails (because they could be phishing sites or spam sites?)
I'm using this and a lot of places reject the email I believe:
SmtpClient smtp = new SmtpClient
{
Host = smtpClient,
Timeout = 40000, // 40 seconds
DeliveryMethod = SmtpDeliveryMethod.Network
};
How do you work around this? Do you have to use login credentials to some recognized / white-listed server so that it is authorized and trusted email?
I could use something like GMAIL authentication, but my server should be allowed to send emails, it shouldn't have to rely on gmail.
Long ago there were many servers on the Internet that provided the service delivery of e-mail without asking too many questions about who was using the service.
Then came the SPAMMERS!.
A server that was sending mail freely around the world was no longer a benefactor of the internet, but a problem.
Nowadays it is increasingly difficult to find a server that sends mail on the Internet without asking for credentials.
Your e-mail server (smtp.yourbusiness.com or whatever you want) does not trust you.
When you ask it to send an email for you, it wants to know who you are.
SmtpClient smtp = new SmtpClient
smtp.Host = "smtp.yourbusiness.com";
NetworkCredential credentials = new NetworkCredential("your_user_name_on_smtpserver", "your_password_on_smtpserver");
smtp.Credentials = credentials;
are you setting the sender mail address and can your mail server be resolved by reverse dns lookup? Some spam filters distrust mail servers they can't reverse lookup. If you for instance make your mail server send mails using a bogus or foreign mail domain, spam filters will probably pick this up and filter your mail. If you have set up a SPF record for your domain and your sending mail server isn't in that record, mails from that server will also often be filtered by spam filters. Another reason could be an IP address from a range known to be dynamically assigned by internet providers. Mail servers sending from these address ranges are mostly spam bots and are therefore often also filtered.
To send GMail you need to specify a number of settings - yes you'll need to use authentication (GMail wouldn't open up their relay to everyone, or they'd get used for spamming).
If I recall correctly you need to send on TCP port 587, enable SSL, host to mail.google.com and provide a username and password on your SmtpClient to get Gmail to actually send it - neglecting one of those usually generates an email back from them telling you what you've forgotten.
Edit: Just to clarify on the username and password bit; you'd need to create a new NetworkCredential with the username and password for your GMail account, and set the Credentials property of your SmtpClient to that.
If i interpret your question correctly: the messages are sent our from your server, but they are trapped in the spamfilters at the receiving end?
If this is the problem, it has nothing to to with the way you are sending the messages but much more with the content of your message and the characteristics of your server. So it does not matter what credentials you use or if you send it with SmtpClient or any other class. Moving to Gmail does not help and Gmail has a limit to the number of messages you can send.
Spam filters like spam assassin use rules. Match too many: your message is considered spam.
Have a look here for an example:
http://spamassassin.apache.org/tests_3_3_x.html
So make sure your message does not get too high a score and it will go trough.

retrieve email using c#?

I want to retrieve emails from my Go Daddy account using C#. I tested OpenPop.NET to do that like so:
OpenPop.Pop3.Pop3Client PopClient = new OpenPop.Pop3.Pop3Client();
PopClient.Connect("pop.secureserver.net", 995, true);
PopClient.Authenticate("username", "pass");
My problem is that the server does not accept the user credentials. I'm sure the credentials are OK.
I have tried the same code with my Gmail account and every thing goes OK. Is something missing that Go Daddy wants me to set?
Is there another a .NET tool like OpenPop.NET that I can use to retrieve emails using the POP3 protocol?
I found out that OpenPop.Pop3.AuthenticationMethod.UsernameAndPassword is the missing part. I have modified the code to be following and it works.
OpenPop.Pop3.Pop3Client PopClient = new OpenPop.Pop3.Pop3Client();
PopClient.Connect("pop.secureserver.net", 995, true);
PopClient.Authenticate("username", "pass", OpenPop.Pop3.AuthenticationMethod.UsernameAndPassword);
Port 995 is Secure POP3 which doesn't have to be supported by your mail provider.
Use 110 for regular POP3.
Its probably because your email account is not configured for Pop by default.
Go to settings in your email account and enable pop.

Categories

Resources