服务器认证发送邮件 上传图片并生成略缩图
2006-08-25 13:41
453 查看
服务器认证发送邮件
参数说明
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; }
参数说明
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; }
相关文章推荐
- 服务器认证发送邮件 上传图片并生成略缩图
- 上传图片生成略缩图“最佳解决”
- 微信公众平台开发[10]-开发模式-高级功能-将图片、视频、音频I内容上传到微信服务器并发送给用户
- Java学习日记8:文件上传工具类的实现和服务器给客户端发送图片
- 1服务器上传图片 2另存为服务器上文件 3生成验证码图片
- phpcms 上传图片服务器安全认证错误
- 上传图片生成略缩图“最佳解决”
- 上传图片并生成略缩图
- [原]as3 flash web 应用 (8)图片批量上传之 向服务器发送和获取额外数据
- PYTHON发送邮件时,有的服务器不用密码认证的
- 服务器认证发送邮件
- Spring MVC上传图片,Java二进制图片写入数据库,生成略缩图
- ASIFormDataRequest/ASIHTTPRequest 向服务器发送数据/上传图片 返回网页错误数据
- 重提:上传图片生成略缩图“最佳解决”
- 重提:上传图片生成略缩图“最佳解决”
- 多选本地相册中的图片并展示(仿QQ空间动态发送页面)上传至服务器
- 强大的java编程实现邮件发送功能,支持内容,上传多个图片和多个附件
- PHPCMS V9上传附件图片出现“服务器安全认证错误”解决方法
- 上传图片生成略缩图“最佳解决”
- PHPCMS V9上传附件图片出现“服务器安全认证错误”解决方法