狂淘

          www.kuangtao.net

             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            6 隨筆 :: 185 文章 :: 68 評論 :: 0 Trackbacks
           /// <summary>
            2    /// 自定義方法用來發(fā)送郵件
            3     /// </summary>
            4    /// <param name="Sender">發(fā)件人地址</param>
            5    /// <param name="Receiver">收件人地址</param>
            6    /// <param name="Subject">郵件標題</param>
            7    /// <param name="Content">郵件內(nèi)容</param>
            8    /// <param name="server">服務(wù)器名</param>
            9    /// <returns>返回一個布爾值,如果返回True表示發(fā)送成功!否則為false</returns>

           10
           11     #region sendMail1 Jmail    
           12     public bool sendMail1(string Sender, string Receiver,string Name,string Pwd, string Subject, string Content, string server)
           13    {       
           14        //int sunEmail = 0;
           15        jmail.MessageClass myJmail = new jmail.MessageClass();
           16        myJmail.Charset = "GB2312"//設(shè)置使用的郵件字符集,默認US-ASCII 中國則為GB2312
           17        myJmail.Encoding = "base64"//     
           18        myJmail.ISOEncodeHeaders = false//郵件頭是否使用iso-8859-1編碼 默認值為true;     
           19        //myJmail.ContentType="text/html";
           20        myJmail.Priority =Convert.ToByte(1); //優(yōu)先級別 1最高
           21         myJmail.From = Sender;  //返回或設(shè)置發(fā)件人的地址
           22         myJmail.MailServerUserName = Name;//發(fā)送人郵箱用戶名
           23         myJmail.MailServerPassWord = Pwd; //發(fā)送人郵箱密碼
           24
           25         //myJmail.AddHeader("Priority","3");
           26        //myJmail.AddHeader("MSMail-Priority", "Normal");
           27        //myJmail.AddHeader("Mailer","Microsoft Outlook Express 6.00.2800.1437");
           28        //myJmail.AddHeader("MimeOLE","Produced By Microsoft MimeOLE V6.00.2800.1441");
           29
           30        myJmail.Subject = Subject;  //郵件的主題(標題)
           31        myJmail.AddRecipient(Receiver, """"); //添加收件人
           32        if (FileUp.PostedFile.ContentLength != 0)
           33        {
           34            string filePath = FileUp.PostedFile.FileName;
           35            myJmail.AddAttachment(@filePath,false,""); //添加一個附件
           36        }

           37
           38        Content ="<html>"
           39               +"<head>"
           40               +"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\" />"
           41               +"<title>SendEmail</title>"
           42               +"<style type=\"text/css\">"
           43               +"</style>"
           44               +"</head>"
           45               +"<body>"
           46               +System.DateTime.Now
           47               +"<hr/>"
           48               + Content
           49               +"</body>"
           50               +"</html>";
           51
           52        myJmail.Body = Content;  //郵件的正文
           53
           54        return myJmail.Send(server, true);//發(fā)送郵件
           55
           56    }

           57    #endregion
           sendMail1
           58
           59    #region sendMail2 SmtpClient  
           60    public bool sendMail2(string fromAddre, string toAddre, string subject, string body, string userName, string password, string smtpHost)
           61    {
           62
           63        MailMessage message = new MailMessage();
           64
           65        message.From = new MailAddress(fromAddre, userName, System.Text.Encoding.GetEncoding("gb2312"));
           66        message.To.Add(new MailAddress(toAddre, toAddre, System.Text.Encoding.GetEncoding("gb2312")));
           67        message.Subject = subject;//設(shè)置郵件主題        
           68        message.SubjectEncoding = System.Text.Encoding.GetEncoding("gb2312");
           69        message.IsBodyHtml = true;//設(shè)置郵件正文為html格式      
           70        message.Body = body;//設(shè)置郵件內(nèi)容 
           71        message.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312");
           72        message.Priority = MailPriority.High;
           73
           74        SmtpClient client = new SmtpClient(); //smtp服務(wù)器
           75
           76        client.Host = smtpHost;
           77        //client.Port = 25;
           78        //client.Credentials = new NetworkCredential(fromAddre, password); //用戶名憑證 
           79        client.DeliveryMethod = SmtpDeliveryMethod.Network; //設(shè)置發(fā)送方式 
           80        client.UseDefaultCredentials = true;
           81        CredentialCache myCache = new CredentialCache();
           82        myCache.Add(smtpHost,25,"login"new NetworkCredential(userName, password));
           83        client.Credentials = myCache;
           84        if (FileUp.PostedFile.ContentLength != 0)
           85        {
           86          string filePath = FileUp.PostedFile.FileName;
           87          Attachment data = new Attachment(@filePath);
           88          message.Attachments.Add(data); //添加一個附件
           89        }
           
           90        try
           91        {
           92            client.Send(message);
           93            return true;
           94        }

           95        catch (Exception ex)
           96        {
           97            //Response.Write("<script>alert('" + ex.Message + "');</script>");
           98            return false;
           99        }

          100
          101    }

          102    #endregion

          103
          104    #region sendmail3 SmtpClient
          105    public bool sendMail3(string Sender, string Receiver, string Name, string Pwd, string Subject, string Content, string server)
          106    {
          107
          108        System.Net.Mail.MailAddress senderAddresss = new MailAddress(Sender);
          109        System.Net.Mail.MailAddress receiverAddresss = new MailAddress(Receiver);
          110        System.Net.Mail.MailMessage message = new MailMessage(senderAddresss, receiverAddresss);
          111        message.Subject = Subject;
          112        message.Body = Content;
          113        message.BodyEncoding = System.Text.Encoding.UTF8;
          114        message.IsBodyHtml = true;
          115
          116        System.Net.Mail.SmtpClient client = new SmtpClient();
          117        client.Host = server;
          118        client.UseDefaultCredentials = true;
          119        client.Credentials = new System.Net.NetworkCredential(Name, Pwd);
          120
          121        client.DeliveryMethod = SmtpDeliveryMethod.Network;
          122
          123        //添加附件
          124        //Attachment data = new Attachment(@"附件地址如:e:\a.jpg", System.Net.Mime.MediaTypeNames.Application.Octet);
          125        // message.Attachments.Add(data);
          126
          127        try
          128        {
          129            client.Send(message);
          130            return true;
          131
          132        }

          133        catch (Exception ex)
          134        {
          135            return false;
          136        }

          137
          138    }

          139    #endregion

          140
          141    #region sendMail4 web.mail
          142    public bool sendmail4(string Sender, string Receiver, string Name, string Pwd, string Subject, string Content, string server)
          143    
          144         MailMessage mailMsg = new MailMessage();
          145        //發(fā)送地址
          146        mailMsg.From = Sender;
          147        //接收地址
          148        mailMsg.To = Receiver;
          149        //設(shè)置郵件正文內(nèi)容的類型式
          150        mailMsg.BodyFormat = MailFormat.Text;
          151        //郵件主題
          152        mailMsg.Subject = Subject;
          153
          154        // 創(chuàng)建一個附件對象 
          155        //MailAttachment ma = new MailAttachment(f.Value);//f.value附件完整路徑
          156
          157       // mailMsg.Attachments.Add(ma);
          158
          159
          160        //郵件內(nèi)容        
          161        mailMsg.Body =Content;
          162        //服務(wù)器端的ip,因為我們用的是本地的虛擬smtp服務(wù)器,所以只需要填寫本地ip地址
          163        //SmtpMail.SmtpServer = "127.0.0.1";
          164       
          165 
          166    
          167        ///以下三條一般都要加 一般的郵箱服務(wù)器都需要身份驗證
          168
          169        mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate""1");
          170        //設(shè)置驗證用戶名(把userName改為你的驗證用戶名) 
          171        mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", Name);
          172        //設(shè)置驗證密碼(把pwd改為你的驗證密碼)     //發(fā)件人用戶名
          173        mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", Pwd);//郵箱密碼
          174
          175                        
          176        SmtpMail.SmtpServer = server; //可選擇其他服務(wù)器
          177        try
          178        {
          179            SmtpMail.Send(mailMsg);
          180            return true;
          181        }

          182        catch (Exception ex)
          183        {
          184            Response.Write("<script>alert('"+ex.Message+"');</script>");
          185            return false;
          186        }

          187
          188    }

          189    #endregion

          190
          191    #region sendMail5 OpenSmtp   
          192
          193    public bool sendMail5(string Sender, string Receiver, string Name, string Pwd, string Subject, string Content, string server)
          194    {
          195        OpenSmtp.Mail.MailMessage OpMMsg = new OpenSmtp.Mail.MailMessage();
          196
          197        OpenSmtp.Mail.SmtpConfig.VerifyAddresses = false;
          198        OpMMsg.Charset = "gb2312";     
          199        OpMMsg.Priority = OpenSmtp.Mail.MailPriority.High;
          200        OpMMsg.From =new OpenSmtp.Mail.EmailAddress(Sender);
          201        OpMMsg.AddRecipient(Receiver,OpenSmtp.Mail.AddressType.To);
          202        OpMMsg.Subject = Subject;
          203        OpMMsg.Body = Content;
          204
          205
          206        OpenSmtp.Mail.Smtp smtp = new OpenSmtp.Mail.Smtp();
          207
          208        smtp.Host = server;
          209        smtp.Username = Name;
          210        smtp.Password = Pwd;
          211        smtp.Port = 25;
          212        try
          213        {
          214            smtp.SendMail(OpMMsg);
          215            return true;
          216        }

          217        catch
          218        {
          219            return false;
          220        }

          221    }

          222
          223    #endregion
          posted on 2009-09-21 10:59 狂淘 閱讀(816) 評論(0)  編輯  收藏 所屬分類: .net
          主站蜘蛛池模板: 兴城市| 安多县| 壶关县| 乡宁县| 富裕县| 六枝特区| 蒙山县| 固阳县| 昌邑市| 抚远县| 隆安县| 东宁县| 仁布县| 元江| 洛隆县| 乌拉特前旗| 汕头市| 滁州市| 昔阳县| 铅山县| 大名县| 唐河县| 会宁县| 桃江县| 甘德县| 鹰潭市| 张家界市| 嘉黎县| 繁峙县| 宁武县| 普兰店市| 永新县| 庆城县| 云南省| 香港 | 正安县| 西充县| 武隆县| 敦煌市| 哈密市| 四川省|