首页 > 开发 > 综合 > 正文

发邮件、带附件。利用属性升级变量轻松使用Session

2024-07-21 02:26:03
字体:
来源:转载
供稿:网友

后台代码:

//-------------------------------------------------------------------------------------------------------

using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
using system.web.mail;
using system.io;

namespace sendemailmodule
...{
    /**//// <summary>
    /// sendmailresume 的摘要说明。
    /// </summary>
    public class sendmailresume : system.web.ui.page
    ...{
        protected system.web.ui.webcontrols.panel panelmessage;
        protected system.web.ui.webcontrols.panel panelmain;
        protected system.web.ui.webcontrols.requiredfieldvalidator rfvemail;
        protected system.web.ui.webcontrols.textbox txtyouremail;
        protected system.web.ui.webcontrols.textbox txtmessage;
        protected system.web.ui.webcontrols.button btnsubmit;
        protected system.web.ui.webcontrols.regularexpressionvalidator revemail;
        protected system.web.ui.webcontrols.label lblalert;
        protected system.web.ui.webcontrols.button btnuploadok;
        protected system.web.ui.htmlcontrols.htmlinputfile fileupload;
        protected system.web.ui.webcontrols.label lbluploadfileds;
        protected system.web.ui.webcontrols.panel panelerror;
   
        private void page_load(object sender, system.eventargs e)
        ...{
            // first on load
            if(!this.ispostback)           
            ...{               
                this.thearrayfile = new arraylist();               
            }
        }

        private void btnuploadok_click(object sender, system.eventargs e)
        ...{
            string fullpath = this.fileupload.postedfile.filename;
            if(fullpath == "")
                return;           
            string filepostfix = fullpath.substring(fullpath.lastindexof(".") + 1).tolower();
            if(filepostfix == "doc" || filepostfix == "pdf")
                saveresumefile(fullpath);
            else
                return;

            string temp = null;
            for(int i=0; i<thearrayfile.count; i++)
            ...{
                string[] str = this.thearrayfile[i].tostring().split('#');
                temp += string.format(@"{0} | ",str[0]);
            }
            this.lbluploadfileds.text= temp;
        }

        private void btnsubmit_click(object sender, system.eventargs e)
        ...{
                sendemail();
        }   

        web 窗体设计器生成的代码#region web 窗体设计器生成的代码
        override protected void oninit(eventargs e)
        ...{
            //
            // codegen: 该调用是 asp.net web 窗体设计器所必需的。
            //
            initializecomponent();
            base.oninit(e);
        }
       
        /**//// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void initializecomponent()
        ...{   
            this.btnsubmit.click += new system.eventhandler(this.btnsubmit_click);
            this.btnuploadok.click += new system.eventhandler(this.btnuploadok_click);
            this.load += new system.eventhandler(this.page_load);

        }
        #endregion

 

        /**//// <summary>
        /// save resume file
        /// </summary>
        /// <param name="clientfile">clinet file path</param>
        private void saveresumefile(string clientfile)
        ...{
            string fullpath = clientfile;        //this.fileupload.postedfile.filename;
            string filename = fullpath.substring(fullpath.lastindexof("/") + 1);           
            string currentdaytime = system.datetime.now.tostring("yyyy-mm-dd hh:mm:ss").replace(@":","");
            string filesavepath = string.format(@"{0}{1}_{2}",server.mappath("theresumefile"),currentdaytime,filename);
           
            try
            ...{
                this.fileupload.postedfile.saveas(filesavepath);
                string filenameandpath = string.format("{0}#{1}",filename,filesavepath);
                thearrayfile.add(filenameandpath);
            }
            catch(system.exception sende)
            ...{
                response.write(sende.tostring());
            }

           
        }

       
        /**//// <summary>
        /// send email
        /// </summary>
        public void sendemail()
        ...{
            try
            ...{
                string selsubject     = "resume (posted from ssbg website)";       
                string txtyouremail     = this.txtyouremail.text.trim();   
                string txtmessage     = this.txtmessage.text.trim();

                string strfromman     = txtyouremail;
                //string strtoman         =     "[email protected]";        //"[email protected]";
                string strtoman  = system.configuration.configurationsettings.appsettings["emailto"].tostring();
                mailmessage mail=new mailmessage();

                mail.from =strfromman;
                mail.to = strtoman;

                mail.subject = string.format("{0}",selsubject);

                string theshow ="";
                theshow += "<table align=center width='80%'   cellpadding='0' cellspacing='0' bordercolorlight='#6699ff' bordercolordark='#6699ff' style='border-collapse:collapse;border-collapse: collapse;margin: 0px;padding: 0px;' border = '1'>";
                theshow += string.format("<tr><td colspan ='2' align='center'><b>{0}</b></td><tr>",selsubject); 
                theshow += string.format("<tr><td align='left' nowrap width='20%'>{0}</td><td>{1}&nbsp;</td></tr>","message:",txtmessage);
                theshow += "</table>";   
               
                mail.body ="<html><head><link rel='stylesheet' href='' type='text/css'></head><body>" + theshow + "</body></html>";
                mail.bodyformat=mailformat.html;
                try
                ...{// add file
                    for(int i=0; i< this.thearrayfile.count; i++)
                    ...{
                        string[] str = this.thearrayfile[i].tostring().split('#');
                        mailattachment attachment = new mailattachment(str[1]);
                        mail.attachments.add(attachment);
                    }
                }
                catch(system.exception ef)
                ...{
                    response.write(ef.tostring());
                }
                   
                mail.fields.add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
                mail.fields.add("http://schemas.microsoft.com/cdo/configuration/sendusername","[email protected]");//这里填写你邮箱的用户名
                mail.fields.add("http://schemas.microsoft.com/cdo/configuration/sendpassword","ssbgroup");        //你邮箱的密码

                smtpmail.smtpserver= "mail.ssbg.com.cn";
                smtpmail.send(mail);
                this.panelmessage.visible = true;
                this.panelmain.visible = false;
                this.panelerror.visible = false;

                this.deletefiles();
            }
            catch
            ...{
                this.panelerror.visible = true;
                this.panelmain.visible = false;
                this.panelmessage.visible = false;
                   
            }
        }

        private void deletefiles()
        ...{
            fileinfo[]   thegetfiles = operationclass.getfiles(server.mappath("theresumefile"));
            foreach(fileinfo f in thegetfiles)
            ...{
                try
                ...{
                    f.delete();
                }
                catch
                ...{
                    continue;
                }
            }
        }

//--------------
        private arraylist thearrayfile
        ...{
            get...{return session["arrayfile"] as arraylist;}
            set...{session["arrayfile"] = value;}
        }


    }


    public class operationclass
    ...{
        /**////   <summary>  
        ///   get all files  
        ///   </summary>  
        ///   <param   name="fullpath">current dir path</param>  
        ///   <returns></returns>  
        public static   fileinfo[]   getfiles(string fullpath)  
        ...{  
            directoryinfo   curdir   =   new   directoryinfo(fullpath);  
            fileinfo[]   fileinfo   =   curdir.getfiles();  
            return   fileinfo;  
        }
    }
}

//***************************************************************************************

前台代码:

<%@ page language="c#" codebehind="sendmailresume.aspx.cs" autoeventwireup="false" inherits="sendemailmodule.sendmailresume" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
    <head>
        <title>=== send your resume === </title>
        <meta content="microsoft visual studio .net 7.1" name="generator">
        <meta content="c#" name="code_language">
        <meta content="javascript" name="vs_defaultclientscript">
        <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetschema">
        <link href="contactus_emx_nav_left.css" type="text/css" rel="stylesheet">
    </head>
    <body bgcolor="#5887a5">
        <form id="form1" method="post" runat="server">
            <font face="宋体">
                <div align="center">
                    <div align="center">
                        <table id="table2" cellspacing="0" cellpadding="0" width="100%" border="0">
                            <tr>
                                <td valign="top"><font face="宋体"><asp:panel id="panelmain" runat="server">&nbsp;&nbsp;
      <table id="table6" cellspacing="1" cellpadding="1" width="100%" align="center" border="0">
                                                <tr>
                                                    <td>
                                                        <table id="table5" cellspacing="1" cellpadding="1" align="center" bgcolor="#ffffff" border="0">
                                                            <tr>
                                                                <td align="right" colspan="5"><font face="宋体"></font></td>
                                                            </tr>
                                                            <tr>
                                                                <td align="right"><font face="宋体"></font></td>
                                                                <td align="right" colspan="3">
                                                                    <div align="center">
                                                                        <h2><strong >send your resume</strong></h2>
                                                                    </div>
                                                                </td>
                                                                <td align="right"><font face="宋体">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font></td>
                                                            </tr>
                                                            <tr>
                                                                <td align="right" colspan="2">
                                                                    <div align="left"></div>
                                                                </td>
                                                                <td align="right" colspan="2">
                                                                    <div align="left">fields marked by an sterlsk(<span >*</span>)
                                                                        are required.
                                                                    </div>
                                                                </td>
                                                                <td align="right"></td>
                                                            </tr>
                                                            <tr>
                                                                <td class="number" nowrap>
                                                                    <div align="right"></div>
                                                                </td>
                                                                <td class="number" nowrap>
                                                                    <div align="right"><strong>your&nbsp;email:</strong></div>
                                                                </td>
                                                                <td colspan="2">
                                                                    <asp:textbox id="txtyouremail" runat="server" width="232px"></asp:textbox><span >*</span>
                                                                    <asp:requiredfieldvalidator id="rfvemail" runat="server" errormessage="*" controltovalidate="txtyouremail" display="dynamic">input your email!</asp:requiredfieldvalidator>
                                                                    <asp:regularexpressionvalidator id="revemail" runat="server" errormessage="*" controltovalidate="txtyouremail" validationexpression="w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*">input your email!</asp:regularexpressionvalidator></td>
                                                                <td ></td>
                                                            </tr>
                                                            <tr>
                                                                <td class="number" valign="top" nowrap></td>
                                                                <td class="number" valign="top" nowrap>
                                                                    <div align="right"><strong>upload&nbsp;resume:</strong></div>
                                                                </td>
                                                                <td colspan="2"><input id="fileupload" type="file" name="file2" runat="server">&nbsp;
                                                                    <asp:button id="btnuploadok" runat="server" width="66px" height="22px" text="upload"></asp:button><span >*
                                                                        <asp:label id="lblalert" runat="server" width="166px" forecolor="#404040">(.doc | .pdf)</asp:label></span></td>
                                                                <td></td>
                                                            </tr>
                                                            <tr>
                                                                <td class="number" valign="top" nowrap></td>
                                                                <td class="number" valign="top" nowrap></td>
                                                                <td colspan="2">
                                                                    <asp:label id="lbluploadfileds" runat="server" width="100%" height="8px"></asp:label></td>
                                                                <td></td>
                                                            </tr>
                                                            <tr>
                                                                <td class="number" valign="top" nowrap>
                                                                    <div align="right"><font face="宋体"></font></div>
                                                                </td>
                                                                <td class="number" valign="top" nowrap>
                  &nbs, p;                                                 <div align="right"><strong>message:</strong></div>
                                                                </td>
                                                                <td colspan="2">
                                                                    <asp:textbox id="txtmessage" runat="server" width="507px" textmode="multiline" height="113px"></asp:textbox></td>
                                                                <td></td>
                                                            </tr>
                                                            <tr>
                                                                <td class="number" nowrap colspan="2">
                                                                    <div align="right"></div>
                                                                </td>
                                                                <td valign="top" colspan="2">maximum 2000 characters</td>
                                                                <td valign="top"></td>
                                                            </tr>
                                                            <tr>
                                                                <td colspan="2"></td>
                                                                <td colspan="2"></td>
                                                                <td></td>
                                                            </tr>
                                                            <tr>
                                                                <td valign="top" align="center"><font face="宋体"></font></td>
                                                                <td valign="top" align="center">&nbsp;
                                                                </td>
                                                                <td valign="top" align="left">
                                                                    <asp:button id="btnsubmit" runat="server" text="submit"></asp:button>&nbsp;</td>
                                                                <td valign="top" align="center">&nbsp;</td>
                                                                <td valign="top" align="center"></td>
                                                            </tr>
                                                            <tr>
                                                                <td valign="top" align="center"></td>
                                                                <td valign="top" align="center"></td>
                                                                <td valign="top" align="left"></td>
                                                                <td valign="top" align="center"></td>
                                                                <td valign="top" align="center"></td>
                                                            </tr>
                                                        </table>
                                                    </td>
                                                </tr>
                                            </table></asp:panel><asp:panel id="panelmessage" runat="server" backcolor="#5887a5" visible="false">
                                            <table id="table1" cellspacing="1" cellpadding="1" align="center"
                                                bgcolor="#5887a5" border="0">
                                                <tbody>
                                                    <tr >
                                                        <td bgcolor="#cccccc" colspan="2"><font face="宋体">email sent:</font></td>
                                                    </tr>
                                                    <tr>
                                                        <td valign="top" colspan="2">
                                                            <table id="table4" cellspacing="0" cellpadding="0" width="100%" border="0">
                                                                <tr >
                                                                    <td bgcolor="#5887a5">the mail
                                                                        already&nbsp;send to the ssbg mailbox, thank you!</td>
                                                                </tr>
                                                                <tr>
                                                                    <td bgcolor="#5887a5"><font face="宋体"></font></td>
                                                                </tr>
                                                                <tr>
                                                                    <td align="right" bgcolor="#5887a5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                                                        sincerely&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                                                    </td>
                                                                </tr>
                                                                <tr>
                                                                    <td align="right" bgcolor="#5887a5">ssbg - it
                                                                        solutions&nbsp;&nbsp;
                                                                    </td>
                                                                </tr>
                                                                <tr>
                                                                    <td align="right" bgcolor="#5887a5"><a href="#">&nbsp;&nbsp;
                                                                        </a>
                                                                    </td>
                                                                </tr>
                                                            </table></font></td>
                            </tr>
                            <tr >
                                <td colspan="2"><font face="宋体">&nbsp;</font></td>
                            </tr>
                        </table>
                </asp:panel></font><asp:panel id="panelerror" runat="server" backcolor="#5887a5" visible="false">
                <table id="table3" cellspacing="1" cellpadding="1" width="498"
                    align="center" border="0">
                    <tr >
                        <td colspan="2"><font face="宋体">message :</font></td>
                    </tr>
                    <tr >
                        <td colspan="2">the network malfunction, the
                            email transmission defeat, please transmit, thanks!
                        </td>
                    </tr>
                    <tr >
                        <td colspan="2"><font face="宋体">&nbsp;</font></td>
                    </tr>
                </table>
            </asp:panel></td></tr></tbody></table></div></div></font></form>
    </body>
</html>

 


 

上一篇:C# 事件及响应方法

下一篇:C#图片处理

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表