Tuesday 21 May 2013

Send Email With ASP.NET C# (Yahoo & Gmail)


Send Email With ASP.NET C# (Yahoo & Gmail)


I searched the we for ways to send mail with C# and alot of the examples that I found didn't work.  Here is one that did.
Gmail:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System.Net.Mail;
 
 
    {
        MailMessage mail = new MailMessage();
        mail.To.Add("xxxxx@gmail.com");
        mail.To.Add("xxxxx@yahoo.com");
        mail.From = new MailAddress("xxxxx@gmail.com");
        mail.Subject = "Email using Gmail";
 
        string Body = "Hi, this mail is to test sending mail" +
                      "using Gmail in ASP.NET";
        mail.Body = Body;
 
        mail.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
        smtp.Credentials = new System.Net.NetworkCredential
             ("xxxxx@gmail.com", "yourpassword");
        //Or your Smtp Email ID and Password
        smtp.EnableSsl = true;
        smtp.Send(mail);
 
        }
Yahoo:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System.Net.Mail;
 
{
 
            MailMessage mail = new MailMessage();
            mail.To.Add("email address to sent to");
            mail.To.Add("email address to sent to");
            mail.From = new MailAddress("from email address @ yahoo");
            mail.Subject = "Email using Yahoo";
 
            string Body = "Hi, this mail is to test sending mail" +
                          "using Yahoo in ASP.NET";
            mail.Body = Body;
 
            mail.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.mail.yahoo.com"; //Or Your SMTP Server Address
            smtp.Credentials = new System.Net.NetworkCredential
                 ("username", "password");
            //Or your Smtp Email ID and Password
            //smtp.EnableSsl = true;
            smtp.Send(mail);
 
}

No comments:

Post a Comment