C#、.NET版邮件(群)发系统
这个小程序是之前做的,今天把它晾上来了。程序比较简单,,适合初学者学习。先给个截图:
实现单发和多发功能,可附加多份附件,限制总大小为1M,大于后无法发送。
默认发送格式为Html。
【修改】【添加】的操作是针对发件人的,点击【修改】弹出下图界面:
填充文本框的是在ThreadSendEmail.cs中的构造函数中
/// <summary> /// 发送邮件 /// </summary> /// <param name="strSenderEmail">发件人邮箱</param> /// <param name="strTitle">邮件标题</param> /// <param name="strBody">邮件正文</param> /// <param name="ToEmail">收件人邮箱</param> /// <param name="AttachmentFiles">附件</param> public ThreadSendEmail(string strSenderEmail, string strTitle, string strBody, string ToEmail, ArrayList AttachmentFiles) { this.strSenderEmail = strSenderEmail; strToEmail = ToEmail; strEmailTitle = strTitle; strEmailBody = strBody; arrAttachmentFiles = AttachmentFiles; using (FileStream stream = new FileStream(inforpath, FileMode.OpenOrCreate, FileAccess.Read)) { using (StreamReader read = new StreamReader(stream, Encoding.Default)) { string GetSenderInfor; while ((GetSenderInfor = read.ReadLine()) != null) { string[] line = GetSenderInfor.Trim().Split(' '); if (line[1] == strSenderEmail) { strSenderSMTP = line[0]; strSnederPwd = line[2]; } } } } }
当然,点击【添加】文本框中是没有内容的。
下面的函数是发送邮件函数
/// <summary> /// 发送邮件 /// </summary> /// <returns>true:发送成功 false:发送失败</returns> public bool Send() { SmtpClient client = new SmtpClient(); client.Host = strSenderSMTP; client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential(strSenderEmail, strSnederPwd); client.DeliveryMethod = SmtpDeliveryMethod.Network; MailMessage mm = new MailMessage(strSenderEmail, strToEmail, strEmailTitle, strEmailBody); mm.BodyEncoding = Encoding.Default; mm.IsBodyHtml = true; if (arrAttachmentFiles.Count != 0) { foreach (string attach in arrAttachmentFiles) { Attachment attachment = new Attachment(attach, System.Net.Mime.MediaTypeNames.Application.Octet); mm.Attachments.Add(attachment); } } try { client.Send(mm); MessageBox.Show("邮件发送成功", "成功", MessageBoxButtons.OK); return true; } catch (Exception ex) { MessageBox.Show("发送失败" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return false; } }
若为修改时,点击添加、修改发件人对话框确定按钮,程序将清空txt然后重新写入。具体代码为
确定按钮代码
1 private void btnOK_Click(object sender, EventArgs e)
2 {
3 NewInforLine = txtSMTP.Text + " " + txtSenderEmail.Text + " " + txtSenderPwd.Text;
4 Regex regex = new Regex(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"); //验证Email格式
5 if (regex.IsMatch(txtSenderEmail.Text.Trim()) && txtSenderPwd.Text.Trim() != "")
6 {
7 if (arg) //修改发件人
8 {
9 string[] content;
10 string[] content1;
11 try
12 {
13 content = File.ReadAllLines(PathBase + inforpath, Encoding.Default);
14 content1 = new string[content.Length];
15 for (int i = 0; i < content.Length; i++)
16 {
17 if (content[i] != OldInforLine)
18 {
19 content1[i] = content[i];
20 continue;
21 }
22 content1[i] = NewInforLine;
23 }
24
25 FileStream stream2 = File.Open(inforpath, FileMode.OpenOrCreate, FileAccess.Write);
26 stream2.Seek(0, SeekOrigin.Begin);
27 stream2.SetLength(0); //清空txt文件
28 stream2.Close();
29 File.WriteAllLines(PathBase + inforpath, content1);
30 }
31 catch (Exception ex)
32 {
33 MessageBox.Show(ex.Message);
34 }
35 }
36 else //添加发件人
37 {
38 using (FileStream stream = new FileStream(inforpath, FileMode.Append, FileAccess.Write))
39 {
40 using (StreamWriter write = new StreamWriter(stream, Encoding.Default))
41 {
42 string stremail = txtSMTP.Text + " " + txtSenderEmail.Text + " " + txtSenderPwd.Text;
43 write.WriteLine(stremail);
44 }
45 }
46 }
47 ClearContent();
48 this.DialogResult = System.Windows.Forms.DialogResult.OK;
49 }
50 else
51 {
52 MessageBox.Show("邮箱格式不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
53 this.DialogResult = System.Windows.Forms.DialogResult.None;
54 }
55 }
程序结构:
添加收件人按钮弹出
适合初学者,程序里面有注解,比较容易懂。