using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Net.Mail;
using System.IO;
using System.Net.Mime;
namespace sn.Common
{
public class Comm
{
#region 读取Web.config值
public static string GetConfigByKey(string key)
{
return ConfigurationSettings.AppSettings[key];
}
#endregion
#region 电子邮件
/// <summary>
/// 判断附件容量
/// </summary>
/// <param name="file">文件地址 </param>
/// /// <param name="MaxSize">最大容量,单位:M </param>
/// <returns> </returns>
public static Attachment GetAttachment(string file,int MaxSize)
{
if (!File.Exists(file))
{
System.Web.HttpContext.Current.Response.Write(" <script>alert('" + file + "文件不存在!') </script>");
System.Web.HttpContext.Current.Response.End();
return null;
}
int size = 0;
FileStream fs = new FileStream(file, FileMode.Open);
string name = fs.Name;
size = (int)(fs.Length / 1024);
fs.Close();
//控制文件大小不大于10M
if (size > 1024*MaxSize)
{
System.Web.HttpContext.Current.Response.Write(" <script>alert('附件容量过大!') </script>");
System.Web.HttpContext.Current.Response.End();
return null;
}
Attachment att = new Attachment(file, MediaTypeNames.Application.Octet);
ContentDisposition disposition = att.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
return att;
}
/// <summary>
/// 判断附件容量
/// </summary>
/// <param name="file">文件地址 </param>
/// <returns> </returns>
public static Attachment GetAttachment(string file)
{
return GetAttachment(file,10);
}
/// <summary>
/// 发送EMAIL
/// </summary>
/// <param name="from">发件人地址 </param>
/// <param name="password">密码 </param>
/// <param name="DisPalyName">对方收到时显示的名称 </param>
/// <param name="to">收件人 </param>
/// <param name="cc">抄送:默认为空 </param>
/// <param name="subject">主题 </param>
/// <param name="ssl">经过ssl加密 </param>
/// <param name="priority">邮件优先级:MailPriority.High </param>
/// <param name="isbodyhtml">是否HTML邮件 </param>
/// <param name="attachment">附件:没有则为null </param>
/// <param name="body">正文 </param>
/// <returns>返回值:如果为空则成功,否则失败 </returns>
public static string SendEMail(string from, string password, string DisPalyName, string to, string cc, string subject, bool ssl, MailPriority priority, bool isbodyhtml, Attachment attachment, string body)
{
MailMessage mm = new MailMessage();
mm.From = new MailAddress(from, DisPalyName, Encoding.UTF8);
mm.To.Add(to);
mm.SubjectEncoding = Encoding.UTF8;
mm.Subject = subject;
if (attachment != null)
mm.Attachments.Add(attachment);
if (cc != null && cc != "")
mm.CC.Add(cc); //抄送
mm.IsBodyHtml = isbodyhtml; //是否是HTML邮件
mm.BodyEncoding = Encoding.UTF8; //编码
mm.Priority = priority; // MailPriority.High; //优先级
mm.Body = body;
SmtpClient sc = new SmtpClient();
sc.Credentials = new System.Net.NetworkCredential(from, password);
//sc.Port = 25;
sc.Host = "smtp." + from.Substring(from.IndexOf('@') + 1);
try
{
sc.Send(mm);
return "";
}
catch (Exception ee)
{
return ee.ToString();
}
}
/// <summary>
/// 发送EMAIL
/// </summary>
/// <param name="from">发件人地址 </param>
/// <param name="password">密码 </param>
/// <param name="DisPalyName">对方收到时显示的名称 </param>
/// <param name="to">收件人 </param>
/// <param name="cc">抄送:默认为空 </param>
/// <param name="subject">主题 </param>
/// <param name="attachment">附件 </param>
/// <param name="body">正文 </param>
/// <returns>返回值:如果为空则成功,否则失败 </returns>
public static string SendEMail(string from, string password, string DisPalyName, string to, string cc, string subject,Attachment attachment, string body)
{
return SendEMail(from,password,DisPalyName,to,cc,subject,false,MailPriority.High,false,attachment,body);
}
#endregion
}
}
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Net.Mail;
using System.IO;
using System.Net.Mime;
namespace sn.Common
{
public class Comm
{
#region 读取Web.config值
public static string GetConfigByKey(string key)
{
return ConfigurationSettings.AppSettings[key];
}
#endregion
#region 电子邮件
/// <summary>
/// 判断附件容量
/// </summary>
/// <param name="file">文件地址 </param>
/// /// <param name="MaxSize">最大容量,单位:M </param>
/// <returns> </returns>
public static Attachment GetAttachment(string file,int MaxSize)
{
if (!File.Exists(file))
{
System.Web.HttpContext.Current.Response.Write(" <script>alert('" + file + "文件不存在!') </script>");
System.Web.HttpContext.Current.Response.End();
return null;
}
int size = 0;
FileStream fs = new FileStream(file, FileMode.Open);
string name = fs.Name;
size = (int)(fs.Length / 1024);
fs.Close();
//控制文件大小不大于10M
if (size > 1024*MaxSize)
{
System.Web.HttpContext.Current.Response.Write(" <script>alert('附件容量过大!') </script>");
System.Web.HttpContext.Current.Response.End();
return null;
}
Attachment att = new Attachment(file, MediaTypeNames.Application.Octet);
ContentDisposition disposition = att.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
return att;
}
/// <summary>
/// 判断附件容量
/// </summary>
/// <param name="file">文件地址 </param>
/// <returns> </returns>
public static Attachment GetAttachment(string file)
{
return GetAttachment(file,10);
}
/// <summary>
/// 发送EMAIL
/// </summary>
/// <param name="from">发件人地址 </param>
/// <param name="password">密码 </param>
/// <param name="DisPalyName">对方收到时显示的名称 </param>
/// <param name="to">收件人 </param>
/// <param name="cc">抄送:默认为空 </param>
/// <param name="subject">主题 </param>
/// <param name="ssl">经过ssl加密 </param>
/// <param name="priority">邮件优先级:MailPriority.High </param>
/// <param name="isbodyhtml">是否HTML邮件 </param>
/// <param name="attachment">附件:没有则为null </param>
/// <param name="body">正文 </param>
/// <returns>返回值:如果为空则成功,否则失败 </returns>
public static string SendEMail(string from, string password, string DisPalyName, string to, string cc, string subject, bool ssl, MailPriority priority, bool isbodyhtml, Attachment attachment, string body)
{
MailMessage mm = new MailMessage();
mm.From = new MailAddress(from, DisPalyName, Encoding.UTF8);
mm.To.Add(to);
mm.SubjectEncoding = Encoding.UTF8;
mm.Subject = subject;
if (attachment != null)
mm.Attachments.Add(attachment);
if (cc != null && cc != "")
mm.CC.Add(cc); //抄送
mm.IsBodyHtml = isbodyhtml; //是否是HTML邮件
mm.BodyEncoding = Encoding.UTF8; //编码
mm.Priority = priority; // MailPriority.High; //优先级
mm.Body = body;
SmtpClient sc = new SmtpClient();
sc.Credentials = new System.Net.NetworkCredential(from, password);
//sc.Port = 25;
sc.Host = "smtp." + from.Substring(from.IndexOf('@') + 1);
try
{
sc.Send(mm);
return "";
}
catch (Exception ee)
{
return ee.ToString();
}
}
/// <summary>
/// 发送EMAIL
/// </summary>
/// <param name="from">发件人地址 </param>
/// <param name="password">密码 </param>
/// <param name="DisPalyName">对方收到时显示的名称 </param>
/// <param name="to">收件人 </param>
/// <param name="cc">抄送:默认为空 </param>
/// <param name="subject">主题 </param>
/// <param name="attachment">附件 </param>
/// <param name="body">正文 </param>
/// <returns>返回值:如果为空则成功,否则失败 </returns>
public static string SendEMail(string from, string password, string DisPalyName, string to, string cc, string subject,Attachment attachment, string body)
{
return SendEMail(from,password,DisPalyName,to,cc,subject,false,MailPriority.High,false,attachment,body);
}
#endregion
}
}