0

I have a web server at home running IIS 10 and .Net 4.8.

I am trying to send an email through a C#.Net, using the following yahoo stmp service call.

However, I cannot seem to get it to work? Whenever I try to execute the following code, the web page seems to be loading for about 30 seconds, then returns an "SMTP Server returned an invalid response" error message, which apparently doesn't mean anything specific? This is getting pretty frustrating as I've been on this for over 4 hours now... so thanks for any help!

using (MailMessage mm = new MailMessage("uneviesystems@yahoo.com", "MaxOvrdrv007@yahoo.ca"))
{
    mm.Subject = "test";
    mm.Body = "testing maudine...";
    mm.IsBodyHtml = false;
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.mail.yahoo.com";
    smtp.EnableSsl = true;
    
    NetworkCredential NetworkCred = new NetworkCredential("uneviesystems@yahoo.com", "*******");
    smtp.UseDefaultCredentials = true;
    smtp.Credentials = NetworkCred;
    smtp.Port = 465;
    try
    {
        smtp.Send(mm);
    }
    catch(SmtpException ex)
    {
        string p = "";
    }
            
}
MaxOvrdrv
  • 1,780
  • 17
  • 32
  • The following may be helpful: https://stackoverflow.com/a/55115761/10024425, [Yahoo OAuth 2.0 Guide](https://developer.yahoo.com/oauth2/guide/), [Implementing Yahoo OAuth 2.0 in CSharp and Asp.Net](https://www.yogihosting.com/implementing-yahoo-oauth-2-0-in-csharp-and-asp-net/), and [Send Email from Yahoo!, GMail, Hotmail (C#)](https://www.codeproject.com/Tips/520998/Send-Email-from-Yahoo-GMail-Hotmail-Csharp). – Tu deschizi eu inchid Oct 16 '22 at 17:40

3 Answers3

2

try using application password instead account password. You can generate application password in account settings.

kkosciolek
  • 46
  • 2
  • I did... tried Yahoo Dev and Google Dev for Mail... it only works 50% of the time, and only if (believe it or not!) I do not have any links in the content of the email message. Then I need to regen a new AppPassword. That works for about 5 emails, then back to 50% rate, if that... depends on the content of the email I found too. No links? 9 emails then it fails... lol – MaxOvrdrv Oct 18 '22 at 16:18
  • Try using MailKit library to send mail and wrap code on try...catch. If you code throw exception, show me your exception. Some mailboxes have limis e.g. for number of request. – kkosciolek Oct 20 '22 at 06:01
1

Port 465 isn't supported by System.Net.Mail.SmtpClient -

http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.enablessl.aspx

You could try using port 587 instead (if Yahoo supports it) and disable SSL.

  • Just tried this... I get a different error now: Transaction failed. The server response was: Email rejected – MaxOvrdrv Oct 16 '22 at 18:13
  • Try allowing “Less Secure Apps” on your yahoo account. – David Gerschcovsky Oct 16 '22 at 18:20
  • It is currently impossible for me to find this feature anywhere within my account settings or account info on Yahoo? All is see are generic icons which do not offer this option at all... I have change password, recovery email, etc... but NOT that option... do you have a URL for direct access to this? – MaxOvrdrv Oct 16 '22 at 18:50
  • https://support.reolink.com/hc/en-us/articles/360004195474-How-to-Allow-Less-Secure-Apps-to-Access-Your-Yahoo-Mail – David Gerschcovsky Oct 16 '22 at 20:01
  • 1
    Yeah they removed this feature completely in 2022... most of them did. I guess I will have to go with OAuth instead. I wonder how apps who send emails on behalf of their users, using their own email addresses can cope with this these days. I'm sure they don't ask every user for a key to their email... – MaxOvrdrv Oct 16 '22 at 22:55
0

Send your email asynchronously and format your code to dispose client properly.

using (var message = new MailMessage())
{
    message.To.Add(new MailAddress("recepient email", "receipient name"));
    message.From = new MailAddress("your email", "your name");
    message.Subject = "My subject";
    message.Body = "My message";
    message.IsBodyHtml = false; // change to true if body msg is in html

    using (var client = new SmtpClient("smtp.mail.yahoo.com"))
    {
        client.UseDefaultCredentials = false;
        client.Port = 587;
        client.Credentials = new NetworkCredential("your email", "your password");
        client.EnableSsl = true;

        try
        {
            await client.SendMailAsync(message); // Email sent
        }
        catch (Exception e)
        {
            // Email not sent, log exception
        }
    }
}
Ibrahim Timimi
  • 2,656
  • 5
  • 19
  • 31
  • I am getting a new exception: Unable to read data from the transport connection: net_io_connectionclosed. – MaxOvrdrv Oct 16 '22 at 18:04
  • @MaxOvrdrv Are you able to upgrade to .NET 4.8? It could solve your problem. – Ibrahim Timimi Oct 16 '22 at 18:09
  • Already done... Target Framework is set to 4.8, apppool is set to CLR 4.0 ... – MaxOvrdrv Oct 16 '22 at 18:12
  • @MaxOvrdrv Can you reach the SMTP port (587) on the server. Edit: You may use Telnet, `smtp.mail.yahoo.com 587` – Ibrahim Timimi Oct 16 '22 at 18:20
  • @MaxOvrdrv Could you try this before creating smtp client: `ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;` – Ibrahim Timimi Oct 16 '22 at 18:34
  • directly from the server, using telnet: 220 smtp.mail.yahoo.com ESMTP ready tried your second comment (Tls11): I get the exact same error message: net_io_connectionclosed. – MaxOvrdrv Oct 16 '22 at 18:39