.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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93 |
/// <summary> /// 发送邮件 /// </summary> /// <param name="subject">邮件标题</param> /// <param name="body">内容</param> /// <param name="tos">接收者 格式 [email protected] 或 姓名:[email protected]</param> /// <param name="ccs">抄送者 格式 [email protected] 或 姓名:[email protected]</param> /// <param name="bccs">密送者 格式 [email protected] 或 姓名:[email protected]</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; } |
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。