How to Send Email in ASP.NET

{0 Comments}

In this article you can know how to send email or mail through asp.net. For this some basic things which you must notice that the required namespace, port number, SMTP server address are some of the basic things.

  • Basic namespace you must include are;

 C# |  copy code |? 
1
using System.Net;
2
using System.Net.Mail;
3

 

  • Other important thing is Port number, as here we are using gmail’s smtp server so the port number will be 567. But it may change according to your mailing server.
  • SMTP address:  here for gmail is smtp.gmail.com, it varies form server to server

Now have a look on the function/methode which you can use for sending mail is 

 C# |  copy code |? 
01
public void SendEmail(string from, string to, string subject, string body)
02
            {
03
                MailMessage mail = new MailMessage(new MailAddress(from), new MailAddress(to));
04
 
05
                mail.Subject = subject;
06
                mail.Body = body;
07
 
08
                SmtpClient smtpMail = new SmtpClient("smtp.gmail.com");
09
                smtpMail.Port = 587;
10
                smtpMail.EnableSsl = true;
11
                smtpMail.Credentials = new NetworkCredential("xxxx@gmail.com", "xxxxxxx");
12
                // and then send the mail
13
                smtpMail.Send(mail);
14
            }

Here sendEmail is the method which will send mail, here this methode take four parameter which are from,to,subject,body . then all you have change is the Network Credential parameters where first parameter is the email id form where it is going to send mail. And other where its password. 

Leave a Comment

Your email address will not be published.