• 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("邮件发送失败!" + "
    " + "技术信息:
    " + e.Error.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        MessageBox.Show("邮件成功发出!", "恭喜!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                catch (Exception Ex)
                {
                    MessageBox.Show("邮件发送失败!" + "
    " + "技术信息:
    " + Ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

                                                              2013-12-19  11:05:05

  • 相关阅读:
    SPSS时间序列:频谱分析
    PureBasic—数控编辑框与调节块和进度条
    DELPHI2007 安装ACTIVEX插件的方法
    C++ builder的文件操作
    C++动态数组
    excel快速复制大量公式的方法
    Delphi XE5 如何与其他版本共存
    PureBasic 集成Form设计器的使用
    VS C++ 从一个窗口创建另一个窗口
    ENVI 5.0 Beta 体验——影像数据的显示
  • 原文地址:https://www.cnblogs.com/xw-yanger/p/3481743.html
Copyright © 2020-2023  润新知