Wednesday, April 8, 2015

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

Server Parameters

Server NameSMTP AddressPortSSL
Yahoo!smtp.mail.yahoo.com587Yes
GMailsmtp.gmail.com587Yes
Hotmailsmtp.live.com587Yes

Sample Code

using System.Net;
using System.Net.Mail;
string smtpAddress = "smtp.mail.yahoo.com";
int portNumber = 587;
bool enableSSL = true;
string emailFrom = "fromEmailID@yahoo.com";
string password = "yourEmailPassword";
string emailTo = "someone@domain.com";
string subject = "Testing Email ";
string body = "Hello, I'm sending Email via Code ..." ;
using (MailMessage mail = new MailMessage())
{
    mail.From = new MailAddress(emailFrom);
    mail.To.Add(emailTo);
    mail.Subject = subject;
    mail.Body = body;
    mail.IsBodyHtml = true;
 
    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);
    }
}




No comments: