krobelusmeetsyndra
krobelusmeetsyndra

Reputation: 181

SMTP exception when sending email with Yahoo

I have the following code I use to send emails from my application:

var config = DeserializeUserConfig(perfilAcesso.GetClientConfigPath() + "Encrypted");

using (SmtpClient client = new SmtpClient())
{
    client.Host = config.GetClientSMTP();
    client.EnableSsl = true;
    client.UseDefaultCredentials = false;
    client.Credentials = new System.Net.NetworkCredential(config.GetClientEmail(), config.GetClientPassword());

    using (MailMessage mail = new MailMessage())
    {
        mail.Sender = new MailAddress(config.GetClientEmail(), config.GetClientName());
        mail.From = new MailAddress(config.GetClientEmail(), config.GetClientCompany());
        mail.To.Add(new MailAddress("emailToReceive"));
        mail.Subject = "[PME] SOS - Equipamento Parado";
        mail.Body = "";

        client.Send(mail);
        MessageBox.Show("Email enviado com sucesso!");
   }
}

I have set up three possible SMTP hosts for the user to choose from: Gmail ("smtp.gmail.com"), Outlook ("smtp.live.com") and Yahoo ("smtp.mail.yahoo.com").

When I try to send and email using a Yahoo account, this exception is thrown:

System.Net.Mail.SmtpException: Mailbox unavailable. The server response was: Requested mail action not taken: mailbox unavailable.

I know for a fact that when sending emails with Gmail and Outlook accounts, the method works perfectly, because I tried it several times.

What am I doing wrong? Any help will be greatly appreciated!

Upvotes: 0

Views: 3596

Answers (2)

Mohamed Elrashid
Mohamed Elrashid

Reputation: 8579

Step 1

client.Port = 587;

Step 2

go to https://login.yahoo.com/account/security

Step 3

enable Allow apps that use less secure sign-in

enter image description here

Step 4 : full code

using System;
using System.Net.Mail;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {

            using (SmtpClient client = new SmtpClient())
            {
                client.Host = config.GetClientSMTP();
                client.EnableSsl = true;
                client.Port = 587;
                client.UseDefaultCredentials = false;
                client.Credentials = new System.Net.NetworkCredential(config.GetClientEmail(), config.GetClientPassword());

                using (MailMessage mail = new MailMessage())
                {
                    mail.Sender = new MailAddress(config.GetClientEmail(), config.GetClientName());
                    mail.From = new MailAddress(config.GetClientEmail(), config.GetClientCompany());
                    mail.To.Add(new MailAddress(config.emailToReceive));
                    mail.Subject = "Test 2";
                    mail.Body = "Test 2";
                    var isSend = false;
                    try
                    {
                        client.Send(mail);
                        isSend = true;
                    }
                    catch (Exception ex)
                    {
                        isSend = false;
                        Console.WriteLine(ex.Message);
                    }

                    Console.WriteLine(isSend ? "All Greeen" : "Bad Day");
                    Console.ReadLine();
                }
            }

        }
    }
}

if you add the same emails

 mail.To.Add(new MailAddress(config.emailToReceive));
mail.To.Add(new MailAddress(config.emailToReceive));

you will git Error

Bad sequence of commands. The server response was: 5.5.0 Recipient already specified

if you want to reuse MailMessage

  mail.To.Clear();

Upvotes: 4

user3188639
user3188639

Reputation:

Are you sure that your from/to addresses are correct? From and sender have to be your Yahoo addresses.

Here's a sample that works:

public static void Main(string[] args)
{
using (SmtpClient client = new SmtpClient())
{
    client.Host = "smtp.mail.yahoo.com";
    client.EnableSsl = true;
    client.UseDefaultCredentials = false;
    client.Credentials = new System.Net.NetworkCredential("my-yahoo-login", "yahoo-password");

    using (MailMessage mail = new MailMessage())
    {
        // This works 
        mail.Sender = new MailAddress("[email protected]", "Tom Test");
        mail.From = new MailAddress("[email protected]", "Tom Test");
        mail.To.Add(new MailAddress("[email protected]"));
/* This does not
                mail.Sender = new MailAddress("[email protected]", "Tom Test");
                mail.From = new MailAddress("[email protected]", "Tom Test");
                mail.To.Add(new MailAddress("[email protected]"));
*/
            mail.Subject = "Test mail";
        mail.Body = "Test mail";

        client.Send(mail);
        Console.WriteLine("Mail sent");
    }
}
}

If you put your non-Yahoo address in Sender and From fields (the commented code) you'll get the same exception.

Upvotes: 0

Related Questions