程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> .net密碼找回

.net密碼找回

編輯:C#入門知識

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Net.Mail; 
using System.IO;

/// <summary>
/// sendEmail 的摘要說明
/// </summary>
public static class sendEmail
{
    static sendEmail()
    {
        //
        // TODO: 在此處添加構造函數邏輯
        //
    }


    /// <summary>
    /// 發送郵件程序
    /// </summary>
    /// <param name="from">發送人郵件地址</param>
    /// <param name="fromname">發送人顯示名稱</param>
    /// <param name="to">發送給誰(郵件地址)</param>
    /// <param name="subject">標題</param>
    /// <param name="body">內容</param>
    /// <param name="username">郵件登錄名</param>
    /// <param name="password">郵件密碼</param>
    /// <param name="server">郵件服務器 smtp服務器地址</param>
    /// <param name="fujian">附件</param>
    /// <returns>send ok</returns>
    /// 調用方法 SendMail("[email protected]", "某某人", "[email protected]", "你好", "我測試下郵件", "郵箱登錄名", "郵箱密碼", "smtp.126.com", "");
    /// 
    public static string SendMail(string from, string fromname, string to, string subject, string body, string username, string password, string server, string fujian)
    {
        try
        {
            //郵件發送類
            MailMessage mail = new MailMessage();
            //是誰發送的郵件
            mail.From = new MailAddress(from, fromname);
            //發送給誰
            mail.To.Add(to);
            //標題
            mail.Subject = subject;
            //內容編碼
            mail.BodyEncoding = Encoding.Default;
            //發送優先級
            mail.Priority = MailPriority.High;
            //郵件內容
            mail.Body = body;
            //是否HTML形式發送
            mail.IsBodyHtml = true;
            //附件
            if (fujian.Length > 0)
            {
                mail.Attachments.Add(new Attachment(fujian));
            }
            //郵件服務器和端口
            SmtpClient smtp = new SmtpClient(server, 25);
            smtp.UseDefaultCredentials = true;
            //指定發送方式
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            //指定登錄名和密碼
            smtp.Credentials = new System.Net.NetworkCredential(username, password);

            //mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication 
            //mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", username); //set your username here 
            //mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password); //set your password here

            //超時時間
            smtp.EnableSsl = false;
            smtp.Timeout = 10000;
            smtp.Send(mail);
            return "成功發送請注意查收";
        }
        catch (Exception exp)
        {
            return exp.Message;
        }
    }

    //讀取指定URL地址的HTML,用來以後發送網頁用
    public static string ScreenScrapeHtml(string url)
    {

        //讀取stream並且對於中文頁面防止亂碼
        StreamReader reader = new StreamReader(System.Net.WebRequest.Create(url).GetResponse().GetResponseStream(), System.Text.Encoding.UTF8);
        string str = reader.ReadToEnd();
        reader.Close();
        return str;
    }

    ///   <summary>
    ///   發送郵件
    ///   </summary>
    ///   <param   name= "server "> smtp地址 </param>
    ///   <param   name= "username "> 用戶名 </param>
    ///   <param   name= "password "> 密碼 </param>
    ///   <param   name= "from "> 發信人地址 </param>
    ///   <param   name= "to "> 收信人地址 </param>
    ///   <param   name= "subject "> 郵件標題 </param>
    ///   <param   name= "body "> 郵件正文 </param>
    ///   <param   name= "IsHtml "> 是否是HTML格式的郵件 </param>
    public static string  SendMail(string from, string to, string subject, string body, string server, string username, string password, bool IsHtml)
    {
        try
        {
            //設置SMTP 驗證,端口默認為25,如果需要其他請修改
            SmtpClient mailClient = new SmtpClient(server, 25);


            //指定如何發送電子郵件。
            //Network   電子郵件通過網絡發送到   SMTP   服務器。    
            //PickupDirectoryFromIis   將電子郵件復制到挑選目錄,然後通過本地   Internet   信息服務   (IIS)   傳送。    
            //SpecifiedPickupDirectory 將電子郵件復制到 SmtpClient.PickupDirectoryLocation 屬性指定的目錄,然後由外部應用程序傳送。  

            mailClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;


            //創建郵件對象
            MailMessage mailMessage = new MailMessage(from, to, subject, body);

            //定義郵件正文,主題的編碼方式
            mailMessage.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312");
            mailMessage.SubjectEncoding = System.Text.Encoding.GetEncoding("gb2312");
            //mailMessage.BodyEncoding = Encoding.Default;
            //獲取或者設置一個值,該值表示電子郵件正文是否為HTML
            mailMessage.IsBodyHtml = IsHtml;

            //指定郵件的優先級
            mailMessage.Priority = MailPriority.High;

 

            //發件人身份驗證,否則163   發不了
            //表示當前登陸用戶的默認憑據進行身份驗證,並且包含用戶名密碼
            mailClient.UseDefaultCredentials = true;
            mailClient.Credentials = new System.Net.NetworkCredential(username, password);

            //發送
            mailClient.Send(mailMessage);
            return "發送成功";
        }   
        catch (Exception exp)
        {
            return exp.Message;
        }
    }
      

    //發送plaintxt
    public static void SendText(string from, string to, string subject, string body, string server, string username, string password)
    {
        SendMail(from, to, subject, body, server, username, password, false);
    }

    //發送HTML內容
    public static string  SendHtml(string from, string to, string subject, string body, string server, string username, string password)
    {
    return  SendMail(from, to, subject, body, server, username, password, true);
    }

    //發送制定網頁
    public static string  SendWebUrl(string from, string to, string subject, string server, string username, string password, string url)
    {
        //發送制定網頁
       return  SendHtml(from, to, subject, ScreenScrapeHtml(url), server, username, password);

    }
}

 

 

 

 

後台調用示例:

Response.Write("發送附件:"+sendEmail.SendMail("[email protected]", "某某人", "[email protected]", "你好", "我測試下郵件", "[email protected]", "*********", "smtp.sohu.com", Server.MapPath("~/fujian/wap.doc")));


        Response.Write("電子郵件正文是HTML格式:" + sendEmail.SendMail("[email protected]", "[email protected]", "主題", "正文<br/>又一行", "smtp.sohu.com", "shaxiaozier", "*********", false));


        Response.Write("電子郵件正文非HTML格式:" + sendEmail.SendMail("[email protected]", "[email protected]", "主題", "正文<br/>又一行", "smtp.sohu.com", "shaxiaozier", "*********", true));


        Response.Write("發送制定網頁:"+sendEmail.SendWebUrl("[email protected]", "[email protected]", "zhuti", "smtp.sohu.com", "shaxiaozier", "*********", "http://www.baidu.com"));



原文鏈接:http://blog.csdn.net/mypc2010/article/details/7836961

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved