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

     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. android 百度地图开发

    package sposition.webjoy.net.sendposition; import android.os.Bundle; import android.support.design.w ...

  2. PostgreSQL新手入门

    自从MySQL被Oracle收购以后,PostgreSQL逐渐成为开源关系型数据库的首选. 本文介绍PostgreSQL的安装和基本用法,供初次使用者上手.以下内容基于Debian操作系统,其他操作系 ...

  3. 常用JQ特效代码

    /** * hhBase 平台js * User: huanhuan * QQ: 651471385 * Email: th.wanghuan@gmail.com * 微博: huanhuan的天使 ...

  4. 1069. The Black Hole of Numbers (20)

    For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in ...

  5. Sqoop_ 从 hive 导到mysql常遇九问题总结(转)

    以前以为版本不同,遇到的问题就不同,后来发现,无论是新版本,还是老版本,遇到的问题大部分都是相同的.下面解决问题的方法仅供借鉴 1.拒绝连接的错误表现是什么?2.表不存在该如何解决?3.null字段填 ...

  6. Odoo 9 Odoo $ JQuery undifned

    浏览器处于假死状态,查看console发现 odoo,jquery,$ 未定义三处错误,后台显示IOError: IOError: [Errno 2] No such file or director ...

  7. HDU 4006 优先队列

    The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Oth ...

  8. SpringMvc输入地址报错

    出现上面的错误如果不是路径的问题,就应该找环境的问题,我这里的问题是tomcat7+jdk6,应该是jdk7或以后的版本(因为是web socket) 对应的版本http://tomcat.apach ...

  9. Qt常用命令收集

    qt的命令很多,用到的时候到网上查,常常不能一下查到.这里记录下一些备用 1 从.ui文件生成头文件: uic xxx.ui > xxx.h 2 moc生成 moc yourfilename.h ...

  10. AFN 2.6 code报错总结

    1. 错误打印  code=-1016 filed: text/html 错误原因:AFN默认不能解析请求回来的text/html数据 解决办法: AFN3.0的请看这里 AFHTTPSessionM ...