Walkthrough: 应用EventHandle开发的一个发邮件的小项目

项目背景:Exchange的邮件服务器,设定了所有的用户均不能发送外部邮件项目目的:建一个列表,由用户提交数据,填写:“收件人地址”、“邮件内容”、“标题”、附件,然后利用MOSS自带的审批流程来批准邮件的发出。具体代码如下:public override void ItemUpdated(SPItemEventProperties properties)
            {
                //获取触发事件的列表项
                SPSite site = new SPSite(properties.SiteId);
                SPWeb web = site.OpenWeb(properties.OpenWeb().ID);
                SPList list = web.Lists[properties.ListId];
                SPListItem item = list.Items.GetItemById(properties.ListItemId);
                //获取列表项的创建者               
                string strUserValue = Convert.ToString(item["Author"]);
                int intIndex = strUserValue.IndexOf(';');
                int intID = Int32.Parse(strUserValue.Substring(0, intIndex));
                SPUser muser = web.SiteUsers.GetByID(intID);
                //获取审批信息
                SPModerationInformation myMI = item.ModerationInformation;
                string getstate = myMI.Status.ToString();
                if (getstate == "Approved")
                {
                    //设置发邮件的组件
                    MailMessage mail = new MailMessage("[email protected]", Convert.ToString(item["收件人地址"]));
                    mail.SubjectEncoding = Encoding.UTF8;
                    //邮件主题
                    mail.Subject = Convert.ToString(item["标题"]);
                    mail.IsBodyHtml = false; //是否允许内容为 HTML 格式
                    mail.BodyEncoding = Encoding.UTF8;
                    //邮件内容
                    mail.Body = Convert.ToString(item["邮件内容"]);
                    //邮件的回复地址,用户不能发但能收
                    mail.ReplyTo = new MailAddress(muser.Email, muser.Name);
                    mail.From = new MailAddress("[email protected]", muser.Email);                    //to和cc需要用Add方法添加Email地址
                    //添加附件
                    for (int i = 0; i < item.Attachments.Count; i++)
                    {
                        string strPath = item.Attachments.UrlPrefix + item.Attachments[i].ToString(); //要发送附件的网址
                        string strFileName = strPath.Substring(strPath.LastIndexOf("/") + 1); ;   //文件名
                        WebClient wc = new WebClient();
                        MemoryStream m = new MemoryStream();
                        wc.UseDefaultCredentials = true;
                        byte[] fs = wc.DownloadData(strPath);
                        m = new MemoryStream(fs);  //将文件抓取下来,存于内存中.
                        Attachment ma = new Attachment(m, strFileName);
                        mail.Attachments.Add(ma);
                    }
                    //设置验证信息
                    SmtpClient smtp = new SmtpClient("mailserver");
                    smtp.Credentials = new NetworkCredential("username", "password"); //SMTP 验证
                    smtp.Send(mail);
                    mail.Attachments.Dispose(); //邮件发送完毕,释放对附件的锁定
                    sentinform(mail.Subject, muser.Email, Convert.ToString(item["收件人地址"]));
                }
            }
           
            //发送通知邮件
            void sentinform(string substr,string soueml,string tareml)
            {
                MailMessage sentok = new MailMessage("[email protected]", soueml);
                sentok.SubjectEncoding = Encoding.UTF8;
                sentok.Subject = "ANN:您提交的发送外网邮件已发送成功";
                sentok.Body = "已发送的邮件主题是:" + substr + "  收件人地址是:" + tareml;
                SmtpClient smtp1 = new SmtpClient("mailserver");
                smtp1.Credentials = new NetworkCredential("username", "password"); //SMTP 验证
                smtp1.Send(sentok);
            }  今天发现了新的方法就是在web.config中设置SMTP服务器的验证信息,这样就不用为每个SmtpClient对象设置验证信息了设置实例如下:  <system.net>    <mailSettings>      <smtp from="[email protected]">        <network host="smtpserver1" port="25" userName="username" password="secret" defaultCredentials="true" />      </smtp>    </mailSettings>  </system.net>

你可能感兴趣的:(event)