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

     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. lua语法备忘录

    1.三元表达式 a?b:c => a and b or c 2.循环数组 local keys = args.kvs:get_keys() local arr = xstring.split(k ...

  2. javaweb之框架标签(day1 框架标签的使用)

    框架标签 <frameset> --rows:按照行进行划分<frameset rows='80,*'> --rows:按照列进行划分<frameset cols='80 ...

  3. 清除BOM头源码

    BOM: Byte Order Mark UTF-8 BOM又叫UTF-8 签名,其实UTF-8 的BOM对UFT-8没有作用,是为了支援UTF-16,UTF-32才加上的BOM,BOM签名的意思就是 ...

  4. css限制div字符超出部分,简单有方便

    text-overflow: -o-ellipsis-lastline;overflow: hidden;text-overflow: ellipsis;display: -webkit-box;-w ...

  5. div自定义的滚动条 (水平导航条)

    <!DOCTYPE html> <html> <head> <title></title> <style> div{ /* wi ...

  6. 局域网访问本地localhost页面

    1.关闭防火墙 2.cmd - ipconfig - IPv4 地址 . . . . . . . . . . . . : 192.168.0.34 如果链接的是wifi,请右键确保手机和电脑链接同一个 ...

  7. iPhone6手機產品提交了進網申請

    近期,海外投資蘋果公司為iPhone6手機產品提交了進網申請,經電信設備進網檢測機構測試和我部審查,相關產品滿足進網管理要求.根據<電信條例>有關規定,我部依法定程式在法定時限內為蘋果公司 ...

  8. 一个简单驱动的makefile

    KVERS = $(shell uname -r) #Kernel modulesobj-m += hello.o build: kernel_modules kernel_modules: make ...

  9. Java Web(转)

    struts2+spring+hibernate 上传文件 关 键字: s2sh 上传文件 struts2 spring hibernate 前段时间,我用struts2.1.6.spring2.5. ...

  10. c#根据后台数据,自动生成checkbox

    前端在aspx中,添加生成checkbox的容器div: <div id="container" runat="server"></div&g ...