SMTP email from C#

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/// <summary>
 /// 一人一附件发送邮件
 ///  2017-05-17 涂聚文  GeovinDu
 /// </summary>
 /// <param name="To">接收邮件人</param>
 /// <param name="Subject">邮件标题</param>
 /// <param name="Msg">邮件内容</param>
 /// <param name="bodyHtml">是否網頁</param>
 /// <param name="AttachmentStream"></param>
 /// <param name="AttachmentFileName">附件</param>
 /// <returns></returns>
 public static bool SendEmaildu(string To, string Subject, string Msg, bool bodyHtml, MemoryStream AttachmentStream,string AttachmentFileName)
 {
     bool SendOk = false;
     try
     {
         System.Net.Mail.MailMessage newMsg = new System.Net.Mail.MailMessage(System.Configuration.ConfigurationManager.AppSettings["mailCfg"], To, Subject, Msg);
         newMsg.BodyEncoding = System.Text.Encoding.UTF8;
         newMsg.HeadersEncoding = System.Text.Encoding.UTF8;
         newMsg.SubjectEncoding = System.Text.Encoding.UTF8;
 
         System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
         //smtpClient.Credentials = new NetworkCredential("geovindu@dusystem.com","888888");
         //smtpClient.Port = 25;
         //smtpClient.EnableSsl = false;
         if (AttachmentStream != null && AttachmentFileName != null)
         {
 
             // ADD AN ATTACHMENT.
             //String sFile = @"C:\Source\LNS_IS.TXT";
             //MailAttachment oAttch = new MailAttachment(sFile, MailEncoding.Base64);
             //string filePath = @"C:\TestEmail\SKYPE11月黑咭生日會員.xls";
             //MemoryStream memStream = new MemoryStream();
             //using (FileStream fileStream = File.OpenRead(filePath))
             //{
 
             //    memStream.SetLength(fileStream.Length);
             //    fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);
             //}
 
             newMsg.Attachments.Add(new Attachment(AttachmentStream, AttachmentFileName)); //MediaTypeNames.Image.Jpeg)               
            
 
             //System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(AttachmentStream, AttachmentFileName);
             //System.Net.Mime.ContentDisposition disposition = attachment.ContentDisposition;
             //disposition.FileName = AttachmentFileName;
             //disposition.DispositionType = System.Net.Mime.DispositionTypeNames.Attachment;
             //newMsg.Attachments.Add(attachment);
 
         }
         newMsg.IsBodyHtml = bodyHtml;
         
         smtpClient.Send(newMsg);
         SendOk = true;
         //return SENT_OK;
     }
     catch (Exception ex)
     {
         ex.Message.ToString();
         AttachmentStream.Close();
         SendOk = false;
         //"Error: " + ex.Message + "<br/><br/>Inner Exception: "+ ex.InnerException;
 
          
     }
     AttachmentStream.Close();
     return SendOk;
 
 }
 /// <summary>
 /// 一人一附件发送邮件
 /// 2017-05-17 涂聚文  GeovinDu
 /// </summary>
 /// <param name="To">接收邮件人</param>
 /// <param name="Subject">邮件标题</param>
 /// <param name="Msg">邮件内容</param>
 /// <param name="bodyHtml">是否網頁</param>
 /// <param name="AttachmentFileName">附件</param>
 /// <returns></returns>
 public static bool SendEmaildu(string To, string Subject, string Msg, bool bodyHtml, string AttachmentFileName)
 {
     MemoryStream AttachmentStream = new MemoryStream();
     bool SendOk = false;
     try
     {
         System.Net.Mail.MailMessage newMsg = new System.Net.Mail.MailMessage(System.Configuration.ConfigurationManager.AppSettings["mailCfg"], To, Subject, Msg);
         newMsg.BodyEncoding = System.Text.Encoding.UTF8;
         newMsg.HeadersEncoding = System.Text.Encoding.UTF8;
         newMsg.SubjectEncoding = System.Text.Encoding.UTF8;
 
         System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
         //smtpClient.Credentials = new NetworkCredential("geovindu@dusystem.com","888888");
         //smtpClient.Port = 25;
         //smtpClient.EnableSsl = false;
         if (AttachmentStream != null && AttachmentFileName != null)
         {
 
             // ADD AN ATTACHMENT.
             //String sFile = @"C:\Source\LNS_IS.TXT";
             //MailAttachment oAttch = new MailAttachment(sFile, MailEncoding.Base64);
             //string filePath = @"C:\TestEmail\SKYPE11月黑咭生日會員.xls";
             //MemoryStream memStream = new MemoryStream();
             //using (FileStream fileStream = File.OpenRead(filePath))
             //{
 
             //    memStream.SetLength(fileStream.Length);
             //    fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);
             //}
 
             newMsg.Attachments.Add(new Attachment(AttachmentStream, AttachmentFileName)); //MediaTypeNames.Image.Jpeg)               
 
 
             //System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(AttachmentStream, AttachmentFileName);
             //System.Net.Mime.ContentDisposition disposition = attachment.ContentDisposition;
             //disposition.FileName = AttachmentFileName;
             //disposition.DispositionType = System.Net.Mime.DispositionTypeNames.Attachment;
             //newMsg.Attachments.Add(attachment);
 
         }
         newMsg.IsBodyHtml = bodyHtml;               
         smtpClient.Send(newMsg);
         SendOk = true;
         //return SENT_OK;
     }
     catch (Exception ex)
     {
         ex.Message.ToString();
         AttachmentStream.Close();
         SendOk = false;
         //"Error: " + ex.Message + "<br/><br/>Inner Exception: "+ ex.InnerException;
 
 
     }
     AttachmentStream.Close();
     return SendOk;
 
 }
 
 /// <summary>
 /// 一人多附件发送邮件
 /// 2019-06-18 涂聚文 GeovinDu
 /// </summary>
 /// <param name="To">接收邮件人</param>
 /// <param name="Subject">邮件标题</param>
 /// <param name="Msg">邮件内容</param>
 /// <param name="bodyHtml">是否網頁</param>
 /// <param name="AttachmentFiles">多附件</param>
 /// <returns></returns>
 public static bool SendEmailduFiles(string To, string Subject, string Msg, bool bodyHtml, List<string> AttachmentFiles)
 {
     MemoryStream AttachmentStream = new MemoryStream();
     bool SendOk = false;
     try
     {
         System.Net.Mail.MailMessage newMsg = new System.Net.Mail.MailMessage(System.Configuration.ConfigurationManager.AppSettings["mailCfg"], To, Subject, Msg);
         newMsg.BodyEncoding = System.Text.Encoding.UTF8;
         newMsg.HeadersEncoding = System.Text.Encoding.UTF8;
         newMsg.SubjectEncoding = System.Text.Encoding.UTF8;
 
         System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
         //smtpClient.Credentials = new NetworkCredential("geovindu@dusystem.com","888888");
         //smtpClient.Port = 25;
         //smtpClient.EnableSsl = false;
         
 
 
         if (AttachmentStream != null && AttachmentFiles != null)
         {
 
           
             foreach (string filename in AttachmentFiles)
             {
                 newMsg.Attachments.Add(new Attachment(AttachmentStream, filename)); //MediaTypeNames.Image.Jpeg)  
                 
             }    
 
         }
         newMsg.IsBodyHtml = bodyHtml;              
         smtpClient.Send(newMsg);
         SendOk = true;
     }
     catch (Exception ex)
     {
         ex.Message.ToString();
         AttachmentStream.Close();
         SendOk = false;
     }
     AttachmentStream.Close();
     return SendOk;
 }
 
 /// <summary>
 /// 多人多附件发送邮件
 /// 不考虑统计每封邮件发送状态(另外考虑完善代码)
 ///  2019-06-18 涂聚文 GeovinDu
 /// </summary>
 /// <param name="Tos">多人接收邮件人</param>
 /// <param name="Subject">邮件标题</param>
 /// <param name="Msg">邮件内容</param>
 /// <param name="bodyHtml">是否網頁</param>
 /// <param name="AttachmentFiles">多附件</param>
 /// <returns></returns>
 public static bool SendEmailsAndFiles(List<string> Tos, string Subject, string Msg, bool bodyHtml, List<string> AttachmentFiles)
 {
     MemoryStream AttachmentStream = new MemoryStream();
     bool SendOk = false;
     List<Attachment> all = new List<Attachment>();
     try
     {
         if (AttachmentStream != null && AttachmentFiles != null)
         {
             foreach (string filename in AttachmentFiles)
             {
 
                 all.Add(new Attachment(AttachmentStream, filename));
             }
         }
         if (Tos.Count > 0)
         {
 
 
             foreach (string To in Tos)
             {
                 System.Net.Mail.MailMessage newMsg = new System.Net.Mail.MailMessage(System.Configuration.ConfigurationManager.AppSettings["mailCfg"], To, Subject, Msg);
                 newMsg.BodyEncoding = System.Text.Encoding.UTF8;
                 newMsg.HeadersEncoding = System.Text.Encoding.UTF8;
                 newMsg.SubjectEncoding = System.Text.Encoding.UTF8;
                 System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
                 //smtpClient.Credentials = new NetworkCredential("geovindu@dusystem.com","888888");
                 //smtpClient.Port = 25;
                 //smtpClient.EnableSsl = false;
                 if (AttachmentStream != null && AttachmentFiles != null)
                 {
                     foreach (Attachment attname in all)
                     {
                         newMsg.Attachments.Add(attname); //MediaTypeNames.Image.Jpeg)  
 
                     }
                 }
                 newMsg.IsBodyHtml = bodyHtml;                       
                 smtpClient.Send(newMsg);
                 SendOk = true;
            }
         }
     }
     catch (Exception ex)
     {
         ex.Message.ToString();
         AttachmentStream.Close();
         SendOk = false;
     }
     AttachmentStream.Close();
     return SendOk;
 }

  测试代码:

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
    MemoryStream theMemStream = new MemoryStream();
    string file = @"C:\TestEmail\SKYPE11月黑咭生日會員.xls"//C:\TestEmail\Top1000WorldBanks2014.pdf
    if (File.Exists(file))
    {
        //AttachmentStream = File.Open(file, FileMode.Open, FileAccess.Read);
 
 
        byte[] filebyte = MirosoftSmptHelper.ReadFile(file);
 
        theMemStream.Write(filebyte, 0, filebyte.Length);// = File.OpenRead(file);
        theMemStream.Position = 0;
 
        //theMemStream.Close();
 
        // AttachmentStream = theMemStream;// BytesToStream(filebyte); //new MemoryStream(filebyte);// // BytesToStream(filebyte);// FileToStream(file);//
 
        //AttachmentStream.CanTimeout =true;
        //AttachmentStream.ReadTimeout = 1000;
        //AttachmentStream.WriteTimeout = 1000;
        //AttachmentStream = BytesToStream(filebyte);
 
        //theMemStream.Close();
    }
    List<string> AttachmentFiles = new List<string>();
    AttachmentFiles.Add("馬拉松獎牌刻字.xlsx");
    AttachmentFiles.Add("进一步优化供给推动消费平稳增长2019.pdf");
    AttachmentFiles.Add("逃犯条例草案.pdf");
    AttachmentFiles.Add("复习卷14.doc");
    List<string> Tos = new List<string>();
    Tos.Add("geovindu@163.com");
    Tos.Add("geovindu@hotmail.com");
    Tos.Add("geovi@qq.com");
 
    //1.一人一附件
    //bool issend = MirosoftSmptHelper.SendEmaildu("geovindu@163.com", "中文文件名.net 4.0,geovindu 涂聚文塗聚文工團、", "<p> 生命運浪漫滿屋geovindu江蘇黃河</p><br/><p>上官放個屁,下官一台戲</p>", true,"馬拉松獎牌刻字.xlsx");
    //2.一人多附件
    //bool issend = MirosoftSmptHelper.SendEmailduFiles("geovindu@163.com", "中文文件名.net 4.0,geovindu 涂聚文塗聚文工團、", "<p> 生命運浪漫滿屋geovindu江蘇黃河</p><br/><p>上官放個屁,下官一台戲</p>", true, AttachmentFiles);
   //3.多人多附件
    bool issend = MirosoftSmptHelper.SendEmailsAndFiles(Tos, "中文文件名.net 4.0,geovindu 涂聚文塗聚文工團、", "<p> 生命運浪漫滿屋geovindu江蘇黃河</p><br/><p>上官放個屁,下官一台戲</p>", true, AttachmentFiles);
    theMemStream.Close();
    if (issend)
    {
        MessageBox.Show("ok");
    }
    else
    {
        MessageBox.Show("no");
    }
}

  不忘初心,方得始终;初心易得,始终难守。

 

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/// <summary>
/// 多人多附件发送邮件
/// 不考虑统计每封邮件发送状态(另外考虑完善代码)
///  2019-06-18 涂聚文 GeovinDu
///  2019-07-01
/// </summary>
/// <param name="Tos">多人接收邮件人</param>
/// <param name="Subject">邮件标题</param>
/// <param name="Msg">邮件内容</param>
/// <param name="bodyHtml">是否網頁</param>
/// <param name="AttachmentFiles">多附件--具体文件地址</param>
/// <returns></returns>
public static bool SendEmailsAndFiles(List<string> Tos, string Subject, string Msg, bool bodyHtml, List<string> AttachmentFiles)
{
    MemoryStream AttachmentStream = new MemoryStream();
    bool SendOk = false;
    List<Attachment> all = new List<Attachment>();
    try
    {
        if (AttachmentStream != null && AttachmentFiles != null)
        {
            foreach (string filename in AttachmentFiles)
            {
                Attachment attachment = new Attachment(filename, MediaTypeNames.Application.Octet); // //AttachmentStream, filename
                ContentDisposition disposition = attachment.ContentDisposition;
                disposition.CreationDate = File.GetCreationTime(filename);
                disposition.ModificationDate = File.GetLastWriteTime(filename);
                disposition.ReadDate = File.GetLastAccessTime(filename);
                disposition.FileName = Path.GetFileName(filename);
                disposition.Size = new FileInfo(filename).Length;                     
                disposition.DispositionType = DispositionTypeNames.Attachment;
                all.Add(attachment); //new Attachment(AttachmentStream, filename)
            }
        }
        if (Tos.Count > 0)
        {
 
 
            foreach (string To in Tos)
            {
                System.Net.Mail.MailMessage newMsg = new System.Net.Mail.MailMessage(System.Configuration.ConfigurationManager.AppSettings["mailCfg"], To, Subject, Msg);
                newMsg.BodyEncoding = System.Text.Encoding.UTF8;
                newMsg.HeadersEncoding = System.Text.Encoding.UTF8;
                newMsg.SubjectEncoding = System.Text.Encoding.UTF8;
                System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
                //smtpClient.Credentials = new NetworkCredential("geovindu@dusystem.com","888888");
                //smtpClient.Port = 25;
                //smtpClient.EnableSsl = false;
                if (AttachmentStream != null && AttachmentFiles != null)
                {
                    foreach (Attachment attname in all)
                    {
 
                         
                        newMsg.Attachments.Add(attname); //MediaTypeNames.Image.Jpeg)  
 
                    }
                }
                newMsg.IsBodyHtml = bodyHtml;                       
                smtpClient.Send(newMsg);
                SendOk = true;
           }
        }
    }
    catch (Exception ex)
    {
        ex.Message.ToString();
        AttachmentStream.Close();
        SendOk = false;
    }
    AttachmentStream.Close();
    return SendOk;
}
 
/// <summary>
/// 多人多附件发送邮件
/// 不考虑统计每封邮件发送状态(另外考虑完善代码)
///  2019-07-01 涂聚文 GeovinDu
/// </summary>
/// <param name="Tos">多人接收邮件人</param>
/// <param name="Subject">邮件标题</param>
/// <param name="Msg">邮件内容</param>
/// <param name="bodyHtml">是否網頁</param>
/// <param name="AttachmentFiles">多附件--根据运行程序目录地址</param>
/// <returns></returns>
public static bool SendEmailsAndLocalFiles(List<string> Tos, string Subject, string Msg, bool bodyHtml, List<string> AttachmentFiles)
{
    MemoryStream AttachmentStream = new MemoryStream();
    bool SendOk = false;
    List<Attachment> all = new List<Attachment>();
    try
    {
        if (AttachmentStream != null && AttachmentFiles != null)
        {
            foreach (string filename in AttachmentFiles)
            {
                //具体的文件地址,用此法
                //Attachment attachment = new Attachment(filename, MediaTypeNames.Application.Octet); // //AttachmentStream, filename
                //ContentDisposition disposition = attachment.ContentDisposition;
                //disposition.CreationDate = File.GetCreationTime(filename);
                //disposition.ModificationDate = File.GetLastWriteTime(filename);
                //disposition.ReadDate = File.GetLastAccessTime(filename);
                //disposition.FileName = Path.GetFileName(filename);
                //disposition.Size = new FileInfo(filename).Length;
                //disposition.DispositionType = DispositionTypeNames.Attachment;
                all.Add(new Attachment(AttachmentStream, filename));//在运行程序的目录的文件没有问题,可执行。如是不是此语言件地址,附件无效
            }
        }
        if (Tos.Count > 0)
        {
 
 
            foreach (string To in Tos)
            {
                System.Net.Mail.MailMessage newMsg = new System.Net.Mail.MailMessage(System.Configuration.ConfigurationManager.AppSettings["mailCfg"], To, Subject, Msg);
                newMsg.BodyEncoding = System.Text.Encoding.UTF8;
                newMsg.HeadersEncoding = System.Text.Encoding.UTF8;
                newMsg.SubjectEncoding = System.Text.Encoding.UTF8;
                System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
                //smtpClient.Credentials = new NetworkCredential("geovindu@dusystem.com","888888");
                //smtpClient.Port = 25;
                //smtpClient.EnableSsl = false;
                if (AttachmentStream != null && AttachmentFiles != null)
                {
                    foreach (Attachment attname in all)
                    {
                        newMsg.Attachments.Add(attname); //MediaTypeNames.Image.Jpeg)  
 
                    }
                }
                newMsg.IsBodyHtml = bodyHtml;
                smtpClient.Send(newMsg);
                SendOk = true;
            }
        }
    }
    catch (Exception ex)
    {
        ex.Message.ToString();
        AttachmentStream.Close();
        SendOk = false;
    }
    AttachmentStream.Close();
    return SendOk;
}

  

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/// <summary>
  /// 20190702 多人邮件
  /// geovindu 涂聚文
  /// </summary>
  /// <param name="Mails"></param>
  /// <param name="Subject"></param>
  /// <param name="Msg"></param>
  /// <param name="bodyHtml"></param>
  /// <param name="AttachmentFiles"></param>
  /// <returns></returns>
  public static bool SendEmailsFiles(List<MailAddress> Mails, string Subject, string Msg, bool bodyHtml, List<string> AttachmentFiles)
  {
      MemoryStream AttachmentStream = new MemoryStream();
      bool SendOk = false;
      List<Attachment> all = new List<Attachment>();
      try
      {
          if (AttachmentStream != null && AttachmentFiles != null)
          {
              foreach (string filename in AttachmentFiles)
              {
                  //具体的文件地址,用此法
                  Attachment attachment = new Attachment(filename, MediaTypeNames.Application.Octet); // //AttachmentStream, filename
                  ContentDisposition disposition = attachment.ContentDisposition;
                  disposition.CreationDate = File.GetCreationTime(filename);
                  disposition.ModificationDate = File.GetLastWriteTime(filename);
                  disposition.ReadDate = File.GetLastAccessTime(filename);
                  disposition.FileName = Path.GetFileName(filename);
                  disposition.Size = new FileInfo(filename).Length;
                  disposition.DispositionType = DispositionTypeNames.Attachment;
                  all.Add(attachment);//在运行程序的目录的文件没有问题,可执行。如是不是此语言件地址,附件无效
              }
          }
          if (Mails.Count > 0)
          {
 
              int c = 1;
              foreach (MailAddress ToAddress in Mails)
              {
 
                  MailAddress from=new MailAddress(System.Configuration.ConfigurationManager.AppSettings["mailCfg"],System.Configuration.ConfigurationManager.AppSettings["displayName"]);
                  //from.DisplayName="";
                 // from.Address=;
                  System.Net.Mail.MailMessage newMsg = new System.Net.Mail.MailMessage();
                  newMsg.From = from;
                  newMsg.To.Add(ToAddress);
                  newMsg.Subject = Subject;
                  newMsg.Body = Msg;
                  newMsg.BodyEncoding = System.Text.Encoding.UTF8;
                  newMsg.HeadersEncoding = System.Text.Encoding.UTF8;
                  newMsg.SubjectEncoding = System.Text.Encoding.UTF8;
                  System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
                  //smtpClient.Credentials = new NetworkCredential("geovindu@dusystem.com","888888");
                  //smtpClient.Port = 25;
                  //smtpClient.EnableSsl = false;
                  if (AttachmentStream != null && AttachmentFiles != null)
                  {
                      foreach (Attachment attname in all)
                      {
                          newMsg.Attachments.Add(attname); //MediaTypeNames.Image.Jpeg) 
                      }
                  }
                  newMsg.IsBodyHtml = bodyHtml;
                  //smtpClient.Send(newMsg);
                  smtpClient.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
                  string userState = "test message:"+c.ToString();
                  smtpClient.SendAsync(newMsg, userState);
                  SendOk = true;
                  c++;
              }
               
          }
      }
      catch (Exception ex)
      {
          ex.Message.ToString();
          AttachmentStream.Close();
          SendOk = false;
      }
      AttachmentStream.Close();
      return SendOk;
  }
  /// <summary>
  /// 一人多附件
  /// </summary>
  /// <param name="Mails"></param>
  /// <param name="Subject"></param>
  /// <param name="Msg"></param>
  /// <param name="bodyHtml"></param>
  /// <param name="AttachmentFiles"></param>
  /// <returns></returns>
  public static bool SendEmailFiles(MailAddress Mails, string Subject, string Msg, bool bodyHtml, List<string> AttachmentFiles)
  {
      MemoryStream AttachmentStream = new MemoryStream();
      bool SendOk = false;
      List<Attachment> all = new List<Attachment>();
      try
      {
          if (AttachmentStream != null && AttachmentFiles != null)
          {
              foreach (string filename in AttachmentFiles)
              {
                  //具体的文件地址,用此法
                  Attachment attachment = new Attachment(filename, MediaTypeNames.Application.Octet); // //AttachmentStream, filename
                  ContentDisposition disposition = attachment.ContentDisposition;
                  disposition.CreationDate = File.GetCreationTime(filename);
                  disposition.ModificationDate = File.GetLastWriteTime(filename);
                  disposition.ReadDate = File.GetLastAccessTime(filename);
                  disposition.FileName = Path.GetFileName(filename);
                  disposition.Size = new FileInfo(filename).Length;
                  disposition.DispositionType = DispositionTypeNames.Attachment;
                  all.Add(attachment);//在运行程序的目录的文件没有问题,可执行。如是不是此语言件地址,附件无效
              }
          }
 
 
              MailAddress from = new MailAddress(System.Configuration.ConfigurationManager.AppSettings["mailCfg"], System.Configuration.ConfigurationManager.AppSettings["displayName"]);
              //from.DisplayName="";
              // from.Address=;
              System.Net.Mail.MailMessage newMsg = new System.Net.Mail.MailMessage();
              newMsg.From = from;
              newMsg.To.Add(Mails);
              newMsg.Subject = Subject;
              newMsg.Body = Msg;
              newMsg.BodyEncoding = System.Text.Encoding.UTF8;
              newMsg.HeadersEncoding = System.Text.Encoding.UTF8;
              newMsg.SubjectEncoding = System.Text.Encoding.UTF8;
              System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
              //smtpClient.Credentials = new NetworkCredential("geovindu@dusystem.com","888888");
              //smtpClient.Port = 25;
              //smtpClient.EnableSsl = false;
              if (AttachmentStream != null && AttachmentFiles != null)
              {
                  foreach (Attachment attname in all)
                  {
                      newMsg.Attachments.Add(attname); //MediaTypeNames.Image.Jpeg) 
                  }
              }
              newMsg.IsBodyHtml = bodyHtml;
              //smtpClient.Send(newMsg);
              smtpClient.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
              string userState = "test message:发送中";
              smtpClient.SendAsync(newMsg, userState);
              SendOk = true;                      
              
           
      }
      catch (Exception ex)
      {
          ex.Message.ToString();
          AttachmentStream.Close();
          SendOk = false;
      }
      AttachmentStream.Close();
      return SendOk;
  }

  测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
List<MailAddress> mails = new List<MailAddress>();
          MailAddress address = null;
          address = new MailAddress("geovindu@163.com", "涂聚文", Encoding.UTF8);          
          mails.Add(address);
          address = new MailAddress("geovindu@hotmail.com", "塗聚文", Encoding.UTF8);
          mails.Add(address);
          address = new MailAddress("463588883@qq.com", "捷為工作室", Encoding.UTF8);
          mails.Add(address);
          string strMessage = string.Empty;
          string subject = string.Empty;
 
          subject=txtSubject.Text.Trim();
          strMessage = htmlEditUserControl1.HtmlEditControl.DocumentText;
 
          //1.一人一附件
          //bool issend = MirosoftSmptHelper.SendEmaildu("geovindu@163.com", "中文文件名.net 4.0,geovindu 涂聚文塗聚文工團、", "<p> 生命運浪漫滿屋geovindu江蘇黃河</p><br/><p>上官放個屁,下官一台戲</p>", true,"馬拉松獎牌刻字.xlsx");
          //2.一人多附件
          //bool issend = MirosoftSmptHelper.SendEmailduFiles("geovindu@163.com", "中文文件名.net 4.0,geovindu 涂聚文塗聚文工團、", "<p> 生命運浪漫滿屋geovindu江蘇黃河</p><br/><p>上官放個屁,下官一台戲</p>", true, AttachmentFiles);
         //3.多人多附件
          //bool issend = MirosoftSmptHelper.SendEmailsAndFiles(Tos, subject, strMessage, true, AttachmentFiles);
          bool issend = MirosoftSmptHelper.SendEmailsFiles(mails, subject, strMessage, true, AttachmentFiles);

  

posted @   ®Geovin Du Dream Park™  阅读(339)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2013-06-18 javascript: Convert special characters to HTML
2013-06-18 javascript:中文等字符转成unicode
2010-06-18 Sql 脚本导入EXCEL数据
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示