最近碰到一個發送郵件附件的問題,隨便幾下來,方便以後學習。 

     string[] files = System.IO.Directory.GetFiles(@"~/UploadData" + "/" + DateTime.Now.ToString("yyyyMM"));  //獲取文件路徑 
                foreach (string file in files)  //循環文件夾裏面文件個個數
                {
                    //string extension = System.IO.Path.GetExtension(file); 文件擴展名
                      var attachmentMail = new System.Net.Mail.Attachment(@file);   
                      message.Attachments.Add(attachmentMail);
                }

    smtp.Send(message);   //發送郵件
                ret.success = true;
                ret.message = "发送成功";

CODE:

   /// <summary>
/// 发送邮件
/// </summary>
/// <param name="subject">邮件标题</param>
/// <param name="body">内容</param>
/// <param name="tos">接收者 格式 test@test.com 或 姓名:test@test.com</param>
/// <param name="ccs">抄送者 格式 test@test.com 或 姓名:test@test.com</param>
/// <param name="bccs">密送者 格式 test@test.com 或 姓名:test@test.com</param>
/// <returns></returns>
public static OA.Common.Result SendMail(string subject, string body, string[] tos, string[] ccs, string[] bccs)
{
OA.Common.Result ret = new OA.Common.Result();
/* try
{*/
var config = new SystemConfig().LoadConfig().Mail;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(config.SMTPServer, config.SMTPPort);
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(config.UserName, config.Password);
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.BodyEncoding = System.Text.Encoding.UTF8;
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
message.From = new System.Net.Mail.MailAddress(config.From, config.NickName);
message.Subject = subject;
message.Body = body;
foreach (var to in tos)
{
var tt = to.Split(':');
if (tt.Length > 1)
{
message.To.Add(new System.Net.Mail.MailAddress(tt[1], tt[0]));
}
else
{
message.To.Add(tt[0]);
}
}
if (ccs != null)
{
foreach (var to in ccs)
{
var tt = to.Split(':');
if (tt.Length > 1)
{
message.CC.Add(new System.Net.Mail.MailAddress(tt[1], tt[0]));
}
else
{
message.CC.Add(tt[0]);
}
}
}
if (bccs != null)
{
foreach (var to in bccs)
{
var tt = to.Split(':');
if (tt.Length > 1)
{
message.Bcc.Add(new System.Net.Mail.MailAddress(tt[1], tt[0]));
}
else
{
message.Bcc.Add(tt[0]);
}
}
} //--发送邮件附件--
string[] files = System.IO.Directory.GetFiles(@"~/UploadData" + "/" + DateTime.Now.ToString("yyyyMM"));
foreach (string file in files)
{
//string extension = System.IO.Path.GetExtension(file);
var attachmentMail = new System.Net.Mail.Attachment(@file);
message.Attachments.Add(attachmentMail);
}
//--the end-- smtp.Send(message);
ret.success = true;
ret.message = "发送成功";
/*
}
catch(Exception e)
{
throw e;
ret.success = false;
ret.message = e.Message;
}*/
return ret;
}

.NET中通過OUTLOOK發送附件內容的更多相关文章

  1. XML通過XSD產生CLASS

    步驟一:通過XML獲取XSD 格式:xsd "XML的完整路徑帶文件名" /O:"輸出路徑不帶文件名". C:\Windows\system32>xsd ...

  2. .net發送郵件

    using System; using System.Collections.Generic; using System.Linq; using System.Net.Mail; using Syst ...

  3. Oracle發送email功能

    Oracle 提供一個一個UTL_SMTP,可以發送email,結合oracle本身強大的schedule功能,比寫一隻排程效率高,且更簡單. split功能 /*創建package STRING_F ...

  4. Outlook2016中如何实现自动密送

    Outlook2016中如何实现自动密送 下面的方案您可以参考一下: 1)在Outlook里面键入Alt+F11打开VBA编辑器: 2)激活左边的工程面板,展开并双击上面的“Project (VbaP ...

  5. IOS中程序如何进行推送消息(本地推送,远程推送)

    [1]-------------什么是推送消息? 我就以一张图解释------------ [2]-----------IOS程序中如何进行本地推送?----------- 2.1,先征求用户同意 1 ...

  6. Linux發送郵件

    1.直接使用shell當編輯器 [root@phburdb1 mail]# mail -s "Hello World" juncai.chen@innolux.comHello j ...

  7. DELPHI 通過窗口句柄或窗口标题得到进程句柄

    DELPHI 通過窗口句柄或窗口标题得到进程句柄2009年05月08日 星期五 10:15procedure TForm1.Button1Click(Sender: TObject);varhWind ...

  8. MVC 中使用 SignalR 实现推送功能

    MVC 中使用 SignalR 实现推送功能 一,简介 Signal 是微软支持的一个运行在 Dot NET 平台上的 html websocket 框架.它出现的主要目的是实现服务器主动推送(Pus ...

  9. html5 服務器發送事件

    html5允許頁面獲得來自服務器的更新. 單項消息傳送: 頁面獲得服務器的更新. 以前頁面也可以獲得服務器的更新,但必須詢問服務器是否有可用的更新,而服務器發送事件是單向自動發送. 使用服務器發送事件 ...

随机推荐

  1. 学习PHP第一天-----简单登录

    <!DOCTYPE html> <html> <head> <title>初级登录界面</title> </head> < ...

  2. ./*** > /dev/null 2>&1

    转载:http://dongwei.iteye.com/blog/322702 shell中可能经常能看到:>/dev/null 2>&1 命令的结果可以通过%>的形式来定义 ...

  3. ellipsis

    语法:  text-overflow : clip | ellipsis 参数:  clip : 不显示省略标记(...),而是简单的裁切(clip这个参数是不常用的!)      ellipsis ...

  4. .net 设置导航的当前状态

    1.静态地址共用母版页时,加当前页的状态(使用加参数的方法实现): a: main.Master为链接设参数 MenuId <li> <a <%=MenuId==?" ...

  5. 非静态的字段、方法或属性“System.Web.UI.Page.ClientScript...”要求对象引用 (封装注册脚本)

    在写项目时想对asp.net的注册前台脚本事件进行封装,就添加了一个BasePage.cs页面,但一直报错‘非静态的字段.方法或属性“System.Web.UI.Page.ClientScript.. ...

  6. SSH框架中新建立实体类后的配置

    strut 层 public class Sellauthentication extends ActionSupport {    private SellauthenticationService ...

  7. HTML-embed标签详解

    Embed(一).基本语法:embed src=url说明:embed可以用来插入各种多媒体,格式可以是 Midi.Wav.AIFF.AU.MP3等等,      Netscape及新版的IE 都支持 ...

  8. 解决谷歌浏览器和360浏览器 input 自动填充淡黄色背景色的问题

     input:-webkit-autofill {-webkit-box-shadow: 0 0 0px 1000px white inset;}

  9. true是表示使用身份验证,否则不使用身份验证

    ?phpclass smtp{/* Public Variables */var $smtp_port;var $time_out;var $host_name;var $log_file;var $ ...

  10. 重新启动 Apache 以加载上面安装的模块

    尽管Ubuntu 是一种新兴的Linux分支,但Ubuntu 组织却为Apache提供了丰富的支持软件,这些软件都可以从发行版的光盘获取,也可以从官方站点轻松下载.所以,Ubuntu非常适合作为Web ...