/// 附件信息 /// public struct AttachmentInfo { /// /// 附件的文件名 [如果输入路径,则自动转换为文件名] /// public string FileName { get { return fileName; } set { fileName = Path.GetFileName(value); } } private string fileName;
/// /// 附件的内容 [由经Base64编码的字节组成] /// public string Bytes { get { return bytes; } set { if (value != bytes) bytes = value; } } private string bytes;
/// /// 从流中读取附件内容并构造 /// /// 附件的文件名 /// 流 public AttachmentInfo (string ifileName, Stream stream) { fileName = Path.GetFileName (ifileName); byte[] by = new byte [stream.Length]; stream.Read (by,0,(int)stream.Length); // 读取文件内容 //格式转换 bytes = Convert.ToBase64String (by); // 转化为base64编码 }
/// /// 按照给定的字节构造附件 /// /// 附件的文件名 /// 附件的内容 [字节] public AttachmentInfo (string ifileName, byte[] ibytes) { fileName = Path.GetFileName (ifileName); bytes = Convert.ToBase64String (ibytes); // 转化为base64编码 }
/// /// 从文件载入并构造 /// /// public AttachmentInfo (string path) { fileName = Path.GetFileName (path); FileStream file = new FileStream (path, FileMode.Open); byte[] by = new byte [file.Length]; file.Read (by,0,(int)file.Length); // 读取文件内容 //格式转换 bytes = Convert.ToBase64String (by); // 转化为base64编码 file.Close (); } } } }
--------------------------------------------------------------------------------
// 使用:
MailSender ms = new MailSender (); ms.From = "jovenc@tom.com"; ms.To = "jovenc@citiz.net"; ms.Subject = "Subject"; ms.Body = "body text"; ms.UserName = "########"; // 怎么能告诉你呢 ms.Password = "********"; // 怎么能告诉你呢 ms.Server = "smtp.tom.com";
ms.Attachments.Add (new MailSender.AttachmentInfo (@"D:\test.txt"));
Console.WriteLine ("mail sending...");
上一篇:使用ASP.NET开发邮件发送系统
下一篇:基于.NET的邮件解决方案
|