用C#实现用免费smtp服务器(GMail)发邮件(转)

原文地址:http://www.cnblogs.com/skywind/archive/2007/09/21/901734.html

Method 1

--------------------------------------------------------------------------------------------------------

ExpandedBlockStart.gif View Code
//  using System.Net.Mail;
MailMessage message =  new MailMessage();
message.From =  new MailAddress( " Sender@gmail.com "" Your DisplayName ");  //  发件人邮箱地址和显示名称
message.To.Add( new MailAddress( " Recipients@gmail.com "));  //  收件人地址,可以设置多个

message.Subject =  " 邮件标题 " ;

message.Body =  " 邮件正文 ";

message .IsBodyHtml =  false//  设置邮件正文是否为 html 格式的值
message .BodyEncoding = System.Text.Encoding.UTF8;   //  设置邮件正文编码
message .Priority = MailPriority.Normal;  //  设置电子邮件的优先级

//  包含附件

string attachPath =  " 附件地址 ";

System.Net.Mail.Attachment attachment =  new System.Net.Mail.Attachment(attachPath); 

attachment.Name = System.IO.Path.GetFileName(attachPath);  //  附件名称
attachment.NameEncoding = System.Text.Encoding.GetEncoding( " gb2312 ");  //  附件名称的编码
attachment.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;  //  设置附件的编码
attachment.ContentDisposition.Inline =  true;
attachment.ContentDisposition.DispositionType = System.Net.Mime.DispositionTypeNames.Inline;
string cid = attachment.ContentId;  //  关键性的地方,这里得到一个id数值

message .Attachments.Add(attachment);  //  可以添加多个附件

 

SmtpClient client =  new SmtpClient();

client .Host =  " smtp.gmail.com "//  设置 smtp 事务的主机名称或 IP 地址
client .Port =  587//  端口号

client.Credentials =  new System.Net.NetworkCredential( " Sender@gmail.com "" mailbox password ");  //  Your mail address & password
client.EnableSsl =  true//  经过ssl加密,gmail 邮箱必须设置为 ture

try
{
client.Send(message);
Response.Write( " 邮件发送到 " + message.To.ToString() +  " <br> ");
}
catch (Exception ee)
{
Response.Write(ee.messageage +  " <br> " + ee.InnerException.messageage);

Method 2
------------------------------------------------------------------------------------------------------

// http://weblogs.asp.net/scottgu/archive/2005/12/10/432854.aspx

ExpandedBlockStart.gif View Code
//  using System.Net.Mail;
MailMessage message =  new MailMessage();
message.From =  new MailAddress( " Sender@gmail.com "); 

message.To.Add( new MailAddress( " Recipients@gmail.com ")); 

message.Subject =  " This is my subject "
message.Body =  " This is the content "
SmtpClient client =  new SmtpClient();
client.EnableSsl =  true;   //  经过ssl加密,gmail 邮箱必须设置为 ture 

try
{
client.Send(message);
Response.Write( " 邮件发送到 " + message.To.ToString() +  " <br> ");
}
catch (Exception ee)
{
Response.Write(ee.Message );
//In web.config

<system.net>
<mailSettings>
<smtp from="displayName<from@gmail.com>">
<network host="smtp.gmail.com" port="587" userName="Sender@gmail.com" password="your pwd" />
<!-- if has 'defaultCredentials="true"' , using Gmail can not send success -->
</smtp>
</mailSettings>
</system.net>

 

转载于:https://www.cnblogs.com/beijia/archive/2012/05/09/mail.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值