10 Sep

C# Send an Email with Attachment from Yahoo

This example shows how to send an email with attachment from Yahoo.
To send an email using Yahoo, we need to add a reference to the dynamic link library System.Net.Mail.
To do this:

  • Go to solution explorer of your project
  • Select add a reference
  • Click on .Net Tab
  • Select System.Net.Mail.

The Yahoo SMTP Server requires an encrypted connection (SSL) on port 587.

Sample :

        public void SendMailUsingYahoo()
        {
            try
            {
                MailMessage mailMessage = new MailMessage();
                mailMessage.From = new MailAddress("sender@yahoo.com");

                //receiver email adress
                mailMessage.To.Add("receiver@gmail.com");

                //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("sender@yahoo.com", "password");
                //enabled SSL
                smtpClient.EnableSsl = true;
                //Send an email
                smtpClient.Send(mailMessage);
            }
            catch (Exception ex)
            {

            }
        }

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

  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());
    }
    }
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *