使用System.Net.Mail通过gmail发送电子邮件

简介:
gmail的smtp采用了ssl连接:
     Outgoing Mail (SMTP) Server - requires TLS: smtp.gmail.com (use authentication)
     Use Authentication: Yes
     Use STARTTLS: Yes (some clients call this SSL)
     Port: 465 or 587 

知道了gmail的发信细节,用System.Net.Mail,就是下面这段代码就可以了
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  System.Net;
None.gif
using  System.Net.Mail;
None.gif
None.gif
namespace  GMailSend
ExpandedBlockStart.gif
{
InBlock.gif    
class Program
ExpandedSubBlockStart.gif    
{
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
try
ExpandedSubBlockStart.gif            
{
InBlock.gif                Program prg 
= new Program();
InBlock.gif                prg.Host 
= "smtp.gmail.com";
InBlock.gif                prg.SmtpUsername 
= "[email]zhangshanyou@gmail.com[/email]";
InBlock.gif                prg.SmtpPassword 
= "xxxxxxxx";
InBlock.gif                prg.Port 
= 587;
InBlock.gif                prg.Send(
"[email]zhangshanyou@gmail.com[/email]","[email]33235911@qq.com[/email]",  "test""test"nullnull);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gif            
{
InBlock.gif                Console.WriteLine(ex.Message);
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            Console.Read();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string _host;
InBlock.gif
InBlock.gif        
public string Host
ExpandedSubBlockStart.gif        
{
ExpandedSubBlockStart.gif            
get return _host; }
ExpandedSubBlockStart.gif            
set { _host = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private int _port;
InBlock.gif
InBlock.gif        
public int Port
ExpandedSubBlockStart.gif        
{
ExpandedSubBlockStart.gif            
get return _port; }
ExpandedSubBlockStart.gif            
set { _port = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private string _smtpUsername;
InBlock.gif
InBlock.gif        
public string SmtpUsername
ExpandedSubBlockStart.gif        
{
ExpandedSubBlockStart.gif            
get return _smtpUsername; }
ExpandedSubBlockStart.gif            
set { _smtpUsername = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private string _smtpPassword;
InBlock.gif
InBlock.gif        
public string SmtpPassword
ExpandedSubBlockStart.gif        
{
ExpandedSubBlockStart.gif            
get return _smtpPassword; }
ExpandedSubBlockStart.gif            
set { _smtpPassword = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Send(string from, string to, string subject, string body, string[] cc, string[] bcc)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
// Create mail message
InBlock.gif
            MailMessage message = new MailMessage(from, to, subject, body);
InBlock.gif            message.BodyEncoding 
= Encoding.GetEncoding(936);
InBlock.gif
InBlock.gif            
if (cc != null && cc.Length > 0)
ExpandedSubBlockStart.gif            
{
InBlock.gif                
foreach (string ccAddress in cc)
ExpandedSubBlockStart.gif                
{
InBlock.gif                    message.CC.Add(
new MailAddress(ccAddress));
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (bcc != null && bcc.Length > 0)
ExpandedSubBlockStart.gif            
{
InBlock.gif                
foreach (string bccAddress in bcc)
ExpandedSubBlockStart.gif                
{
InBlock.gif                    message.Bcc.Add(
new MailAddress(bccAddress));
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
// Send email
InBlock.gif
            SmtpClient client = new SmtpClient(this._host, this._port);
InBlock.gif            
if (!String.IsNullOrEmpty(this._smtpUsername) && !String.IsNullOrEmpty(this._smtpPassword))
ExpandedSubBlockStart.gif            
{
InBlock.gif                client.Credentials 
= new NetworkCredential(this._smtpUsername, this._smtpPassword);
ExpandedSubBlockEnd.gif            }

InBlock.gif            client.EnableSsl 
= true;
InBlock.gif
InBlock.gif            client.Send(message);
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
自由、创新、研究、探索…… 
Url:  [url]http://shanyou.cnblogs.com[/url] 
website:  [url]http://www.openbeta.cn[/url]

Feedback

#1楼    回复  引用  查看    

2007-06-10 00:19 by  Student [未注册用户]
public void Send(string from, string to, string subject, string body, string[] cc, string[] bcc) 

// Create mail message 
MailMessage message = new MailMessage(from, to, subject, body); 
message.BodyEncoding = Encoding.GetEncoding(936); 

if (cc != null && cc.Length > 0) 

foreach (string ccAddress in cc) 

message.CC.Add(new MailAddress(ccAddress)); 


if (bcc != null && bcc.Length > 0) 

foreach (string bccAddress in bcc) 

message.Bcc.Add(new MailAddress(bccAddress)); 



//能解释一下这段代码吗? 
谢谢





本文转自 张善友 51CTO博客,原文链接:http://blog.51cto.com/shanyou/74282,如需转载请自行联系原作者
目录
相关文章
C#编程-140:Net.Mail类发送邮件
C#编程-140:Net.Mail类发送邮件
C#编程-140:Net.Mail类发送邮件
|
安全 Java 网络安全
Java Mail---SMTP、POP3协议-DOS下手动收发邮件演示过程
Java Mail---SMTP、POP3协议-DOS下手动收发邮件演示过程
247 0
Java Mail---SMTP、POP3协议-DOS下手动收发邮件演示过程
|
数据安全/隐私保护