0

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!

2 Answers2

4

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();
Mohamed Elrashid
  • 8,125
  • 6
  • 31
  • 46
0

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("my-email-address@yahoo.co.uk", "Tom Test");
        mail.From = new MailAddress("my-email-address@yahoo.co.uk", "Tom Test");
        mail.To.Add(new MailAddress("my-email-address@outlook.com"));
/* This does not
                mail.Sender = new MailAddress("my-email-address@outlook.com", "Tom Test");
                mail.From = new MailAddress("my-email-address@outlook.com", "Tom Test");
                mail.To.Add(new MailAddress("my-email-address@yahoo.co.uk"));
*/
            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.

  • I am sure the email being passed to `Sender` and `From` fields is Yahoo, because I'm using a method to assign it. If it works for Gmail and Outlook, I know the emails are correctly being passed, and that should work for Yahoo too. – krobelusmeetsyndra Jan 11 '19 at 09:58