It's true; gmail allows you to use their mail servers to send email from your applications. If you combine that with the ASP.NET System.Net.Mail.SmtpClient class, you are ready to use your free gmail account and server.
Here is the Google mail server info you need:
Mail Host: smtp.gmail.com
Port: 587
You can use this info to send email from any Web or Windows application:
How to use in ASP.NET Web Application:
· In your web.config file, Add the following configuration section:
<system.net>
<mailSettings>
<smtp from=your_gmail_email@gmail.com>
<network host="smtp.gmail.com"
password="your_gmail_password"
port="587"
userName="your_gmail_email@gmail.com"/>
smtp>
mailSettings>
system.net>
· Now from any where in your ASP.NET web application, you can send email using System.Net.Mail.SmtpClient as follows:
using System.Net.Mail;
...
SmtpClient mailClient = new SmtpClient();
mailClient.EnableSsl = true;
mailClient.Send(from, to, subject, body);
This is very useful in many ways, for example, when you want to get notified about certain events that happen on your site. In my other article: "Ever wanted to know when Google bot visits your site?" I utilize the code above to know when a certain visitor comes to my site, specifically in this case, Google's spider.
How to use in .NET Windows Application:
· From your Windows application, you will need to setup the Network Credentials. Here is the code:
using System.Net.Mail;
using System.Net;
...
SmtpClient mailClient = new SmtpClient(
"smtp.gmail.com", "587");
mailClient.EnableSsl = true;
NetworkCredential cred = new NetworkCredential(
"your_gmail_username",
"your_gmail_password");
mailClient.Credentials = cred;
mailClient.Send("from_me@gmail.com", "to_you@gmail.com",
"subject goes here", "email body goes here");
And that's it! Please use the above code responsively :)
Комментариев нет:
Отправить комментарий