C# 郵件發送系統

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net;
using System.IO;
using System.Net.Mime;
using System.Threading;
服務引用

 

private void Form1_Load(object sender, EventArgs e)
        {
            comboBoxEx3.SelectedIndex = 0;
            txtMailFrom.SelectedIndex = 0;
            txtSmtpServer.SelectedIndex = 0;
            txtMailTo.SelectedIndex = 0;
        }
        //發送郵件
        private void btnSend_Click(object sender, EventArgs e)
        {
            if (IsValid())
            {
                if (MessageBox.Show("您肯定要發送當前郵件嗎?", "詢問", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    string serverHost = "smtp." + txtSmtpServer.Text + comboBoxEx3.Text; //smtp 地址
                    int port = Int32.Parse(numericUpDown1.Value.ToString());    //smtp 密碼
                    string mailFrom = txtMailFromEx.Text + "@" + txtMailFrom.Text; //發信人,必須是提供smtp服務的郵件服務器 
                    string mailPwd = txtformPwd.Text;   //密碼
                    string displayName = txtDisplayName.Text;   //顯示名
                    string mailSubject = txtMailSubject.Text;   //主題
                    string mailBody = txtMailBody.Text;   //正文
                    string file = string.IsNullOrEmpty(txtPath.Text) ? null : txtPath.Text;
                    int contactNum = dgvContact.Rows.Count - 1; //聯繫人數量
                    //循環給每一個聯繫人發送指定數量的郵件
                    for (int i = 0; i < contactNum; i++)
                    {
                        string mailTo = dgvContact.Rows[i].Cells[0].Value.ToString();  //收件人
                        //發送指定數量的郵件
                        for (int j = 1; j <= Int32.Parse(txtNum.Text); j++)
                        {
                            try
                            {
                                EmailHelper.SendEmail(serverHost, port, mailFrom, mailPwd, displayName, mailTo, mailSubject, mailBody, file);
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message);
                            }
                        }
                    }
                }
            }
        }
        //驗證輸入是否有誤
        private bool IsValid()
        {
            //檢測附件大小 發件必需小於10M 不然返回  不會執行如下代碼
            if (txtPath.Text != "")
            {
                if (!EmailHelper.CheckAttachment(txtPath.Text.Trim()))
                {
                    return false;
                }
            }
            if (txtSmtpServer.Text == "")
            {
                MessageBox.Show("請輸入SMTP服務器名!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (txtMailFromEx.Text == "")
            {
                MessageBox.Show("請輸入發件人郵箱地址!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (txtformPwd.Text == "")
            {
                MessageBox.Show("請輸入發件人郵箱密碼!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            if (dgvContact.Rows.Count <= 1)
            {
                MessageBox.Show("請添加收件人!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }
        //添加聯繫人
        int i = 1;  //拼接聯繫人描述字段,加入聯繫人框
        private void btnAddContact_Click(object sender, EventArgs e)
        {
            string Contact = txtMailToEx.Text + "@" + txtMailTo.Text;
            for (int m = 0; m < dgvContact.Rows.Count - 1; m++)
            {
                if (dgvContact.Rows[m].Cells[0].Value.ToString().Equals(Contact))
                {
                    MessageBox.Show("已存在!");
                    return;
                }
            }
            string[] row1 = new string[] { Contact, "Contacter" + i };
            dgvContact.Rows.Add(row1);
            i++;
        }
        //移除聯繫人
        private void btnDelContact_Click(object sender, EventArgs e)
        {
            dgvContact.Rows.RemoveAt(dgvContact.Rows.Count - 2);
            i--;
        }
        //添加附件
        private void btnAttachment_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtPath.Text = openFileDialog1.FileName;
            }
        }
        // 發送郵件後所處理的函數
        private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
            try
            {
                if (e.Cancelled)
                {
                    MessageBox.Show("發送已取消!");
                }
                if (e.Error != null)
                {
                    MessageBox.Show("郵件發送失敗!" + "\n" + "技術信息:\n" + e.Error.ToString(), "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show("郵件成功發出!", "恭喜!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show("郵件發送失敗!" + "\n" + "技術信息:\n" + Ex.Message, "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

                                                          2013-12-19  11:05:05服務器

相關文章
相關標籤/搜索