Secure Server is not receiving what I am sending - c#

I am trying to connect to server via TLS 1 on Windows.
here is how I am connecting to server
public void Connect(string hostName, int port)
{
this.tcpClient.Client.Connect(hostName, port);
RemoteCertificateValidationCallback validationCallback = new RemoteCertificateValidationCallback(ServerValidationCallback);
LocalCertificateSelectionCallback selectionCallback = new LocalCertificateSelectionCallback(ClientCertificateSelectionCallback);
EncryptionPolicy encryptionPolicy = EncryptionPolicy.RequireEncryption;
this.sslStream = new SslStream(this.tcpClient.GetStream(), true, validationCallback, selectionCallback, encryptionPolicy);
//handshake
X509CertificateCollection clientCertificates = GetCertificates();
this.sslStream.AuthenticateAsClient(hostName);
//this.sslStream.AuthenticateAsClient(hostName, clientCertificates, SslProtocols.Tls, true);
}
Than I am sending messages via method
this.sslStream.Write(messageBytes);
this.sslStream.Flush();
this.tcpClient.GetStream().Flush();
Server did not receiving anything from me.

Related

How to listen for incoming connections for sending in c# UDP?

So I am coding a C# UDP server client, which I am wanting for it to listen for incoming connections on port 8001 and then send a message in return. The problem it tries to send the message first and as such sends to to IP address 0.0.0.0 using IPAdress.ANY. How can I recode this to listen for incoming UDP connections first then send message once connection is found? Thanks.
while (!done)
{
string text_to_send = satt.Text;
if (text_to_send.Length == 0)
{
done = true;
}
else
{
//Encode the attack command, and send on the port to the listening computers.
byte[] send_buffer = Encoding.ASCII.GetBytes(text_to_send);
Console.WriteLine("Sending the attack information to the IP: {0} port: {1}",
sending_end_point.Address,
sending_end_point.Port);
const int listenPort = 8001;
UdpClient listener = new UdpClient(listenPort);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
string data_recieved;
byte[] recieve_byte_array;
recieve_byte_array = listener.Receive(ref groupEP);
data_recieved = Encoding.ASCII.GetString(recieve_byte_array, 0, recieve_byte_array.Length);
Console.WriteLine("Number of searches by the listening computers: " + data_recieved);

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Office 365 error

Trying to send an email via MVC 5 C#. This newly created email address is on an office 365 server. Tried numerous solutions online but to no avail I get the following error message: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [LO3P265CA0018.GBRP265.PROD.OUTLOOK.COM]'. My code is as follows:
public void ConcernConfirmEmail(Appointments a, EmailConfig ec)
{
Dictionary<string, string> tokens = new Dictionary<string, string>();
tokens.Add("Name", a.sirenDetail.FirstName);
tokens.Add("Time", a.start.ToString("HH:mm"));
tokens.Add("Date", a.start.ToString("dd/MM/yyyy"));
tokens.Add("Location", a.site.SiteDescription);
using (SmtpClient client = new SmtpClient()
{
Host = "smtp.office365.com",
Port = 587,
UseDefaultCredentials = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(ec.EmailUser, ec.EmailPassword),
TargetName = "STARTTLS/smtp.office365.com",
EnableSsl = true
})
{
MailMessage message = new MailMessage()
{
From = new MailAddress("emailadress#myorg.net"),
Subject = "Subject",
Sender = new MailAddress("emailadress214#myorg.net", "password"),
Body = PopulateTemplate(tokens, GetTemplate("ConfirmTemplate.html")),
IsBodyHtml = true,
BodyEncoding = System.Text.Encoding.UTF8,
SubjectEncoding = System.Text.Encoding.UTF8,
};
message.To.Add(a.sirenDetail.EmailAddress.ToString());
client.Send(message);
}
}
According to oficcial microsoft documentation, SmtpClass is obsolete for a while now, microsoft encourages deveopers to use new open souce Smtp implementations, like MailKit.
When using MailKit with username and password authentication, you have to set the authentication mechanism to use NTLM.
Here is a working example:
public async Task Send(string emailTo, string subject, MimeMessage mimeMessage, MessagePriority messagePriority = MessagePriority.Urgent)
{
MimeMessage mailMessage = mimeMessage;
mailMessage.Subject = subject;
mailMessage.Priority = messagePriority;
if (emailTo.Contains(';'))
{
foreach (var address in emailTo.Split(';'))
{
mailMessage.To.Add(new MailboxAddress("", address));
}
}
else
{
mailMessage.To.Add(new MailboxAddress("", emailTo));
}
mailMessage.From.Add(new MailboxAddress("Sender", _smtpCredentials.SenderAddress));
using var smtpClient = new SmtpClient
{
SslProtocols = SslProtocols.Tls,
CheckCertificateRevocation = false,
ServerCertificateValidationCallback = (s, c, h, e) => true,
};
await smtpClient.ConnectAsync(_smtpCredentials.Server, _smtpCredentials.Port, SecureSocketOptions.StartTlsWhenAvailable);
await smtpClient.AuthenticateAsync(new SaslMechanismNtlm(new NetworkCredential(_smtpCredentials.User, _smtpCredentials.Password)));
try
{
await smtpClient.SendAsync(mailMessage);
}
catch (Exception ex)
{
}
finally
{
if (smtpClient.IsConnected) await smtpClient.DisconnectAsync(true);
}
}
The most important line here is
await smtpClient.AuthenticateAsync(new SaslMechanismNtlm(new NetworkCredential(_smtpCredentials.User, _smtpCredentials.Password)));
That is setting the NTLM as the authentication mechanism for the client to use.
But
If you are unable to change Smtp library right now, you can try change your code to look like this, as imcurrent using in older services and work fine:
using (var smtpClient = new SmtpClient(smtpServer)
{
UseDefaultCredentials = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(user, password),
EnableSsl = false,
Port = 587
})
See that the change is on just on the EnableSsl set to false, and not needded to set the TargetName property.
Hope this helps

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP

I know that this an old question but I can find a solution for my problem, here is the thing , I am trying to send a email via smtp of outlook , but I get this error message: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP, sorry for my english.
public void Send(MailMessage message)
{
msgToSend = message;
string password = string.Empty;
password = CryptoHelper.DecryptData("encoded_password", "key");
var smtpClient = new SmtpClient("smtp.office365.com", 587);
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential("username", "password");
smtpClient.EnableSsl = true;
smtpClient.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
smtpClient.SendCompleted += (s, e) =>
{
smtpClient.Dispose();
message.Dispose();
};
smtpClient.SendAsync(message, null);
}

How to know what UDP port using to send a message

I have a program that sends a message to a server and the server recognizes the message inteprets it and sends back the message to the port it was listening from in the begining. so let's say the client sends from port 5000. The server will read the message from port 5000, and send back a message to the port the client used to send the message so the client can receive it. My question now is that I don't want to fix a port for the server ( i don't want the server to always send back to client to port 1000), so how can i know what port my client used to send the message every single time? So in my client, my UdpClient Listener = new UdpClient(xxx) [(xxxx) being the port number] can be filled by a new value every single time? the value of the port it used to send the message
thanks
here's my program :
private const int sendPort = 9999;
static void Main(string[] args)
{
Boolean done = false;
IPAddress send_to_address = IPAddress.Parse("xx.xxx.xxx.xxx");
IPEndPoint sending_end_point = new IPEndPoint(send_to_address, sendPort);
IPEndPoint receiving_end_point = new IPEndPoint(IPAddress.Any, 0);
UdpClient Listener = new UdpClient(WHAT TO PUT HERE???);
while (!done)
{
Socket sending_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
Console.WriteLine("Enter text to send");
string text_to_send = Console.ReadLine();
// the socket object must have an array of bytes to send.
// this loads the string entered by the user into an array of bytes.
byte[] send_buffer = Encoding.ASCII.GetBytes(text_to_send);
// Remind the user of where this is going.
Console.WriteLine("sending to address: {0} port: {1}", sending_end_point.Address, sending_end_point.Port);
sending_socket.SendTo(send_buffer, sending_end_point);
Console.WriteLine("Message has been sent to the broadcast address");
Console.WriteLine("Now we are waiting for a message back from the Listener");
// here we receive the Message from the Server
Byte[] ByteFromListener = Listener.Receive(ref receiving_end_point);
string datafromreceiver = Encoding.ASCII.GetString(ByteFromListener);
Console.WriteLine(Datafrom receiver)
}
}
}
}
The answer was quite simple. Just needed to use the same socket to receive the message.
Here is the code :
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
class Program
{
private const int sendPort = 9999;
static void Main(string[] args)
{
Boolean done = false;
IPAddress send_to_address = IPAddress.Parse("10.128.105.177");
IPEndPoint sending_end_point = new IPEndPoint(send_to_address, sendPort);
EndPoint receiving_end_point = new IPEndPoint(IPAddress.Any, 0);
while (!done)
{
Socket sending_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
Console.WriteLine("Enter text to send");
string text_to_send = Console.ReadLine();
// the socket object must have an array of bytes to send.
// this loads the string entered by the user into an array of bytes.
byte[] send_buffer = Encoding.ASCII.GetBytes(text_to_send);
// Remind the user of where this is going.
Console.WriteLine("sending to address: {0} port: {1}", sending_end_point.Address, sending_end_point.Port);
sending_socket.SendTo(send_buffer, sending_end_point);
Console.WriteLine("Message has been sent to the broadcast address");
Console.WriteLine("Now we are waiting for a message back from the Listener");
Byte[] ByteFromListener = new byte[8000];
sending_socket.ReceiveFrom(ByteFromListener, ref receiving_end_point);
string datafromreceiver;
datafromreceiver = Encoding.ASCII.GetString(ByteFromListener).TrimEnd('\0');
//Here we print the message from the server
Console.WriteLine(datafromreceiver.ToString());
}
}
}
}

OpenPop.Net - The server did not respond

I am using OpenPop.NET and I am trying to download the emails from the server. But, it is giving the below error message In the meantime, I could configure and download the mails without any issues in Outlook by using the same settings that I did with OpenPop.NET.
The server did not respond with a + response. The response was: "-ERR Command is not valid in this state."
FetchAllMessages(smtpserver, 110, false, emailId, password);
public static List<Message> FetchAllMessages(string hostname, int port, bool useSsl, string username, string password)
{
using (Pop3Client client = new Pop3Client())
{
client.Connect(hostname, port, useSsl);
client.Authenticate(username, password);
int messageCount = client.GetMessageCount();
List<Message> allMessages = new List<Message>(messageCount);
for (int i = messageCount; i > 0; i--)
{
allMessages.Add(client.GetMessage(i));
}
return allMessages;
}
}
throwing exception in client.Authenticate(username, password);

Categories

Resources