send mail using MailKit without specifying port number - asp.net-core

Using asp.net core 3.1
I can send email using this code:
Code snippet 1 using System.Net.Mail.SmtpClient
using (System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient())
{
smtp.Host = "mail.my-real-domain-name.com";
smtp.EnableSsl = false;
NetworkCredential credential = new NetworkCredential(emailModel.From,
emailModel.Password);
smtp.UseDefaultCredentials = false;
smtp.Credentials = credential;
smtp.Send(mm);
}
but when I try to send email using MailKit it doesn't connect to mailserver
Code snippet 2 in MailKit documentation
using (var client = new MailKit.Net.Smtp.SmtpClient())
{
client.Connect("mail.my-real-domain-name.com",0,false);
client.Authenticate(emailModel.From, emailModel.Password);
client.Send(message);
client.Disconnect(true);
}
What's the equal of Code snippet 1 using MailKit?
None of below lines worked so I want to know how to send email without specifying port number using MailKit.
It doesn't throw any error, just doesn't connect, I mean it stays in client.Connect line.
// client.Connect("mail.my-real-domain-name.com", 465);
// client.Connect("mail.my-real-domain-name.com", 465, true);
// client.Connect("mail.my-real-domain-name.com", 25, false);
// client.Connect("mail.my-real-domain-name.com", 2525, false);
// client.Connect("mail.my-real-domain-name.com", 587,MailKit.Security.SecureSocketOptions.StartTls);
MailKit

If you take a look at the SmtpClient.cs reference source, in the Initialize() method:
if port == 0
if there is a MailConfiguration XML file available, it uses the specified port from the XML (which defaults to port 25): https://referencesource.microsoft.com/#System/net/System/Net/Configuration/SmtpNetworkElement.cs,e4c99455ef3ea37c
else if there is no XML config file, it uses the static defaultPort member, which is set to 25.
So to get the same behavior as System.Net.Mail, use port 25.
MailKit will also accept port = 0 in the Connect() call and, depending on what the SecureSocketOptions argument is, it will choose an appropriate default port in the ComputeDefaultValues() method: https://github.com/jstedfast/MailKit/blob/master/MailKit/Net/Smtp/SmtpClient.cs#L1253
If the port == 0, then if useSsl=true or socketOptions=SslOnConnect, it will choose port 465, otherwise it will choose port 25.

Related

Asp.net core website intermittently refusing connections

I have an asp.net core 3.0 website. It has a controller that implements an HttpGet function that does some database stuff then returns a Json object (https://localhost:44356/api/runner/match).
I have a console application that uses an HttpClient to hit that url. I launch the site locally and then I launch my console app. About 50% of the time it works. The other 50% of the time I get:
HttpRequestException: No connection could be made because the target machine actively refused it.
SocketException: No connection could be made because the target machine actively refused it.
I'm trying to figure out why my console app's connection is being blocked. I don't know how to start debugging this. I tried to implement a retry on the request, but once I get the exception, I keep getting it. So I think it's something non-deterministic happening in my website, potentially related to SSL?
I'm able to hit the url in Chrome locally just fine.
How do I figure out what is blocking the connection from being made?
Is there any chance this is something IIS Express is doing?
Calling code in console app:
static async Task<List<Deck>> GetMatchData()
{
string baseUrl = "https://localhost:44356/api/runner/match";
using (HttpClient client = new HttpClient())
{
HttpResponseMessage res = null;
res = await client.GetAsync(baseUrl);
Controller function:
[HttpGet("match")]
public async Task<ActionResult> GetMatchup()
{
int count = db.Decks.Count();
Random r = new Random();
int d1 = r.Next(count) + 1; // database ids start at 1 for some reason
int d2 = r.Next(count - 1) + 1;
if (d1 == d2)
d2++;
List<Deck> result = new List<Deck>();
result.Add(await db.Decks.FindAsync(d1));
result.Add(await db.Decks.FindAsync(d2));
if (result[0] == null || result[1] == null)
{
return BadRequest();
}
return Ok(result);
}
try
HttpClient httpClient = new HttpClient();
//specify to use TLS 1.2 as default connection
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
string baseUrl = "https://localhost:44356/api/runner/match";
httpClient.BaseAddress = new Uri(baseUrl );
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var getResult = await httpClient.GetAsync(baseUrl);

Send Microsoft.Azure.ServiceBus Message to BizTalk 2013 WCF-Custom

I need to send messages from a .NET Core app via the Azure Service Bus to BizTalk 2013. I have configured a WCF Custom receive port on BizTalk but on receiving a message get the following error:
The adapter "WCF-Custom" raised an error message. Details "System.Xml.XmlException: The input source is not correctly formatted.
I've found examples using Windows.Azure.ServiceBus package and BrokeredMessage, but this is deprecated. I need to use Microsoft.Azure.ServiceBus and the Message object.
I've tried many ways of serializing the XML but nothing seems to work.
In short I'm creating the message like this:
var message = new Message(Encoding.UTF8.GetBytes("<message>Hello world</message>"));
Is there a way to serialize the message correctly to be received by WCF in BizTalk 2013?
I figured it out.
For anyone who needs to send messages via Azure Service Bus using Microsoft.Azure.ServiceBus Message to BizTalk 2013 WCF-Custom receive port.
var toAddress = "sb://yourbusname.servicebus.windows.net/yourqueuename";
var bodyXml = SerializeToString(yourSerializableObject); //
var soapXmlString = string.Format(#"<s:Envelope xmlns:s=""http://www.w3.org/2003/05/soap-envelope"" xmlns:a=""http://www.w3.org/2005/08/addressing""><s:Header><a:Action s:mustUnderstand=""1"">*</a:Action><a:To s:mustUnderstand=""1"">{0}</a:To></s:Header><s:Body>{1}</s:Body></s:Envelope>",
toAddress, bodyXml);
var content = Encoding.UTF8.GetBytes(soapXmlString);
var message = new Message { Body = content };
message.ContentType = "application/soap+msbin1";
This wraps the Xml in a proper SOAP format. Note the "to" embedded in the SOAP envelope is necessary (I found it didn't work using message.To).
For completeness, this is the serialization method (for clean xml):
public string SerializeToString<T>(T value)
{
var emptyNamespaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
var serializer = new XmlSerializer(value.GetType());
var settings = new XmlWriterSettings
{
Indent = false,
OmitXmlDeclaration = true
};
using (var stream = new StringWriter())
using (var writer = XmlWriter.Create(stream, settings))
{
serializer.Serialize(writer, value, emptyNamespaces);
return stream.ToString();
}
}

What uri pattern do I need to communicate with my PC from my handheld device?

As I was reminded here, I need to probably use "ppp_peer" to programmatically connect from my Compact Framework app to my Web API app running on my PC.
I have tried this (replacing an IPAddress with "ppp_peer"):
string uri = string.Format("http://ppp_peer:28642/api/FileTransfer/GetHHSetupUpdate?serialNum={0}&clientVersion={1}", serNum, clientVer);
...but I get, "NullReferenceException" in "Main" (prior to this I got "Unable to Connect to the Remote Server").
I have a breakpoint in the server code, but it doesn't reach that, so it must be somewhere in the client where this is occurring.
The client code in context is:
string uri = string.Format("http://ppp_peer:28642/api/FileTransfer/GetHHSetupUpdate?serialNum={0}&clientVersion={1}", serNum, clientVer);
RESTfulMethods.DownloadNewerVersionOfHHSetup(uri);
. . .
public static void DownloadNewerVersionOfHHSetup(string uri)
{
string dateElements = DateTime.Now.ToString("yyyyMMddHHmmssfff", CultureInfo.InvariantCulture);
var outputFileName = string.Format("HHSetup_{0}.exe", dateElements);
try
{
var webRequest = (HttpWebRequest)WebRequest.Create(uri);
var webResponse = (HttpWebResponse)webRequest.GetResponse();
string statusCode = webResponse.StatusCode.ToString();
if (statusCode == "NoContent")
{
MessageBox.Show("You already have the newest available version.");
}
else
{
var responseStream = webResponse.GetResponseStream();
using (Stream file = File.Create(outputFileName))
{
CopyStream(responseStream, file);
MessageBox.Show(string.Format("New version downloaded to {0}", outputFileName));
}
}
}
catch (WebException webex)
{
string msg = webex.Message;
string innerEx = webex.InnerException.ToString();
string status = webex.Status.ToString();
MessageBox.Show(string.Format("Message: {0}; Status: {1}; inner Ex: {2}", msg, status, innerEx));
}
}
Do I need an IPAddress in addition to ppp_peer, or is my formatting of the URI wrong, or...???
UPDATE
After the "NRE" I also see, ""...encountered a serious error and must shut down"
I changed the code from above to see just what ppp_peer is translated as:
IPAddress ipAd = Dns.Resolve("PPP_PEER").AddressList[0];
string IPAddr = ipAd.ToString();
MessageBox.Show(IPAddr);
string uri = string.Format("http://{0}:28642/api/FileTransfer/GetHHSetupUpdate?serialNum={1}&clientVersion={2}", IPAddr, serNum, clientVer);
The MessageBox call shows me "192.168.55.100" which is different from what I thought my PC's IPAddress was...???
I get the same with:
IPAddress ipAd = Dns.GetHostEntry("PPP_PEER").AddressList[0];
UPDATE 2
Using this instead (I got it from here [Get ip address of host pc from windows mobile when connected via ActiveSync):
IPAddress ipAd = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0];
...the IP Address displayed is "one up" (192.168.55.101), and instead of an NRE, I get:
Message: Unable to connect to the remote server; Status: ConnectFailure; inner Ex: System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it at System.Net.Sockets.SocketConnectNoCheck(EndPoint remoteEP) ...
So it seems I'm doing all I can on the client end, and the server hears the knock, but is not opening the door - am I right?
BTW, out of curiosity I also added this code:
string hostName = Dns.GetHostName();
MessageBox.Show(string.Format("host name is {0}", hostName));
...and I see "WindowsCE"
UPDATE 3
According to this post by Andy Wiggly (the cat/bloke who wrote "MS .NET Compact Framework"), you do use "ppp_peer":
HttpWebRequest request = REST.CreateRequest(#"http://ppp_peer/DataServicesWebsite/NorthwindService.svc/Customers",
HttpMethods.GET, String.Empty, #"application/atom+xml", "", "");
The interestingest thing about this is the lack of a port assignment (":28642" or whatever); however, this style also gives me an NRE (yes, kind of like a Null Ready to Eat).
UPDATE 4
So what uri will it take to access the host machine from the handheld device?
I have tried all of the following permutations from the client/Compact Framework app, and none work:
IPAddress ipAd = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0];
string IPAddr = ipAd.ToString();
//string uri = string.Format("http://ppp_peer/api/...
//string uri = string.Format("http://ppp_peer:28642/api...
//string uri = string.Format("http://PPP_PEER/api/...
string uri = string.Format("http://PPP_PEER:28642/api/...
//string uri = string.Format("http://{0}:28642/api/...
//string uri = string.Format("http://192.168.125.50:28642/api/...
//string uri = string.Format("http://Platypus:28642/api/...
RESTfulMethods.DownloadNewerVersionOfHHSetup(uri);
The error is happening somewhere in that client code (can't step through it, so I don't know exactly where), because I have a breakpoint on the last line shown, and it is never reached.
SERVER (Web API) code:
[Route("api/FileTransfer/GetUpdatedHHSetup")]
public HttpResponseMessage GetUpdate(string serialNum, string clientVersion)
{
return _fileTransfer.GetHHSetupUpdate(serialNum, clientVersion);
}
public HttpResponseMessage GetHHSetupUpdate(string serialNum, string clientVersion)
{
HttpResponseMessage result;
string filePath = GetAvailableUpdateForCustomer(serialNum); // <= breakpoint on this
line
I put some debug lines in DownloadNewerVersionOfHHSetup() so that it now looks like this:
public static void DownloadNewerVersionOfHHSetup(string uri)
{
MessageBox.Show("Made it into DownloadNewerVersionOfHHSetup");
string dateElements = DateTime.Now.ToString("yyyyMMddHHmmssfff",
CultureInfo.InvariantCulture);
var outputFileName = string.Format("HHSetup_{0}.exe", dateElements);
try
{
var webRequest = (HttpWebRequest)WebRequest.Create(uri);
MessageBox.Show("Made it into DownloadNewerVersionOfHHSetup #2");
var webResponse = (HttpWebResponse)webRequest.GetResponse();
MessageBox.Show("Made it into DownloadNewerVersionOfHHSetup #3");
. . .
I never see "#3", so it must be a problem inside the call to GetResponse(), but how can I find out exactly what? I get the NRE, then "...encountered a serious error and must shut down"
This is where it tries to call the server but, as mentioned, it never makes it to the server method being called...
UPDATE 5
It turns out that this now works:
http://192.168.125.50:28642/api/
...and the main reason that it does is because there was a mismatch between my routing attribute (GetUpdatedHHSetup) and what I was calling from the client (GetHHSetupUpdate). Once I aligned those planets, the NRE went away, and I got the expected result.
PPP_PEER is not needed in the uri/connection string. Using the host PC's IP Address (and port number for the server/Web API app) works now (after fixing the mismatch between the routing attribute and what the client was calling it).
I reckon using the machine name would work just as well, too.

JavaMail, Exchange Server and “no login methods supported”

I'm trying to read contents of a mailbox, using JavaMail and IMAP.
No SSL, only plain IMAP.
My code is like this:
// Connection default properties
Properties props = new Properties();
props.setProperty("mail.imap.timeout", "5000");
props.setProperty("mail.imap.connectiontimeout", "5000");
props.setProperty("mail.pop3.timeout", "5000");
props.setProperty("mail.pop3.connectiontimeout", "5000");
props.setProperty("mail.smtp.timeout", "5000");
props.setProperty("mail.smtp.connectiontimeout", "5000");
// Get session
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
// Get the store
Store store = session.getStore(account.getProtocol()); // returns "imap"
String username = account.getUsername();
String password = account.getPassword();
String host = account.getHost();
store.connect(host, username, password);
// Get folder
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
Message messages[] = folder.search(ft);
System.out.println("Ci sono " + messages.length + " messaggi da leggere");
Here is what i get:
https://www.dropbox.com/s/zbqh7gt3xqgobo7/imap_error.png
It seems that Exchange server is rejecting my login trials...i'm stuck with this and can't understand how to proceed further.
Anyone can help?
The server is configured not to allow 'LOGIN' on plaintext ports.
Note the CAPABILITY response: LOGINDISABLED, and no alternative AUTH methods are presented. You will likely need to connect with SSL encryption.
On the other hand, I don't know why they'd keep open the plaintext port if it can't be used....

Set callback for System.DirectoryServices.DirectoryEntry to handle self-signed SSL certificate?

I have an application replicating data from a directory service using typical System.DirectoryServices.DirectoryEntry code. I now have a requirement to replicate from Novell eDirectory using SSL with a self-signed certificate. I suspect that the existing code would work with a valid certificate that could be verified, or perhaps if the self-signed cert is added to the local machine keystore. In order to make it work for sure with a self-signed cert however, the only solution I can find is to use the System.DirectoryServices.Protocols namespace and the LdapConnection class, whereby I can wire up a VerifyServerCertificate callback. I can't find any way of applying the same concept to a DirectoryEntry instance, or of connecting with an LdapConnection instance and somehow "converting" that to a DirectoryEntry instance. Maybe it isn't possible, I'd just like to confirm that really. Any other thoughts welcome.
The only pertinent link I've found is at: http://www.codeproject.com/Articles/19097/eDirectory-Authentication-using-LdapConnection-and
This is a phenomenal question.
I've been battling this same issue for a few days now, and I've finally got some definitive proof on why the DirectoryEntry object will not work in this scenario.
This particular Ldap server (running on LDAPS 636) also issues it's own self signed certificate. Using LdapConnection (and monitoring the traffic via Wireshark), I noticed a handshake taking place that does not occur when using DirectoryEntry :
The first sequence is the from the secured ldap server, the second sequence is from my machine. The code that prompts the second sequence is :
ldapConnection.SessionOptions.VerifyServerCertificate += delegate { return true; };
There are others way to "fake out" the callback, but this what I've been using.
Unfortunately, DirectoryEntry does not have an option or method to verify a self signed cert, thus the acceptance of the certificate never happens (second sequence), and the connection fails to initialize.
The only feasible way to accomplish this is by using LdapConnection, in conjunction with a SearchRequest and SearchResponse. This is what I've got so far :
LdapConnection ldapConnection = new LdapConnection("xxx.xxx.xxx:636");
var networkCredential = new NetworkCredential("Hey", "There", "Guy");
ldapConnection.SessionOptions.SecureSocketLayer = true;
ldapConnection.SessionOptions.VerifyServerCertificate += delegate { return true; };
ldapConnection.AuthType = AuthType.Negotiate;
ldapConnection.Bind(networkCredential);
SearchRequest request = new SearchRequest("DC=xxx,DC=xxx,DC=xxx", "(sAMAccountName=3074861)", SearchScope.Subtree);
SearchResponse response = (SearchResponse)ldapConnection.SendRequest(request);
if(response.Entries.Count == 1)
{SearchResultEntry entry = response.Entries[0];
string DN = entry.DistinguishedName;}
From there you can gather AD Properties from the SearchResponse, and process accordingly. This is a total bummer though, because the SearchRequest seems to be much slower then using the DirectoryEntry.
Hope this helps!
I promise, this will be my last post on this particular question. :)
After another week of research and development, I have a nice solution to this, and it has worked exceedingly well for me thus far.
The approach is somewhat different then my first answer, but in general, it's the same concept; using the LdapConnection to force validation of the certificate.
//I set my Domain, Filter, and Root-AutoDiscovery variables from the config file
string Domain = config.LdapAuth.LdapDomain;
string Filter = config.LdapAuth.LdapFilter;
bool AutoRootDiscovery = Convert.ToBoolean(config.LdapAuth.LdapAutoRootDiscovery);
//I start off by defining a string array for the attributes I want
//to retrieve for the user, this is also defined in a config file.
string[] AttributeList = config.LdapAuth.LdapPropertyList.Split('|');
//Delcare your Network Credential with Username, Password, and the Domain
var credentials = new NetworkCredential(Username, Password, Domain);
//Here I create my directory identifier and connection, since I'm working
//with a host address, I set the 3rd parameter (IsFQDNS) to false
var ldapidentifier = new LdapDirectoryIdentifier(ServerName, Port, false, false);
var ldapconn = new LdapConnection(ldapidentifier, credentials);
//This is still very important if the server has a self signed cert, a certificate
//that has an invalid cert path, or hasn't been issued by a root certificate authority.
ldapconn.SessionOptions.VerifyServerCertificate += delegate { return true; };
//I use a boolean to toggle weather or not I want to automatically find and query the absolute root.
//If not, I'll just use the Domain value we already have from the config.
if (AutoRootDiscovery)
{
var getRootRequest = new SearchRequest(string.Empty, "objectClass=*", SearchScope.Base, "rootDomainNamingContext");
var rootResponse = (SearchResponse)ldapconn.SendRequest(getRootRequest);
Domain = rootResponse.Entries[0].Attributes["rootDomainNamingContext"][0].ToString();
}
//This is the filter I've been using : (&(objectCategory=person)(objectClass=user)(&(sAMAccountName={{UserName}})))
string ldapFilter = Filter.Replace("{{UserName}}", UserName);
//Now we can start building our search request
var getUserRequest = new SearchRequest(Domain, ldapFilter, SearchScope.Subtree, AttributeList);
//I only want one entry, so I set the size limit to one
getUserRequest.SizeLimit = 1;
//This is absolutely crucial in getting the request speed we need (milliseconds), as
//setting the DomainScope will suppress any refferal creation from happening during the search
SearchOptionsControl SearchControl = new SearchOptionsControl(SearchOption.DomainScope);
getUserRequest.Controls.Add(SearchControl);
//This happens incredibly fast, even with massive Active Directory structures
var userResponse = (SearchResponse)ldapconn.SendRequest(getUserRequest);
//Now, I have an object that operates very similarly to DirectoryEntry, mission accomplished
SearchResultEntry ResultEntry = userResponse.Entries[0];
The other thing I wanted to note here is that SearchResultEntry will return user "attributes" instead of "properties".
Attributes are returned as byte arrays, so you have to encode those in order to get the string representation. Thankfully, System.Text.Encoding contains a native ASCIIEncoding class that can handle this very easily.
string PropValue = ASCIIEncoding.ASCII.GetString(PropertyValueByteArray);
And that's about it! Very happy to finally have this figured out.
Cheers!
I have used below code to connect with ldaps using DirectoryEntry.
What i understood in my scenerio is directoryEntry does not work when ldaps is specified in server path or authentication type is mentioned as "AuthenticationTypes.SecureSocketsLayer" but if only ldaps port is mentioned at the end of server name it work. After having a look at wireshark log i can see handshake taking place as mentioned in above post.
Handshake:
Code:
public static SearchResultCollection GetADUsers()
{
try
{
List<Users> lstADUsers = new List<Users>();
DirectoryEntry searchRoot = new DirectoryEntry("LDAP://adserver.local:636", "username", "password");
DirectorySearcher search = new DirectorySearcher(searchRoot);
search.PropertiesToLoad.Add("samaccountname");
SearchResult result;
SearchResultCollection resultCol = search.FindAll();
Console.WriteLine("Record count " + resultCol.Count);
return resultCol;
}
catch (Exception ex)
{
Console.WriteLine("exception" + ex.Message);
return null;
}
}