使用.net(C#)发送邮件学习手册(带成功案例)

1.了解发送邮件的三种方式
2.实例介绍使用client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.PickupDirectoryFromIis
3.如何设定本机IIS的SMTP服务器
1.了解发送邮件的三种方式
第一:client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
//通過遠程SMTP服務器傳送該郵件,這裡的network表示你要使用的远程SMTP服務器。
第二:client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.PickupDirectoryFromIis;
//通過本機SMTP服務器傳送該郵件,这里的PickupDirectoryFromIis表示你的邮件会通过本机IIS的SMTP服務器传送你的邮件。所以如果使用该项一定要设定在SMTP服務器上设定好你要转到的服务器的地址。下文会详细介绍。
第三:client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.SpecifiedPickupDirectory;
//表示电子邮件会被复制到System.Net.Mail.SmtpDeliveryMethod.PickupDirectorylocation所指定的目录中。以便有其他程序来执行发送该邮件。

2.实例介绍使用client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.PickupDirectoryFromIis传送邮件。
(1)mail.aspx的代码如下(直接粘贴):

Java代码 复制代码
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="mail.aspx.cs" Inherits="mail" %>   
      
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
      
    <html xmlns="http://www.w3.org/1999/xhtml" >   
    <head runat="server">   
        <title>mail to users</title>   
    </head>   
    <body>   
        <form id="form1" runat="server">   
        <div>   
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>   
        </div>   
        </form>   
    </body>   
    </html>  

(2)mail.aspx.cs代码如下:
注意:一般公司 都是代理上网的。所以如果使用该项。只能发送内部网的邮件。
但是并不是说该项不能发送外部网的邮件。而是代理封锁的原因。

    using System;   
    using System.Data;   
    using System.Configuration;   
    using System.Collections;   
    using System.Web;   
    using System.Web.Security;   
    using System.Web.UI;   
    using System.Web.UI.WebControls;   
    using System.Web.UI.WebControls.WebParts;   
    using System.Web.UI.HtmlControls;   
    using System.Net;   
    using System.Net.Mail;   
    public partial class mail : System.Web.UI.Page   
    {   
        protected void Page_Load(object sender, EventArgs e)   
        {   
            //SendMail(发件者, 收件者, 主旨, 内容, 主机,发件者昵称, 密码 ,附件)   
            SendMail("sunjie@yyhj.com.cn", "lilei.luo@yyhj.com.cn", "主旨", "邮件内容测试", "exhj.yyhj.com.cn", "孙节", "yyhj", "");   
        }   
        public void SendMail(string send, string recieve, string subject, string mailbody, string host, string uname, string pwd, string strFileName)   
        {   
            //生成一个   使用SMTP发送邮件的客户端对象   
            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();   
            //生成一个主机IP   
            //client.Port = 25; //587, 465, 995   
            client.Host = host;   
      
            //表示不以当前登录用户的默认凭据进行身份验证   
            client.UseDefaultCredentials =true ;   
            //包含用户名和密码   
            if (uname != "")   
            {   
                client.Credentials = new System.Net.NetworkCredential(uname, pwd);   
            }   
      
            //指定如何发送电子邮件。   
            client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.PickupDirectoryFromIis;   
            //通过本机SMTP服务器传送该邮件,   
            //其实使用该项的话就可以随意设定“主机,发件者昵称, 密码”,因为你的IIS服务器已经设定好了。而且公司内部发邮件是不需要验证的。   
      
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();   
            message.To.Add(recieve);   
            message.From = new System.Net.Mail.MailAddress(send, uname, System.Text.Encoding.UTF8);   
            message.Subject = subject;   
            message.Body = mailbody;   
            //定义邮件正文,主题的编码方式   
            message.BodyEncoding = System.Text.Encoding.GetEncoding("UTF-8");   
            message.SubjectEncoding = System.Text.Encoding.GetEncoding("UTF-8");   
            //获取或设置一个值,该值指示电子邮件正文是否为   HTML。   
            message.IsBodyHtml = false;   
            //指定邮件优先级   
            message.Priority = System.Net.Mail.MailPriority.High;   
            //添加附件   
            //System.Net.Mail.Attachment data = new Attachment(@"E:\9527\tubu\PA260445.JPG", System.Net.Mime.MediaTypeNames.Application.Octet);   
            if (strFileName != "" && strFileName != null)   
            {   
                Attachment data = new Attachment(strFileName);   
                message.Attachments.Add(data);   
            }   
      
            try  
            {   
                //发送   
                client.Send(message);   
               Label1.Text = "发送成功!";   
            }   
            catch (System.Net.Mail.SmtpException ex)   
            {   
                Label1.Text ="发送失败:"+ ex.Message;   
            }   
        }   
        }  

 

2.介绍使用client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network传送邮件。
使用该项的话。你的电脑首先必须是直接链接外网的。
那 就直接把mail.aspx.cs里的client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.PickupDirectoryFromIis;换成 client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
然后要设定的就是
//SendMail(发件者, 收件者, 主旨, 内容, 主机,发件者昵称, 密码 ,附件)
SendMail("loeley@gmail.com", "sy4l@163.com", "主旨", "12.37郵件內容", "smtp.163.com", "loeley", "81859505", "");
转自:http://hi.baidu.com/lslyl/blog/item/ba67366ef4202ddd80cb4afa.html

posted @ 2013-06-05 23:46  Net-Spider  阅读(240)  评论(0)    收藏  举报