.NET中通過OUTLOOK發送附件內容
最近碰到一個發送郵件附件的問題,隨便幾下來,方便以後學習。
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發送附件內容的更多相关文章
- XML通過XSD產生CLASS
步驟一:通過XML獲取XSD 格式:xsd "XML的完整路徑帶文件名" /O:"輸出路徑不帶文件名". C:\Windows\system32>xsd ...
- .net發送郵件
using System; using System.Collections.Generic; using System.Linq; using System.Net.Mail; using Syst ...
- Oracle發送email功能
Oracle 提供一個一個UTL_SMTP,可以發送email,結合oracle本身強大的schedule功能,比寫一隻排程效率高,且更簡單. split功能 /*創建package STRING_F ...
- Outlook2016中如何实现自动密送
Outlook2016中如何实现自动密送 下面的方案您可以参考一下: 1)在Outlook里面键入Alt+F11打开VBA编辑器: 2)激活左边的工程面板,展开并双击上面的“Project (VbaP ...
- IOS中程序如何进行推送消息(本地推送,远程推送)
[1]-------------什么是推送消息? 我就以一张图解释------------ [2]-----------IOS程序中如何进行本地推送?----------- 2.1,先征求用户同意 1 ...
- Linux發送郵件
1.直接使用shell當編輯器 [root@phburdb1 mail]# mail -s "Hello World" juncai.chen@innolux.comHello j ...
- DELPHI 通過窗口句柄或窗口标题得到进程句柄
DELPHI 通過窗口句柄或窗口标题得到进程句柄2009年05月08日 星期五 10:15procedure TForm1.Button1Click(Sender: TObject);varhWind ...
- MVC 中使用 SignalR 实现推送功能
MVC 中使用 SignalR 实现推送功能 一,简介 Signal 是微软支持的一个运行在 Dot NET 平台上的 html websocket 框架.它出现的主要目的是实现服务器主动推送(Pus ...
- html5 服務器發送事件
html5允許頁面獲得來自服務器的更新. 單項消息傳送: 頁面獲得服務器的更新. 以前頁面也可以獲得服務器的更新,但必須詢問服務器是否有可用的更新,而服務器發送事件是單向自動發送. 使用服務器發送事件 ...
随机推荐
- org.openqa.selenium.StaleElementReferenceException
org.openqa.selenium.StaleElementReferenceException如何解啊.什么原因造成的,貌似有时会出现,有时不会出现
- C# empty private constructor
A private constructor is a special instance constructor. It is generally used in classes that contai ...
- jsp...九九乘法表,三角形,菱形
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- hadoop配置远程客户端
独立出一台机器,作为客户端,可以连接远程集群,配置注意事项: 1.首先是hive,需要服务器启动一个服务 hive --service metastore 然后修改hive客户端 hive-site. ...
- winform记事本初步实现
public Form1() { InitializeComponent(); } private void 剪切TToolStripMenuItem_Click(object sender, Eve ...
- Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds 解决方法
Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds. If the server requires ...
- Oracle登录操作系统验证和密码文件验证
1.确认数据库版本 2.查看当前配置文件 ORALCE数据库不同的登录验证方式是和SQLNET.ORA配置文件有关系的,在配置文件中有一个参数sqlnet.authentication_service ...
- SRM 588 DIV1
250 题意:有n首不同的曲子,你唱每首曲子需要花费a的时间以及一个调整的时间b,调整的时间为此首歌的曲调减去上一首歌的曲调的绝对值. 思路:我们用dp[i][k]表示前i首歌只唱k首用的最小时间花费 ...
- jQuery库中的变量$和其它类库的变量$冲突解决方案
jQuery.noConflict();//把变量$给其它插件 /* 由于把jQuery插件中的变量$给了其它插件使用 那么在调用jQuery插件的时候只能使用jQuery 但是这样很不方便 1.其实 ...
- MVC概念性的内容
MVC: 是一个缩写(model + view + control), Model:是一些类文件, 功能:负责增删改查, 负责跟数据库打交道 (把数据存入到数据库: 从数据库把数据读 ...