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.ServiceProcess;
namespace MailManagerSystem
{
    public partial class SendOneMailForm : Form
    {
        //附件list
        private ArrayList lstAttachments = null;
        //收件人地址
        private string strTo;        public string StrTo
        {
            get { return strTo; }
            set { strTo = value; }
        }        //构造函数
        public SendOneMailForm()
        {
            InitializeComponent();
        }
        // 浏览文件
        private void Browse_Click(object sender, EventArgs e)
        {
            //设置对话框初始属性
            openFileDialog.InitialDirectory = "C:\\";
            openFileDialog.Filter =
                "All Files (*.*)|*.*|HTML Files (*.htm;*.html)|*.htm|Microsoft Mail Documents (*.msg)|*.msg|Word Documents (*.doc)|*.doc|Excel Files(*.xl*)|*.xl*|Excel Worksheets (*.xls)|*.xls|Excel Charts (*.xlc)|*.xlc|PowerPoint Presentations (*.ppt)|*.ppt|Text Files (*.txt)|*.txt";
            openFileDialog.FilterIndex = 1;
            //打开文件对话框
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                if (lstAttachments == null)
                {
                    lstAttachments = new ArrayList();
                    lbxAttachments.Items.Clear();
                }
                //记录全路径文件名
                lstAttachments.Add(new Attachment(openFileDialog.FileName));
                //获得文件名
                string[] strFileName = openFileDialog.FileName.Split('\\');
                System.Array.Reverse(strFileName);
                lstAttachments.Add(strFileName[0]);
            }
        }
        // 发送邮件
        private void Send_Click(object sender, EventArgs e)
        {
            //检查收件人和发件人地址
            if (tbxTo.Text.Trim().Equals("") || tbxFrom.Text.Trim().Equals(""))
            {
                MessageBox.Show("请输入收信人和发信人地址!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            //创建邮件
            MailMessage objMailMessage = new MailMessage(tbxFrom.Text.Trim(), tbxTo.Text.Trim());           // if (cmbPriority.SelectedItem.ToString() == "Normal")
           // { objMailMessage.Priority}
            //添加抄送地址
            string[] strCC = tbxCC.Text.Split(';');
            for (int i = 0; i < strCC.Length; i++)
            {
                if (!strCC[i].Trim().Equals(""))
                {
                    objMailMessage.CC.Add(new MailAddress(strCC[i].Trim()));
                }
            }
            //添加暗送地址
            string[] strBCC = tbxBCC.Text.Split(';');
            for (int i = 0; i < strBCC.Length; i++)
            {
                if (!strBCC[i].Trim().Equals(""))
                {
                    objMailMessage.CC.Add(new MailAddress(strBCC[i]));
                }
            }
            //设置邮件主题
            objMailMessage.Subject = tbxSubject.Text;
            //设置邮件文本
            objMailMessage.Body = tbxBody.Text;
            //添加附件
            if (lstAttachments != null)
            {
                for (int i = 0; i < lstAttachments.Count; i++)
                {
                    objMailMessage.Attachments.Add((Attachment) lstAttachments[i]);
                }
            }
            //通过servicecontroller对象,查找smtp服务            ServiceController[] services = ServiceController.GetServices();
            //查找服务
            for (int i = 0; i < services.Length; i++)
            {
                if (services[i].ServiceName.ToLower().Equals("smtpsvc"))
                {
                    if (services[i].Status == ServiceControllerStatus.Running)
                    {
                        try
                        {
                            //启动服务
                            //services[i].Start();
                            //创建SMTP服务器
                            SmtpClient objClient = new SmtpClient("125.0.0.1");
                            //发送邮件
                            objClient.Send(objMailMessage);
                            MessageBox.Show("邮件发送完毕", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        catch (Exception exp)
                        {
                            MessageBox.Show(exp.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    break;
                }
            }
        }
        //画面初始化
        private void SendOneMailForm_Load(object sender, EventArgs e)
        {
            tbxTo.Text = StrTo;
        }        private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            fontDialog1 .ShowDialog();
            fontDialog1.AllowVectorFonts=true;
            fontDialog1.FixedPitchOnly=true;
            fontDialog1 .ShowApply=true;
            fontDialog1.ShowEffects=true ;
            tbxBody.Font=fontDialog1.Font;
        }        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            
        }        private void toolStripButton2_Click(object sender, EventArgs e)
        {           
        }        private void toolStripButton1_Click_1(object sender, EventArgs e)
        {
            if (fontDialog1.ShowDialog() == DialogResult.OK)
            {
                this.tbxBody.Font = this.fontDialog1.Font;
            } 
        }        private void toolStripButton2_Click_1(object sender, EventArgs e)
        {
            this.colorDialog1.AllowFullOpen = true;
            this.colorDialog1.AnyColor = true;
            this.colorDialog1.FullOpen = true;
            this.colorDialog1.SolidColorOnly = false;
            this.colorDialog1.ShowDialog();
            this.tbxBody.ForeColor = this.colorDialog1.Color;
        }                       
    }
}