当前位置:编程学习 > asp >>

分享3段平时很实用的微代码

一。发送电子邮件
用的dll是微软自带的,觉得挺好用的!!
 
 public class SimpleEmailHelper
    {
        private string _SmtpAdd;
        private string _UserID;
        private string _UserPsw;

        public SimpleEmailHelper(string smtpAddress, string userID, string userPsw)
        {
            _SmtpAdd = smtpAddress;
            _UserID = userID;
            _UserPsw = userPsw;
        }

        public bool Send(string from, string to, string subject, string message,string cc)
        {
            return Send(from, from, to, to, subject, message,cc);
        }

        public bool Send(string from, string fromDisplay, string sendTo, string sendToDisplay,string subject, string message,string cc)
        {

            bool ret = true;

            SmtpClient client = new SmtpClient();
            client.Host = _SmtpAdd;//邮件服务器 比如 网易的是 smtp.163.COM
            client.Port = 25;//端口号,也可不写
            client.DeliveryMethod = SmtpDeliveryMethod.Network;//发送方式
            client.Credentials = new NetworkCredential(_UserID, _UserPsw);//用户名和密码

            MailMessage myMessage = new MailMessage();
            myMessage.Priority = MailPriority.Normal;//优先级
            myMessage.From = new MailAddress(from, fromDisplay, Encoding.GetEncoding("gb2312"));
            myMessage.To.Add(sendTo);
            if (cc != "")
            {
                myMessage.CC.Add(cc);
            }
            myMessage.Subject = subject;//邮件主题
            myMessage.SubjectEncoding = Encoding.GetEncoding("gb2312");
            myMessage.IsBodyHtml = true;
            myMessage.BodyEncoding = Encoding.GetEncoding("gb2312");
            myMessage.Body = message;//正文
            myMessage.Attachments.Add(new Attachment(@"C:\Users\lando\Desktop\Flex问题集结号.txt"));//加入附件。。。
            client.Send(myMessage);//开始发送。
            return ret;
        }
    }
 
页面调用:
SQ.FrameWork.SimpleEmailHelper emailHelper = new SQ.FrameWork.SimpleEmailHelper(stmpServerIpAddress, userId, psw);
emailHelper.Send(from, distEmailAddress, TextBoxTopic.Text.Trim(),TextBoxContent.Text.Trim(),txtCCCleint.Text);
ShowMessage("邮件发送成功。");
需要注意一下的是:
stmpServerIpAddress:是收邮件的服务器地址,比如我用网易的,那么就是 smtp.163.com  等等
userId:你发电子邮件的用户名
psw:你发电子邮件的密码
from:发送人姓名
distEmailAddress:收件人列表,可以有多个,用逗号分隔开来。。都很好理解!~。
二。下载word文档
这个很常用吧,这是我刚刚在给一个实习生改毕业论文的时候,遇到的,所以就记下来了!~以飨园友们哦!~。
 
 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string courseName = ((Label)GridView1.Rows[e.RowIndex].Cells[1].FindControl("Label1")).Text.ToString();//在GridView中文件名字
        string time = ((Label)GridView1.Rows[e.RowIndex].Cells[2].FindControl("Label2")).Text.ToString();//在GridView中找时间
        string tempPath = BusyworkManage.Path + tm.ReturnTeacherID(Request.Cookies["StudentID"].Value.ToString()) +BusyworkManage.TopicPath +
courseName + "/" + courseName + "_" + time + ".doc";//这样做是为了不使下载后的文件的名字重复!~~。
        string path = Server.MapPath(tempPath);
        FileInfo fInfo = new FileInfo(path);
        string fname = fInfo.Name;
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fname));
        Response.AddHeader("Content-Length", fInfo.Length.ToString());
        Response.ContentType = "application/octet-stream";
        Response.WriteFile(fInfo.FullName);
        Response.Flush();
 
首先需要说明的是,在gridview控件中放一个button 按钮,如下:
 
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Height="139px"
        Width="100%" OnRowDeleting="GridView1_RowDeleting"
            onselectedindexchanged="GridView1_SelectedIndexChanged">
        <Columns>
        。。。。。。省略代码。。。。。
            <asp:BoundField DataField="成绩" HeaderText="成绩">
                <ItemStyle HorizontalAlign="Center" />
            </asp:BoundField>
            <asp:CommandField ButtonType="Button" DeleteText="下作业载" ShowDeleteButton="True">
                <ItemStyle HorizontalAlign="Center" />
            </a

补充:Web开发 , ASP.Net ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,