欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-07-02 18:35:29
一。发送电子邮件 用的dll是微软自带的,觉得挺好用的!!    public class simpleemailhelper   ...

一。发送电子邮件
用的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 按钮,如下:
 
<: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" />
            </asp:commandfield>
            <asp:hyperlinkfield text="提交作业" datanavigateurlformatstring="submitbusywork.aspx?course={0}&amp;time={1}" datanavigateurlfields="课程名称,作业次数">
                <itemstyle horizontalalign="center" />
            </asp:hyperlinkfield>
        </columns>
    </asp:gridview>
 
 然后,就可以了啦!~
三。div的绝对居中
很多时候,我们需要在中让一个p居中进行显示,而不会受到滚动条的影响,那怎么才能得到效果?其实,很简单,你需要理解下面这段就可以了。
最前端开人郁闷的就是浏览器的兼容性问题,所以在下面的代码中通过各浏览器的特有属性,来进行判断浏览器的类型。
比如说,self.pageyoffset 如果它为true的话,那么它说明在ie9中起作用,也说明了这个属性在ie9中是独一无二的。
直接看代码:
 
<script type='text/javascript'>
        function mypopuprelocate() {
            var scrolledx, scrolledy;
            if (self.pageyoffset) {//ie9 起作用
                scrolledx = self.pagexoffset;
                scrolledy = self.pageyoffset;
                alert("self.pageyoffset");
            } else if (document.documentelement && document.documentelement.scrolltop) {// ie 6 ,360浏览器等起作用
                scrolledx = document.documentelement.scrollleft;
                scrolledy = document.documentelement.scrolltop;
                alert("document.documentelement && document.documentelement.scrolltop");
            } else if (document.body) {//chrome... ie9 firfox....ie 5.5起作用
                scrolledx = document.body.scrollleft;
                scrolledy = document.body.scrolltop;
                alert("document.body");
            }
           //以上是浏览器滚动的距离
           // alert("scrolledx:" + scrolledx);
           // alert("scrolledy:" + scrolledy);

            var centerx, centery;
            if (self.innerheight) {
                centerx = self.innerwidth;
                centery = self.innerheight;
            } else if (document.documentelement && document.documentelement.clientheight) {
                centerx = document.documentelement.clientwidth;
                centery = document.documentelement.clientheight;

            } else if (document.body) {
                centerx = document.body.clientwidth;
                centery = document.body.clientheight;
            }

            alert("centerx:" + centerx);
            alert("centery:" + centery);

            var leftoffset = scrolledx + (centerx - 250) / 2;
            var topoffset = scrolledy + (centery - 200) / 2;
            document.getelementbyid("mypopup").style.top = topoffset + "px";
            document.getelementbyid("mypopup").style.left = leftoffset + "px";
        }
        function firemypopup() {
            mypopuprelocate();
            document.getelementbyid("mypopup").style.display = "block";
            //            document.body.onscroll = mypopuprelocate;
            //            window.onscroll = mypopuprelocate;
        }
    </script>
 
html code:
 
<p id='mypopup' name='mypopup' style='position: absolute; width: 250px; height: 200px;
        display: none; background: #ddd; border: 1px solid #000; z-index: 100'>
        <p>
            我现在的位置是居中状态<br>
            </p>
        <input type='submit' value=' 关闭窗口! (2) ' onclick='document.getelementbyid("mypopup").style.display="none"'>
    </p>
    <input type='submit' value=' fire! (2) ' onclick='firemypopup()'>
 
这就能得到在各个浏览器中绝对居中了。当然还有其他的方法,比如说 用css,也ok。
特殊情况:
如果,你不需要居中肿么办呢?很简单啊,你需要改的仅仅是下面这句话:
var leftoffset = scrolledx + (centerx - 250) / 2;
var topoffset = scrolledy + (centery - 200) / 2;
比如说,你现在把当前的p放到top 100px,left 100px,就需要下面操作:
var leftoffset = scrolledx + 100;
var topoffset = scrolledy + 100;

好了,就先说这么这三个把!。祝大家 五一快乐哦!!~~

 

摘自 浪迹天涯