这个是我写的文件上传和绑定mymsg的程序
System.Web.Mail.MailMessage mymsg=new MailMessage();
mymsg.From=txtfrom.Text;
mymsg.To=txtto.Text;
mymsg.Subject=txtsubject.Text;
mymsg.Body=txtbody.Text;
HttpPostedFile a=File1.PostedFile;
           if(a.FileName!="")
{
char[] de={'\\'};
string[] Afilename=a.FileName.Split(de);
string strFilename=Afilename[Afilename.Length-1];
string strPath="c:"+"\\Temp\\"+strFilename;
a.SaveAs(strPath);
mymsg.Attachments.Add(new MailAttachment(strPath));
              }这个我发送的程序:            try
{
SmtpMail.SmtpServer="127.0.0.1";
SmtpMail.Send(mymsg);
show.Text="邮件发送成功";
}
catch(Exception)
{
show.Text="邮件发送失败";
}
我把这2个程序写在一个button击发事件里能发送成功但我尝试是将上传和发送分别弄到2个button的击发事件结果就发送不成功。请问在分开写的时候要注意哪些问题啊?

解决方案 »

  1.   

    这个可能是System.Web.Mail.MailMessage mymsg=new MailMessage();在第二个按钮单击时被初始化的原因,第二个按钮单击时,mymsg变量将被初始化(清空,放别的类型的变量也是一样,这是Web控件单击后页面刷新,所有非static变量被初始化.可以在第一个button那里,将赋好值的mymsg放到Session中,Session["mymsg"]=mymsg;
    在第二个button里System.Web.Mail.MailMessage mymsg1=(System.Web.Mail.MailMessage)Session["mymsg"];
      

  2.   

    http://www.cnblogs.com/jzywh/archive/2005/06/09/170783.html