Send an Email with an Attachment from Yahoo Mail in C#

To send email from a Yahoo Mail account in C#, connect to smtp.mail.yahoo.com, start TLS and sign in with an app password. Yahoo rejects your normal account password from mail programs and code, so the first step happens in the browser, not in Visual Studio.

The example uses MailKit, which Microsoft recommends over the built-in System.Net.Mail.SmtpClient (that class is marked as not recommended for new development).

dotnet add package MailKit

Generate an app password

Sign in to Yahoo, open Account Info, then Account Security, and choose “Generate app password”. Give it a name such as “Report sender” and copy the generated password. It only grants access to mail, and you can revoke it from the same page without touching your real password.

Treat it like any other secret: keep it in an environment variable, user secrets or a secret store, never in source code.

Send the email

using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;

string sender = GetRequired("YAHOO_ADDRESS");
string appPassword = GetRequired("YAHOO_APP_PASSWORD");

var message = new MimeMessage();
message.From.Add(new MailboxAddress("Report Bot", sender));
message.To.Add(MailboxAddress.Parse("receiver@example.com"));
message.Subject = "Monthly report";

var body = new BodyBuilder
{
    TextBody = "The monthly report is attached.",
    HtmlBody = "<p>The monthly report is <b>attached</b>.</p>"
};
body.Attachments.Add(@"C:\reports\report.pdf");
message.Body = body.ToMessageBody();

using var smtp = new SmtpClient();
await smtp.ConnectAsync("smtp.mail.yahoo.com", 465, SecureSocketOptions.SslOnConnect);
await smtp.AuthenticateAsync(sender, appPassword);
await smtp.SendAsync(message);
await smtp.DisconnectAsync(true);

static string GetRequired(string name) =>
    Environment.GetEnvironmentVariable(name)
    ?? throw new InvalidOperationException($"Environment variable {name} is not set.");

Yahoo accepts both port 465 with TLS from the first byte (SslOnConnect) and port 587 with STARTTLS (SecureSocketOptions.StartTls). Either is fine; if one port is blocked on your network, try the other.

Things that go wrong

AuthenticationException with the right password. You are using the account password instead of an app password, or the app password was revoked. Generate a new one.

“Sender address rejected”. The From address must be the Yahoo account you signed in with. You can change the display name freely, but not the address.

Large attachments. Yahoo caps messages at about 25 MB, and base64 encoding inflates attachments by roughly a third. For anything big, upload the file somewhere and send a link.

Mixing namespaces. SmtpClient in this example is MailKit.Net.Smtp.SmtpClient. If the file also imports System.Net.Mail, the compiler reports an ambiguous reference. Remove the old using or alias one of them.

Legacy System.Net.Mail code

If you maintain code based on the original version of this post, the quick fix is to replace the account password with an app password and keep port 587 with EnableSsl = true. System.Net.Mail.SmtpClient cannot use port 465, because it only supports STARTTLS, not TLS from the start of the connection. Plan to move to MailKit when you next touch the code: it is async throughout, supports OAuth if Yahoo ever forces it, and gives proper error messages.

For automated, high-volume mail (newsletters, password resets), do not use a personal Yahoo account at all. Yahoo enforces sending limits and will lock accounts that look like they are sending bulk mail. A transactional email service is the right tool there.

Related examples

2 thoughts on “Send an Email with an Attachment from Yahoo Mail in C#”

  1. hi, can You please tell me what is a problem?

    using System.Net.Mail;
    using System.Net;

    namespace WinFormsApp1
    {
    public partial class Form1 : Form
    {
    private static string htmlString;

    public Form1()
    {
    InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    Email();
    }
    public static void Email()
    {
    try
    {
    MailMessage mailMessage = new MailMessage();
    mailMessage.From = new MailAddress(“limfacentrum@yahoo.com”);

    //receiver email adress
    mailMessage.To.Add(“zoo.malpa@wp.pl”);

    //subject of the email
    mailMessage.Subject = “This is a subject”;

    //attach the file
    // mailMessage.Attachments.Add(new //Attachment(@”C:\\attachedfile.jpg”));
    // mailMessage.Body = “Body of the email”;
    // mailMessage.IsBodyHtml = true;

    //SMTP client
    SmtpClient smtpClient = new SmtpClient(“smtp.mail.yahoo.com”);
    //port number for Yahoo
    smtpClient.Port = 587;
    //credentials to login in to yahoo account
    smtpClient.Credentials = new NetworkCredential(“limfacentrum@yahoo.com”, “correctPASS”);
    //enabled SSL
    smtpClient.EnableSsl = true;
    //Send an email
    smtpClient.Send(mailMessage);
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message.ToString());
    }
    }
    }
    }

    Reply

Leave a Comment