Click here to Skip to main content
15,033,028 members
Articles / Programming Languages / C#
Tip/Trick
Posted 3 Jan 2013

Tagged as

Stats

324.6K views
1.9K downloads
78 bookmarked

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
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)

Share

About the Author

adriancs
Software Developer
Malaysia Malaysia
Programming is an art.

Comments and Discussions

 
QuestionShow error in reg Pin
Member 1356745217-Jan-18 5:40
MemberMember 1356745217-Jan-18 5:40 
QuestionSending email programmatically through GMail in /java Pin
Member 1035012817-Aug-17 11:40
MemberMember 1035012817-Aug-17 11:40 
QuestionGetting Error Pin
pvshafeeq2-May-17 22:51
Memberpvshafeeq2-May-17 22:51 
AnswerRe: Getting Error Pin
adriancs3-May-17 15:34
professionaladriancs3-May-17 15:34 
QuestionGreat Pin
Yampolka2-May-17 15:39
MemberYampolka2-May-17 15:39 
Questionsource code Pin
Member 1306558123-Mar-17 7:29
MemberMember 1306558123-Mar-17 7:29 
AnswerRe: source code Pin
adriancs2-May-17 20:03
professionaladriancs2-May-17 20:03 
QuestionThanks Pin
trivedijignesh23-May-15 19:55
Membertrivedijignesh23-May-15 19:55 
AnswerRe: Thanks Pin
vaynenick1-May-16 19:12
Membervaynenick1-May-16 19:12 
Questionthanks , you save my time Pin
mehrdadhj27-Mar-15 13:51
Membermehrdadhj27-Mar-15 13:51 
GeneralMy vote of 1 Pin
Talwinder.Gur22-Feb-15 19:12
MemberTalwinder.Gur22-Feb-15 19:12 
QuestionGetting Error Pin
Member 1046902731-Aug-14 1:10
professionalMember 1046902731-Aug-14 1:10 
AnswerRe: Getting Error Pin
adriancs31-Aug-14 3:33
professionaladriancs31-Aug-14 3:33 
GeneralRe: Getting Error Pin
Iparke15-Mar-15 22:34
MemberIparke15-Mar-15 22:34 
QuestionProxy Usage.. Pin
Musab Hussain21-Aug-14 3:56
MemberMusab Hussain21-Aug-14 3:56 
QuestionCould not send the e-mail - error: Failure sending mail. Pin
Member 109250676-Aug-14 4:46
MemberMember 109250676-Aug-14 4:46 
QuestionVery nice code Pin
Member 1033383527-Mar-14 10:02
MemberMember 1033383527-Mar-14 10:02 
QuestionWaOoo! Pin
Blue_sky00730-Sep-13 6:32
MemberBlue_sky00730-Sep-13 6:32 
GeneralMy vote of 5 Pin
PCoffey27-Sep-13 6:47
MemberPCoffey27-Sep-13 6:47 
GeneralMy vote of 5 Pin
Wooters27-Sep-13 6:29
MemberWooters27-Sep-13 6:29 
GeneralMy vote of 5 Pin
Moosavi S.M.27-Sep-13 4:46
MemberMoosavi S.M.27-Sep-13 4:46 
GeneralMy vote of 5 Pin
Member 892926517-Aug-13 11:23
MemberMember 892926517-Aug-13 11:23 
QuestionNice POST Pin
boss prabu4-Jun-13 7:24
Memberboss prabu4-Jun-13 7:24 
AnswerRe: Nice POST Pin
Blue_sky00730-Sep-13 5:52
MemberBlue_sky00730-Sep-13 5:52 
GeneralNice work Pin
MustafaHamed29-Apr-13 5:34
MemberMustafaHamed29-Apr-13 5:34 

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.