服务器认证发送邮件
参数说明
Subject:邮件标题
ToAddress:目标邮箱
FromAddress:发送人邮箱
Body:邮件内容
public static void SendMail(String Subject,String ToAddress,String FromAddress,String Body)
{
MailMessage mail = new MailMessage();
mail.To = ToAddress;
mail.From = FromAddress;
mail.Subject = Subject;
mail.Body = Body;
//以下内容为服务器认证语句
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
//用户名
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "UserID");
//邮箱密码
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "Password");
//写入smtp服务器名字
SmtpMail.SmtpServer = "smtp.163.com";
SmtpMail.Send( mail );
}
上传图片并生成略缩图
参数说明
FileBox:上传表单中的表单名
SavePath:保存路径
ThumbnailWidth:略缩图宽
ThumbnailHeight:略缩图高
FilePath:文件保存路径(输出参数)
FileName:文件名称(输出参数)
LastError:(输出错误)
public static bool UploadImages(System.Web.UI.HtmlControls.HtmlInputFile FileBox,string SavePath,Int32 ThumbnailWidth,Int32 ThumbnailHeight,out String FilePath,out String FileName,out string LastError)
{
DateTime datTime=System.DateTime.Now;
String strTemp;
string strFileName = FileBox.PostedFile.FileName;
bool blnResult;
LastError="";
System.Drawing.Image xImage;
System.Drawing.Bitmap xBitmap;
int PhotoHeight,PhotoWidth;
string BasePath=System.Web.HttpContext.Current.Server.MapPath("../");
Rectangle NewPhoto;
System.Drawing.Imaging.ImageFormat xObject;
string Extension="";
FileName="";
FilePath="";
try
{
if(strFileName=="")
{
LastError="请点击浏览选择要上传的文件!";
return false;
}
if(Convert.ToDecimal(FileBox.PostedFile.ContentLength)<= 2000000 &&Convert.ToDecimal(FileBox.PostedFile.ContentLength)>1500)
{
//查看扩展名是否合法
Extension=Path.GetExtension(strFileName);
Extension=Extension.ToLower();
if(Extension==".gif" || Extension==".jpg" || Extension==".jpeg" || Extension==".bmp")
{
switch(Extension)
{
case ".gif":
xObject=System.Drawing.Imaging.ImageFormat.Gif;
break;
case ".bmp":
xObject=System.Drawing.Imaging.ImageFormat.Bmp;
break;
default:
xObject=System.Drawing.Imaging.ImageFormat.Jpeg;
break;
}
}
else
{
LastError="上传的文件不是有效的图片文件,请上传格式为:.gif.jpg.bmp的文件!";
return false;
}
//-----------------------------
//生成保存的文件路径及文件名
//-----------------------------
strTemp = datTime.ToShortDateString().Replace( "-", "");
FileName = datTime.ToLongTimeString().Replace(":","")+ Extension;
FilePath = SavePath + strTemp + "/";
Friendlib.Function.CreateDirectory(BasePath + FilePath);
//保存大图
xImage=System.Drawing.Bitmap.FromStream(FileBox.PostedFile.InputStream);
xImage.Save(BasePath+FilePath+FileName);
xImage.Dispose();
//-----------------------------
//开始生成缩略图
//-----------------------------
xBitmap=new Bitmap(BasePath+FilePath+FileName);//------------------
PhotoHeight=xBitmap.Height;
PhotoWidth=xBitmap.Width;
// //判断图片的形状
if(Convert.ToDecimal(PhotoHeight)/Convert.ToDecimal(PhotoWidth)>Convert.ToDecimal(ThumbnailHeight)/Convert.ToDecimal(ThumbnailWidth))//高有余
{
PhotoHeight=Convert.ToInt16((Convert.ToDecimal(ThumbnailHeight)/Convert.ToDecimal(ThumbnailWidth))*Convert.ToDecimal(PhotoWidth));
NewPhoto=new Rectangle(0,0,PhotoWidth,PhotoHeight);
}
else if(Convert.ToDecimal(PhotoHeight)/Convert.ToDecimal(PhotoWidth)<Convert.ToDecimal(ThumbnailHeight)/Convert.ToDecimal(ThumbnailWidth))
{
PhotoWidth=Convert.ToInt16((Convert.ToDecimal(ThumbnailWidth)/Convert.ToDecimal(ThumbnailHeight))*Convert.ToDecimal(PhotoHeight));
NewPhoto=new
Rectangle(Convert.ToInt16((Convert.ToDecimal(xBitmap.Width)-Convert.ToDecimal(PhotoWidth))/2),0,PhotoWidth,PhotoHeight);
}
else
{
NewPhoto= newRectangle(0,0,PhotoWidth,PhotoHeight); < BR> }
System.Drawing.Image myBitmap;
myBitmap=xBitmap.Clone(NewPhoto,System.Drawing.Imaging.PixelFormat.DontCare);
System.Drawing.Image.GetThumbnailImageAbort myCallback = new
System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
System.Drawing.Image myThumbnail =
myBitmap.GetThumbnailImage(ThumbnailWidth,ThumbnailHeight, myCallback,
IntPtr.Zero); myThumbnail.Save(BasePath + FilePath +"s"+FileName,xObject);
myThumbnail.Dispose(); myBitmap.Dispose(); xBitmap.Dispose(); return true; }
else
{
blnResult=false; LastError="不能上传大于2M及小于1.5K的图片!";
}
}< BR> catch (Exception ex) {
throw new System.Exception((ex.Message + ("/r/n" + ex.StackTrace)));
}
return blnResult;
}
private static bool ThumbnailCallback() { return false; }
参数说明
FileBox:表单名称
BasePath:文件基路径
FileSize:文件大小
LastError:错误信息
Extension:文件扩展名
FilePath:文件保存路径
FileName:文件保存名称
Name:文件原名称
public static bool UploadMyFiles(System.Web.UI.HtmlControls.HtmlInputFile FileBox,string BasalPath,String SavePath,ref decimal FileSize,out string LastError,out String Extension ,out String FilePath,out String FileName,out string Name)
{
DateTime datTime=System.DateTime.Now;
String strTemp;
string strFileName = FileBox.PostedFile.FileName;
bool blnResult;
LastError="";
Name="";
FilePath="";
FileName="";
Extension="";
try
{
if (strFileName=="")
{
LastError="请点击浏览选择要上传的文件!";
return false;
}
if(Convert.ToDecimal(FileBox.PostedFile.ContentLength)<=FileSize*1048576)
{
FileSize = FileBox.PostedFile.ContentLength/1024;
CStat xStat = new CStat();
xStat.UserID = System.Web.HttpContext.Current.Session["CurrentUserID"].ToString();
if(xStat.Load())
{
bool sessionVIP;
if(System.Web.HttpContext.Current.Session["BankVIP"] == null)
{
sessionVIP = false;
}
else
{
sessionVIP = true;
}
if(xStat.FilesSize+FileSize>=Function.FeeOrFreeFile(sessionVIP))
{
System.Web.HttpContext.Current.Response.Redirect("~/blog/NotEnoughFile.htm");
}
}
Name=Path.GetFileName(strFileName);
Extension=Path.GetExtension(strFileName);
Extension=Extension.ToLower();
// if(true)//文件符合要求
// {
strTemp = datTime.ToShortDateString().Replace( "-", "");
FileName = strTemp + datTime.ToLongTimeString().Replace(":","")+datTime.Millisecond+ Extension;
FilePath = SavePath + strTemp + "/";
CreateDirectory(BasalPath + FilePath);
FileBox.PostedFile.SaveAs(BasalPath + FilePath + FileName);
blnResult=true;
}
else
{
blnResult=false;
LastError="上传的文件大小应在"+(FileSize).ToString()+"M以内!";
}
}
catch (Exception ex)
{
throw new System.Exception((ex.Message
+ ("/r/n" + ex.StackTrace)));
}
return blnResult;
}
参数说明
AbsPath:文件绝对路径
FileName:文件原名
public static void DownloadFile(string AbsPath,string FileName)
{
FileStream fs = new FileStream(AbsPath, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename="+FileName);
// System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;FileName");
System.Web.HttpContext.Current.Response.Charset = "GB2312";
System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
System.Web.HttpContext.Current.Response.BinaryWrite(r.ReadBytes(Convert.ToInt32(fs.Length)));
System.Web.HttpContext.Current.Response.Flush() ;
}