今天想着收集一下如何用C#代码来实现发送GMail.
下面是我自己写的测试方法,测试通过。
Send GMail Code
static void SendGMail()
{
try
{
// Article: http://www.geekzone.co.nz/tonyhughes/599
//smtp.gmail.com (use authentication)
//Use Authentication: Yes
//Use STARTTLS: Yes (some clients call this SSL)
//Gmail Port: 465 or 587
int port = 587;
string from = "from@gmail.com";
string to = "to@sina.com";
string userid = "from";
string password = "XXX";
MailMessage msg = new MailMessage(from, to);
msg.Subject = "test mail through Gmail by Bu Hai-Qing";
msg.Body = "Hello World!";
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
NetworkCredential crendetial = new NetworkCredential(userid, password);
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = crendetial;
client.Send(msg);
}
catch (System.Exception ex)
{
}
}
static void SendGMail()
{
try
{
// Article: http://www.geekzone.co.nz/tonyhughes/599
//smtp.gmail.com (use authentication)
//Use Authentication: Yes
//Use STARTTLS: Yes (some clients call this SSL)
//Gmail Port: 465 or 587
int port = 587;
string from = "from@gmail.com";
string to = "to@sina.com";
string userid = "from";
string password = "XXX";
MailMessage msg = new MailMessage(from, to);
msg.Subject = "test mail through Gmail by Bu Hai-Qing";
msg.Body = "Hello World!";
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
NetworkCredential crendetial = new NetworkCredential(userid, password);
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = crendetial;
client.Send(msg);
}
catch (System.Exception ex)
{
}
}