Mailkit SMTP - StartTLS & TLS flags - c#

I am trying to connect to iCloud via SmtpClient
The settings I am using are as follows:
Server name: smtp.mail.me.com
SSL Required: Yes
If you see an error message when using SSL, try using TLS or STARTTLS instead.
Port: 587
SMTP Authentication Required: Yes - with relevant username and password
If I use SSL I get "Handshake failed due to unexpected packet format"
If I don't use SSL visual studio debugger hangs on connect.
I think the problem is I am not telling the SmtpClient to use tls but I cant find documentation on how to do this.
The code is as follows:
using (var client = new SmtpClient()) {
client.Timeout = 1000 * 20;
//client.Capabilities.
client.AuthenticationMechanisms.Remove ("XOAUTH2");
client.Connect("SMTP.mail.me.com", 587, false); //dies here
//client.Connect(servername, port, useSsl);
//can I set tls or starttls here??
client.Authenticate(username, password);
client.Send(FormatOptions.Default, message);
}
Am I able to set TLS or StartTLS manually. One thing I did try is the following but it did not seem to work
client.Connect(new Uri("smtp://" + servername + ":" + port + "/?starttls=true"));
Thanks for any help with this.

The Connect() method that you are using only allows enabling/disabling SSL-wrapped connections which is not the same thing as StartTLS.
Due to the confusion, I've implemented a separate Connect() method that makes this more obvious what is going on:
using (var client = new SmtpClient()) {
// Note: don't set a timeout unless you REALLY know what you are doing.
//client.Timeout = 1000 * 20;
client.Connect ("smtp.mail.me.com", 587, SecureSocketOptions.StartTls);
client.Authenticate (username, password);
client.Send (message);
}
Try that.

You can set your options to "SecureSocketOptions.Auto"
something like this
await client.ConnectAsync(mailService.Host, mailService.Port, SecureSocketOptions.Auto);
MailKit will automatically decide to use SSL or TLS.

According to MailKit Doc
Connect(string host,int port,bool useSsl,CancellationToken cancellationToken = null)
The useSsl argument only controls whether or not the client makes an SSL-wrapped connection. In other words, even if the useSsl parameter is false, SSL/TLS may still be used if the mail server supports the STARTTLS extension.

This worked for me over the SecureSocketOptions.Auto option mentioned previously:
client.Connect(host, port, SecureSocketOptions.None);

Related

how make invisible the password with Mailkit in Authenticate if the SMTP server requires authentication?

I understand that SmtpClient doesn't support many modern protocols so the recommendation is use MailKit, but with SmtpClient using System.Net.Mail the password was not required. With Mailkit I must Authanticate with password in my code.
using (var client = new SmtpClient ()) {
client.Connect ("smtp.friends.com", 587, false);
// Note: only needed if the SMTP server requires authentication
client.Authenticate ("joey", "password");
client.Send (message);
client.Disconnect (true);
}
Could I don't use the password visible in my code, please ? How ?
Thank you
You could load the password from an XML (or JSON or any other format) config file.
It's also possible that your SMTP server doesn't require authentication, so you should at least check that.
this is my way of doing this:
Environment.GetEnvironmentVariable("Password")
You need to go to Environment variables in Windows settings:
Now:
Nazwa zmiennej = Environment variable (Password)
Wartość zmiennej = value (your password)
Remember to reload Visual Studio after adding Environment variable because VS load it at the start.
Your code with my method will look like this:
using (var client = new SmtpClient ()) {
client.Connect ("smtp.friends.com", 587, false);
// Note: only needed if the SMTP server requires authentication
client.Authenticate ("joey", Environment.GetEnvironmentVariable("Password"));
client.Send (message);
client.Disconnect (true);
}

MailKit: The handshake failed due to an unexpected packet format

I got exception when I try to connect to my SMTP server using MailKit SmtpClient. BUT my mails have been sent successfully if I use System.Net.Mail.SmtpClient with the same parameters!
The exception message: An error occurred while attempting to establish an SSL or TLS connection.
The inner exception message: The handshake failed due to an unexpected packet format.
Questions
Why does MailKit.Net.Smtp.SmtpClient throw exception but System.Net.Mail.SmtpClient doesn't? What is the difference between them?
How to fix it?
Code
Initialize the parameters required for mail sending:
var host = "myhost.com";
var port = 2525;
var from = "from#mydomain.com";
var to = "to#mydomain.com";
var username = "from#mydomain.com";
var password = "myPassword";
var enableSsl = true;
Sending mail using System.Net.Mail.SmtpClient:
var client = new System.Net.Mail.SmtpClient
{
Host = host,
Port = port,
EnableSsl = enableSsl,
Credentials = new NetworkCredential(username, password)
};
client.Send(from, to, "subject", "body"); // success.
But when I try to connect to the host using MailKit with the same host and port, I got the exception:
var mailKitClient = new MailKit.Net.Smtp.SmtpClient();
mailKitClient.Connect(host, port, enableSsl); // it throws the exception.
The problem is that you are connecting to a plain-text port and expecting SSL.
In MailKit, the true/false useSsl parameter is used to decide whether or not to connect in SSL mode or plain-text mode.
In System.Net.Mail, they don't support connecting in SSL mode, they only support upgrading a plain-text connection to SSL mode using the STARTTLS command once the connection has been established.
To overcome this, MailKit has a different Connect() method that takes an enum value SecureSocketOptions.
What you want is SecureSocketOptions.StartTls:
var mailKitClient = new MailKit.Net.Smtp.SmtpClient();
mailKitClient.Connect(host, port, SecureSOcketOptions.StartTls);

OpenPop.Pop3 error

I'm trying to connect to a exchange server using OpenPop library and when I try to connect it says "Server is not available", this is my code:
Pop3Client Client = new Pop3Client();
Client.Connect("srv", 25, false);
Client.Authenticate("usr", "pass");
Can you show my what did I do wrong?
Thanks
If you authenticate with username and password you need to pass AuthenticationMethod.UsernameAndPassword to Authenticate:
using(var client = New OpenPop.Pop3.Pop3Client())
{
// Connect to the server
client.Connect(Pop3Server, Pop3Port, false);
// Authenticate towards the server
client.Authenticate("usr", "pass", OpenPop.Pop3.AuthenticationMethod.UsernameAndPassword);
// Get the number of messages in the inbox
int messageCount = client.GetMessageCount();
}
Note that I've used using-statement which is always recommend when an onject implements IDisposable.
Is "srv" a valid hostname? Try to ping the hostname in commandline.
And port 25 is usually the port for sending e-mails.
I would recommend verifying the port also.
The port is wrong.
Kindly use the following ports for POP3: port 110 or port 995 for Secure SSL.
Also kindly check that 'srv' is a valid hostname which I think is wrong as well.

C# send email using implicit ssl

Is there any library preferable free that can be used in order to sens email using implicit ssl protocol. My hosting provider support ssl emails ... but standard .net email client cannot handle that.
Use the TLS port (ie 587) rather than the SSL port. I had the same issue for months until I found this solution.
Sending email in .NET through Gmail
System.Net.Mail does support "explicit SSL" (also known as "StartTLS" - usually on port 25 or 587), but not "implicit SSL" (aka "SMTPS" - usually on port 465).
As far as I know, explicit SSL starts from an unsecured connection, then the STARTTLS command is given and finally a SSL secured connection is made.
Implicit SSL, on the other side, requires that the SSL connection is set up before the two parties start talking.
Some servers (like gmail) accept both, so you simply need to set EnableSsl to true and send to the right port. If your server does not support explict SSL, though, this "simple way" is not an option.
I'm also still looking around for a general solution for using System.Net.Mail with implicit SSL, with no luck so far.
Anyway take a look at this article, it may give you some insight.
[edit: #Nikita is right, fixed port numbers to avoid confusion]
You may still be able to use the deprecated System.Web.Mail.MailMessage API (and set its "http://schemas.microsoft.com/cdo/configuration/smtpusessl" option, for explicit SSL/TLS):
System.Web.Mail.MailMessage mailMsg = new System.Web.Mail.MailMessage();
// ...
mailMsg.Fields.Add
("http://schemas.microsoft.com/cdo/configuration/smtpusessl",
true);
Alternatively, if you can, you could run something like stunnel locally to establish an SSL/TLS tunnel from your localhost to your SMTP server. Then, you would have to connect normally (without SSL/TLS) to the tunnel's localhost end as your SMTP server.
Try to check AIM (Aegis Implicit Mail) on https://sourceforge.net/p/netimplicitssl/wiki/Home/
As one of options, our SecureBlackbox includes SMTP component which works via both implicit and explicit SSL and supports different authentication mechanisms (including SASL, NTLM etc).
AspNetEmail supports Explicit SSL and Implicit ssl
http://www.advancedintellect.com/download.aspx
You can use AIM (Aegis Implicit Mail) to send email through implicit SSL:
First install the package: Install-Package AIM
Then Use the sample code to send email
class Mail
{
private static string mailAddress = "{you email address}";
private static string host = "{your host server}";
private static string userName = "{your user name}";
private static string password = "{your password}";
private static string userTo = "{to address}";
private static void SendEmail(string subject, string message)
{
//Generate Message
var mailMessage = new MimeMailMessage();
mailMessage.From = new MimeMailAddress(mailAddress);
mailMessage.To.Add(userTo);
mailMessage.Subject = subject;
mailMessage.Body = message;
//Create Smtp Client
var mailer = new MimeMailer(host, 465);
mailer.User = userName;
mailer.Password = password;
mailer.SslType = SslMode.Ssl;
mailer.AuthenticationMode = AuthenticationType.Base64;
//Set a delegate function for call back
mailer.SendCompleted += compEvent;
mailer.SendMailAsync(mailMessage);
}
//Call back function
private static void compEvent(object sender, AsyncCompletedEventArgs e)
{
if (e.UserState != null)
Console.Out.WriteLine(e.UserState.ToString());
Console.Out.WriteLine("is it canceled? " + e.Cancelled);
if (e.Error != null)
Console.Out.WriteLine("Error : " + e.Error.Message);
}
}

Troubleshooting "The server committed a protocol violation" when sending mail with SmtpClient

I want to send a mail message with the SmtpClient class.
Here's the code I use:
SmtpClient smtpClient = new SmtpClient("Host",25);
NetworkCredential basicCredential =
new NetworkCredential("UserName", "Password");
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress("me#domain.com");
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = basicCredential;
message.From = fromAddress;
message.Subject = "test send";
message.IsBodyHtml = true;
message.Body = "<h1>hello</h1>";
message.To.Add("mail#domain.com");
smtpClient.Send(message);
But it always throws an exception:
The server committed a protocol violation The server response was: UGFzc3dvcmQ6
I can't find the reason for that. Please, if anyone has faced something like this, tell me what to do.
I had the same problem, for my case it was for setting user#domain instead of user, I mean
Old code
new NetworkCredential("UserName#domain.com", "Password");
New code
new NetworkCredential("UserName", "Password");
This looks to me like SmtpClient authentication is somehow getting out of step.
Some authentication mechanisms are "Client: request auth with username and password, Server: success/fail" others are "Client: request auth with username, Server: request password, Client: reply with password, Server: success/fail".
It looks like SmtpClient is expecting the former, while your server is expecting the latter.
As dave wenta suggested, a log of a session would tell you what auth mechanism SmtpClient is trying to use, but it will also say what auth mechanisms the server supports.
What normally happens is that the server offers a number of authetication options, and the client choses which one it is going to use. The behaviour from there should be determined by the protocol chosen. I would hope that the SmtpClient class took care of that for you though, but I'm afraid I've never used that particular class.
Also remember - If you are going to post a log here, change to a throwaway password before you log the session, as a base64 encoded plain text password can be trivially changed back to human readable plain text password.
UGFzc3dvcmQ6 is "Password:" base 64 encoded (without quotes), meaning the password is probably wrong or not send encoded. Try base 64 encoding the password:
string password = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("Password));
Enable logging for System.Net.Mail. Then view the log file. This will show you exactly what is happening during the SMTP layer.
Here is a link with more info:
http://systemnetmail.com/faq/4.10.aspx
thousand of hit on google for UGFzc3dvcmQ6
it seem the server expect encrypted(with base64) username/password
Had the same problem. Solved it in the following steps:
1) find out, what the server offers for SMTP Authentication by connecting to the SMTP Server using Telnet or putty or any other terminal:
telnet xxx.yyy.zzz.aaa 587
(xxx.yyy.zzz.aaa = IP address of the SMTP Server, 587 = port number)
< Server answers with "220 protocol + version + time"
ehlo testing
< Server displays list of capabilities e.g.
250-AUTH NTLM CRAM-MD5 LOGIN
The SMTP client tries to take the most secure protocol first. In my case:
1. System.Net.Mail.SmtpNegotiateAuthenticationModule
2. System.Net.Mail.SmtpNtlmAuthenticationModule
3. System.Net.Mail.SmtpDigestAuthenticationModule
4. System.Net.Mail.SmtpLoginAuthenticationModule
It looks as if the SMTP client tries NTLM while the server tries to run LOGIN.
With a hack (cf. https://blogs.msdn.microsoft.com/knom/2008/04/16/hacking-system-net-mail-smtpclient/), all protocols can be turned of except for the one the server assumes (LOGIN in this case):
FieldInfo transport = smtpClient.GetType().GetField("transport", BindingFlags.NonPublic | BindingFlags.Instance);
FieldInfo authModules = transport.GetValue(smtpClient).GetType().GetField("authenticationModules",BindingFlags.NonPublic | BindingFlags.Instance);
Array modulesArray = authModules.GetValue(transport.GetValue(smtpClient)) as Array;
foreach (var module in modulesArray)
{
Console.WriteLine(module.ToString());
}
// System.Net.Mail.SmtpNegotiateAuthenticationModule
// System.Net.Mail.SmtpNtlmAuthenticationModule
// System.Net.Mail.SmtpDigestAuthenticationModule
// System.Net.Mail.SmtpLoginAuthenticationModule
// overwrite the protocols that you don't want
modulesArray.SetValue(modulesArray.GetValue(3), 0);
modulesArray.SetValue(modulesArray.GetValue(3), 1);
modulesArray.SetValue(modulesArray.GetValue(3), 2);
This can also happen when you simply don't provide the password. In my case I was using credentials from a web.config smtp block and on my deployment server (using octopus deploy) had forgotten to populate the password attribute.
For anyone else coming across this on google.. I fixed this by supplying my username in the "username" format instead of "DOMAIN\username"
$Credential = Get-Credential
Send-MailMessage -SmtpServer exchange.contoso.com -Credential $Credential -From 'user#contoso.com' -To 'recipient#domain.com' -Subject 'Test email' -Port 587 -UseSsl

Categories