Click here to Skip to main content
15,887,027 members
Articles / Programming Languages / C#
Tip/Trick

Send Email from Yahoo!, GMail, Hotmail (C#)

Rate me:
Please Sign up or sign in to vote.
4.89/5 (50 votes)
27 Sep 2013CPOL 395.9K   77   28
Sending email easily from Yahoo!, GMail, Hotmail in C#.
The information provided below was correct at the time of writing.
Yahoo!, Gmail and Hotmail have already upgraded their security authentication system, more steps are required to successfully log into their mail server using programing code. Thus, the following code is no more working. But however, it does still work for most email servers other than Yahoo!, Gmail and Hotmail.

Server Parameters

Server Name SMTP Address Port SSL
Yahoo! smtp.mail.yahoo.com 587 Yes
GMail smtp.gmail.com 587 Yes
Hotmail smtp.live.com 587 Yes

Sample Code

C#
using System.Net;
using System.Net.Mail;

string smtpAddress = "smtp.mail.yahoo.com";
int portNumber = 587;
bool enableSSL = true;

string emailFrom = "email@yahoo.com";
string password = "abcdefg";
string emailTo = "someone@domain.com";
string subject = "Hello";
string body = "Hello, I'm just writing this to say Hi!";

using (MailMessage mail = new MailMessage())
{
    mail.From = new MailAddress(emailFrom);
    mail.To.Add(emailTo);
    mail.Subject = subject;
    mail.Body = body;
    mail.IsBodyHtml = true;
    // Can set to false, if you are sending pure text.

    mail.Attachments.Add(new Attachment("C:\\SomeFile.txt"));
    mail.Attachments.Add(new Attachment("C:\\SomeZip.zip"));

    using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
    {
        smtp.Credentials = new NetworkCredential(emailFrom, password);
        smtp.EnableSsl = enableSSL;
        smtp.Send(mail);
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Other Other
Programming is an art.

Comments and Discussions

 
QuestionException Pin
mughal_158324-Feb-13 22:52
mughal_158324-Feb-13 22:52 
AnswerRe: Exception Pin
adriancs26-Sep-13 18:24
mvaadriancs26-Sep-13 18:24 
GeneralMy vote of 5 Pin
Joezer BH5-Jan-13 21:36
professionalJoezer BH5-Jan-13 21:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.