Casting NetworkCredentials to ICredentialsByHost

In code to send an email from a game built using Unity I have the line:

SmtpServer.Credentials = (ICredentialsByHost)new System.Net.NetworkCredential(username, password);

Where SmtpServer is an instance of System.Net.SmtpClient (Just to be confusing!).

On all platforms I’ve tried this on it works fine, including the Unity Editor on Windows 8.1, except when run as a standalone build on Windows 8.1 where I get an InvalidCastException…

I can’t see a reason for the exception, particulally considering that NetworkCredentials implements the ICredentialsByHost interface so I’m left thinking Unity’s export is doing something?

Larger snippet of code:

//Create an SMTP server conection
SmtpServer = new SmtpClient(SMTPAddress);

SmtpServer.Port = SMTPPort;
SmtpServer.Credentials = (ICredentialsByHost)new System.Net.NetworkCredential(username, password);
SmtpServer.EnableSsl = useSSL;
SmtpServer.Timeout = timeout;

Thanks :slight_smile:

Bump, this is bugging me… :confused:

I am also facing this issue, anyone fixed it

It took a while me to figure it out. But it is simple issue in the player settings of the unity. change the player setting to use .Net2.0 rather than .Net2.0 subset. This will fix it.

5 Likes