Home > Enterprise >  Send mail using yahoo in ASP.Net
Send mail using yahoo in ASP.Net

Time:10-17

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("[email protected]", "[email protected]"))
        {
            mm.Subject = "test";
            mm.Body = "testing colisss de tabar...";
            mm.IsBodyHtml = false;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.mail.yahoo.com";
            smtp.EnableSsl = true;
            
            NetworkCredential NetworkCred = new NetworkCredential("[email protected]", "*******");
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = 465;
            try
            {
                smtp.Send(mm);
            }
            catch(SmtpException ex)
            {
                string p = "";
            }
            
        }

CodePudding user response:

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
        }
    }
}

CodePudding user response:

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.

  • Related