What is the difference between a SmtpException and a SmtpFailedRecipientException - c#

I have been looking at the MSDN documentation and I cannot seem to work out what the difference is:
SmtpException Class
Represents the exception that is thrown when the SmtpClient is not able to complete a Send or SendAsync operation.
SmtpFailedRecipientException
Represents the exception that is thrown when the SmtpClient is not able to complete a Send or SendAsync operation to a particular recipient.
I can't find any information in the documentation to what "operation to a particular recipient" actually means.
Aside
The reason I ask is because I am trying to catch certain exceptions in my client application and execute different methods accordingly.
For Example:
If server is down I would like to leave the file where it is
If send address is invalid I would like to move the file to "Failed" folder

The source for SmtpClient.cs indicates that SmtpException is used for exceptions trying to get to the point of sending the message. SmtpFailedRecipientException is for an error reaching the client (but everything on the server worked fine).

Related

Get a StreamSocket's Connection Status (UWP)

I'm using a StreamSocket in a Windows Universal app to make a TCP/IP connection to a text based game (the socket is left open for an extended period of time). I have it connecting and returning data fine. Most socket implementations I've used in the past have a property or function (or a way to poll the socket) and get the status (whether it's open, closed, broken, pending, etc.).
I haven't been able to find a way to do this with the StreamSocket.
My question is, How do I query the status of the StreamSocket connection to see if it's dropped (without waiting until I send something down the pipe for it to error out)?
E.g., something so I could do something like:
if (this.Socket.Status != Status.Connected)...
if (this.Socket.Status != Status.Connected)
The code you wrote was quite elegant , however, as far as I know there is no such api provided by uwp.
The Windows.Networking.Sockets namespace has convenient helper methods and enumerations for handling errors when using sockets and WebSockets. This can be useful for handling specific network exceptions differently in your app.
An error encountered on DatagramSocket, StreamSocket, or StreamSocketListener operation is returned as an HRESULT value. The SocketError.GetStatus method is used to convert a network error from a socket operation to a SocketErrorStatus enumeration value. Most of the SocketErrorStatus enumeration values correspond to an error returned by the native Windows sockets operation. An app can filter on specific SocketErrorStatus enumeration values to modify app behavior depending on the cause of the exception.
For parameter validation errors, an app can also use the HRESULT from the exception to learn more detailed information on the error that caused the exception. Possible HRESULT values are listed in the Winerror.h header file. For most parameter validation errors, the HRESULT returned is E_INVALIDARG
.

Telling When Couchbase Query Times Out

I am using couchbase using the .NET client to interact with it. I need to be able to differentiate the cause of a failed call to my couchbase box and log them accurately. I've tried provoking a timeout in my testing environment but I have been unable to.
Reading the .net client documentation it appears that ExecuteGet returns an IGetOperationResult object that contains both an exception property and a status code property. The status codes would appear to correspond to this table. I haven't found in the documentation for the .NET client as to whether or not it throws an exception like the Java client. Also, the status codes from the memcached table doesn't seem to have a clear status code value that corresponds to a timeout.
How would one tell whether or not the configured timeout has been exceeded from a call besides not having a value returned?
The StatusCode property is set only when a response from the server is sent. A timeout would result in no response from the server, therefore a null StatusCode. A connection timeout should result in the Exception value being populated on the GetOperationResult...
https://github.com/couchbase/couchbase-net-client/blob/2dc8a63208dfa8b991290d128b14002ad4c43f53/src/Enyim.Caching/Memcached/PooledSocket.cs#L68
However, it is quite possible that somewhere further up the Enyim stack, the original timeout exception is being swallowed and rewrapped in another exception. When you experience timeouts, are you seeing any value for the Exception property?

SMPP BIND ERROR: 0x0000000D in EasySMPP

I am implementing the SMPP client using EasySMPP for .NET
The application is compiling fine but there was no successful outcome and I am getting this weird error
SMPP BIND ERROR: 0x0000000D
What can be done for this, please help.
The error code of 0x0000000D is unfortunately a rather generic "Bind Failed".
To find out what the error codes mean check section 5.1.3, command_status, of the SMPP specification you linked to.
Common causes for getting a "Bind Failed" response are:
Your supplier only allows certain IP addresses to connect
Trying to connect to wrong hostname/port combination
Wrong username/password
To troubleshoot, you could try running Wireshark on your client and take a look at the SMPP PDUs being passed backwards and forwards, if you post the capture on here I'm happy to take a look. Or you could give your supplier a call, they may be able to see something helpful in their server logs.

SmtpClient get result from server on send

The SmtpClient send method returns void. Is there any way to get the server response? Do I just assume it was successful unless it throws an exception?
The class I'm referring to... http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx
To answer your second point, yes, all you can do is assume it's successful - meaning it got the message to the server and the server accepted it, unless you get an exception.
You probably already know the rest of this, but just in case...
From there, the email could get lost and not delivered any number of ways. Your server may accept it and decide not to send it, or accept it and lose power before crashing. It may get blocked by a spam filter along the way, etc.
You can think of an email as being similar to a regular piece of mail in that it passes through several hands between the sender and the recipient. From your code, you can only confirm that it got to the SMTP server you're using to send, which is similar to handing it to a teller at the post office. You don't know (or need to know) how the message is routed from there. it could be by air, ground, or carrier pigeon. You're out of the equation - you don't need to know how it gets sent, just that you trust that they know how to send it. (The same can be said for an email.)
If you need to confirm that the recipient opened it, there are ways of embedding an image in an HTML message on your server and tracking in your logs when that image is accessed, etc. (Google email tracking and email open tracking)
On the other hand...
If the server rejects it, then you do get a server response in a manner of speaking - there should be an error code and a description in the error, which you can use to troubleshoot why it didn't make it, or use error handling to try another route, etc.
You can utilize SendCompleted Event to check that your smtpclient works fine like this:
http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.sendcompleted.aspx
But you cannot get confirmation that your message reached recipient because it may stuck in any server/filter in message chain.
You assume that it was successful unless it throws... although success in this case only means that it was accepted by the mail server, anything else is then up to the server...
IF you want a little bit of control you can use SendAsync and hook the SendCompleted event...

TcpClient.BeginRead/TcpClient.EndRead doesn't throw exception when internet disconnected

I'm using TcpListener to accept & read from TcpClient.
The problem is that when reading from a TcpClient, TcpClient.BeginRead / TcpClient.EndRead doesn't throw exception when the internet is disconnected. It throws exception only if client's process is ended or connection is closed by server or client.
The system generally has no chance to know that connection is broken. The only reliable way to know this is to attempt to send something. When you do this, the packet is sent, then lost or bounced and your system knows that connection is no longer available, and reports the problem back to you by error code or exception (depending on environment). Reading is usually not enough cause reading only checks the state of input buffer, and doesn't send the packet to the remote side.
As far as I know, low level sockets doesn't notify you in such cases. You should provide your own time out implementation or ping the server periodically.
If you want to know about when the network status changes you can subscribe to the System.Net.NetworkInformation.NetworkChange.NetworkAvailabilityChanged event. This is not specific to the internet, just the local network.
EDIT
Sorry, I misunderstood. The concept of "connected" really doesn't exist the more you think about it. This post does a great job of going into more details about that. There is a Connected property on the TcpClient but MSDN says (emphasis mine):
Because the Connected property only
reflects the state of the connection
as of the most recent operation, you
should attempt to send or receive a
message to determine the current
state. After the message send fails,
this property no longer returns true.
Note that this behavior is by design.
You cannot reliably test the state of
the connection because, in the time
between the test and a send/receive,
the connection could have been lost.
Your code should assume the socket is
connected, and gracefully handle
failed transmissions.
Basically the only way to check for a client connection it to try to send data. If it goes through, you're connected. If it fails, you're not.
I don't think you'd want BeginRead and EndRead throwing exceptions as these should be use in multi threaded scenarios.
You probably need to implement some other mechanism to respond to the dropping of a connection.

Categories