在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#网络编程 → C#发送邮件实例代码

C#发送邮件实例代码

C#网络编程

下载此实例
  • 开发语言:C#
  • 实例大小:0.14M
  • 下载次数:54
  • 浏览次数:350
  • 发布时间:2015-07-08
  • 实例类别:C#网络编程
  • 发 布 人:tmxm
  • 文件格式:.rar
  • 所需积分:2
 相关标签: 邮件 C# 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Net.Mail;
using System.Text.RegularExpressions;
using System.IO;
using System.Threading;
 
namespace MailTo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public ArrayList paths = new ArrayList();//保存附件地址
        
 
        /*单发*/
        private void button2_Click(object sender, EventArgs e)
        {
            //if (!check())
            //    return;
            //if (t_to.SelectedIndex != -1)
            //{
            //    mailunit mu = new mailunit(t_stmp.Text, t_from.Text, t_pwd.Text, t_to.Text, t_subject.Text, t_body.Text,paths);
            //    mu.SendMail();
            //}
            //else
            //    MessageBox.Show("请先选中一个收件人!");
        }
 
        /*群发*/
        private void button1_Click(object sender, EventArgs e)
        {
            if (!check())
                return;
            //初始化
            toolStripProgressBar1.Visible = true;
            toolStripStatusLabel3.Text = "";
            toolStripProgressBar1.Maximum = t_to.Items.Count;
 
            ArrayList alist = new ArrayList();
 
            for (int i = 0; i < t_to.Items.Count; i  )
            {
                string mail;
                if (i < senderLst.Items.Count)
                {
                    mail = t_to.Items[i].ToString()   ","   senderLst.Items[i].ToString();
                    
                }
                else
                {
                    mail = t_to.Items[i].ToString()   ","   senderLst.Items[i % senderLst.Items.Count].ToString();
                }
                alist.Add(mail);
 
            }
 
            for (int j = 0; j < alist.Count; j  )
            {
                string senderStr = alist[j].ToString();
                string[] str = senderStr.Split(new char[] { ',', ',',',' });
                string stmpStr = str[1];
                string fromStr = str[2];
                string pwdStr = str[3];
                string toMail = str[0];
                mailunit mu = new mailunit(stmpStr, fromStr, pwdStr, toMail, t_subject.Text, t_body.Text, paths);
                Thread mythread = new Thread(new ThreadStart(mu.SendMail2));
                mythread.Start();
            }
 
            ////多线程群发
            //for (int i = 0; i < t_to.Items.Count; i  )
            //{
            //    for (int j = 0; j < senderLst.Items.Count; j  )
            //    {
            //        string senderStr = senderLst.Items[j].ToString();
            //        string[] str = senderStr.Split(new char[] { ',', ','});
 
            //        string stmpStr = str[1];
            //        string fromStr = str[2];
            //        string pwdStr = str[3];
            //        string toMail = str[0];
            //        mailunit mu = new mailunit(stmpStr, fromStr, pwdStr, t_to.Items[i].ToString(), t_subject.Text, t_body.Text, paths);
            //        Thread mythread = new Thread(new ThreadStart(mu.SendMail2));
            //        mythread.Start();
            //    }
            //}
            while (All.runing != 0)
            {
                toolStripProgressBar1.Value = t_to.Items.Count - All.runing;
                Application.DoEvents();
                toolStripStatusLabel3.Text = "发送成功:"   All.success.ToString()   "条  发送失败:"   All.fail.ToString()   "条";
            }
            toolStripProgressBar1.Visible = false;
            toolStripStatusLabel3.Text = "【结果】发送成功:" All.success.ToString() "条  发送失败:" All.fail.ToString() "条";
            All.success = 0;
            All.fail = 0;
            MessageBox.Show("【结果】发送成功:"   All.success.ToString()   "条  发送失败:"   All.fail.ToString()   "条");
        }
 
        /*检查输入参数*/
        public bool check()
        {
            string err = "注意:\n\n";
            int errCount = 0;
            //if (t_stmp.Text.Trim().Length == 0)
            //{
            //    err  = "Smtp服务器地址不能为空!\r\n";
            //    errCount  ;
            //}
            //if (t_from.Text.Trim().Length == 0)
            //{
            //    err  = "用户名不能为空!\r\n";
            //    errCount  ;
            //}
            //if (t_pwd.Text.Trim().Length == 0)
            //{
            //    err  = "密码不能为空!\r\n";
            //    errCount  ;
            //}
            if (t_subject.Text.Trim().Length == 0)
            {
                err  = "标题不能为空!\r\n";
                errCount  ;
            }
            if (t_to.Items.Count == 0)
            {
                err  = "收件人不能为空!\r\n";
                errCount  ;
            }
            if (t_body.Text.Trim().Length == 0)
            {
                err  = "正文不能为空!\r\n";
                errCount  ;
            }
            Regex myreg = new Regex(@"^\w ([- .]\w )*@\w ([-.]\w )*\.\w ([-.]\w )*$"); //email验证
            //if (!myreg.IsMatch(t_from.Text))
            //{
            //    err  = "用户名格式不正确!\r\n";
            //    errCount  ;
            //}
            //看还有没有其他需要验证的
            if (errCount == 0)
                return true;
            else
            {
                MessageBox.Show(err,"提示");
                return false;
            }
        }
 
        /*添加收件人*/
        private void button3_Click(object sender, EventArgs e)
        {
            add ad = new add();
            if (ad.ShowDialog() == DialogResult.OK&&!t_to.Items.Contains(ad.email))
                t_to.Items.Add(ad.email);
        }
        /*删除收件人*/
        private void button4_Click(object sender, EventArgs e)
        {
            if (t_to.SelectedIndex != -1)
                t_to.Items.RemoveAt(t_to.SelectedIndex);
        }
        /*清空附件*/
        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            t_files.Text = string.Empty;
            paths.Clear();
        }
        /*添加附件*/
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            openFileDialog1.Filter = "所有文件(*.*)|*.*";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string[] files = openFileDialog1.FileNames;
                foreach (string file in files)
                {
                    paths.Add(file);
                    t_files.Text  = Path.GetFileName(file)   "  ";
                }
            }
        }
        /*从文件导入*/
        private void button5_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "文本文件(*.txt)|*.txt";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs);
                string oneEmail;
                Regex myreg = new Regex(@"^\w ([- .]\w )*@\w ([-.]\w )*\.\w ([-.]\w )*$"); //email验证          
                while (sr.Peek()!=-1)
                {
                    oneEmail=sr.ReadLine();
                    if (myreg.IsMatch(oneEmail))
                        t_to.Items.Add(oneEmail);
                }
            }
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            toolStripProgressBar1.Visible = false;
        }
        /*帮助*/
        private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            help hel = new help();
            hel.Show();
        }
        //添加QQ邮箱功能
        private void button6_Click(object sender, EventArgs e)
        {
            QQ q = new QQ();
            if (q.ShowDialog() == DialogResult.OK && q.dic.Count != 0)
            {
                for (int i = 0; i < q.dic.Count ; i  )
                {
                    
                    string e_mail = q.dic[int.Parse(q.qq) i];
                    if (!t_to.Items.Contains(e_mail))
                    {
                        t_to.Items.Add(e_mail);
                    }
                }
            }
        }
 
        private void button7_Click(object sender, EventArgs e)
        {
            AddSender addsender = new AddSender();
            if (addsender.ShowDialog() == DialogResult.OK && !senderLst.Items.Contains(addsender.senderstring))
            {
                senderLst.Items.Add(addsender.senderstring);
            }
             
        }
 
    }
}

标签: 邮件 C# c

实例下载地址

C#发送邮件实例代码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警