-2

So i tried making an email sender and give my account info and this error showed up:

Cannot implicitly convert type ('string', 'string') to System.Net.ICredentialsByHost.

This is the code.

SmtpClient SmtpServer = new SmtpClient("smpt.gmail.com", 587);

SmtpServer.Credentials = ("username", "password"); # The email and password were lighted up with red
MailMessage Mail = new MailMessage();
Mail.From = new MailAddress("from");

I changed the email and password for obvious reasons.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 3
    The part `= ("username", "password")` looks wrong. Did you [read the documentation of `SmtpClient.Credential`](https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient.credentials)? – Uwe Keim Mar 07 '20 at 13:34

2 Answers2

4

You are trying to convert a ValueTuple to ICredentialsByHost. Need to construct a new NetworkCredential instance and set it in SmtpServer:

NetworkCredential credentials = new NetworkCredential("username", "password");  
SmtpServer.Credentials = credentials;
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Pethical
  • 1,472
  • 11
  • 18
0

The SmtpServer.Credentials property needs an object that interface from the ICredentialsByHost interce. ("username", "password") can't be implicitly converted to an object that ICredentialsByHost interface.

You could use the NetworkCredential class like this

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

See this answer

Floris Devreese
  • 3,127
  • 2
  • 21
  • 31