C# 邮件发送,可根据需求修改为群发

我直接上图 上代码吧。

 

代码:



using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Text;  
using System.Windows.Forms;  
using System.Net;  
using System.Net.Mail;  
using System.Net.Mime;  
using System.IO;  
  
namespace SendMailExample  
{  
    /// <summary>  
    /// 作者:Andrew  
    /// Blog: http://blog.csdn.net/Andrew_wx  
    /// </summary>
    public partial class FormSendMail : Form  
    {  
        public FormSendMail()  
        {  
            InitializeComponent();  
        }  
  
        private void FormSendMail_Load(object sender, EventArgs e)  
        {  
            txtSmtpServer.Text = "smtp.qq.com";  
            txtSend.Text = "heuandmei@qq.com";  
            txtDisplayName.Text = "Andrew(王旭)";  
            txtPassword.Text = "";//密码  
            txtReceive.Text = "heuandmei@qq.com";  
            txtTitle.Text = "发信测试";  
            txtBody.Text = "This is a test(测试)";  
            rbtnNoSSL.Checked = true;  
        }  
  
        private void btnAddFiles_Click(object sender, EventArgs e)  
        {  
            OpenFileDialog odlg = new OpenFileDialog();  
            odlg.CheckFileExists = true;  
            //只接收有效的文件名  
            odlg.ValidateNames = true;  
            //允许一次选择多个文件作为附件  
            odlg.Multiselect = true;  
            if (odlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)  
            {  
                lstFiles.Items.AddRange(odlg.FileNames);  
            }  
              
        }  
  
        private void btnSend_Click(object sender, EventArgs e)  
        {  
            this.Cursor = Cursors.WaitCursor;  
            MailMessage mail = new MailMessage();  
            mail.From = new MailAddress(  
                txtSend.Text, txtDisplayName.Text, Encoding.UTF8);  
            mail.To.Add(txtReceive.Text);  
            mail.Subject = txtTitle.Text;  
            mail.SubjectEncoding = Encoding.Default;  
            mail.Body = txtBody.Text;  
            mail.BodyEncoding = Encoding.Default;  
            mail.IsBodyHtml = false;  
            mail.Priority = MailPriority.Normal;  
            //添加附件  
            Attachment attachment = null;  
            if (lstFiles.Items.Count > 0)  
            {  
                for (int i = 0; i < lstFiles.Items.Count; i++)  
                {  
                    string pathFileName = lstFiles.Items[i].ToString();  
                    string extName = Path.GetExtension(pathFileName).ToLower();  
                    //判断附件类型  
                    if (extName == ".rar" || extName == ".zip")  
                    {  
                        attachment = new Attachment(pathFileName, MediaTypeNames.Application.Zip);  
                    }  
                    else  
                    {  
                        attachment = new Attachment(pathFileName, MediaTypeNames.Application.Octet);  
                    }  
                    ContentDisposition cd = attachment.ContentDisposition;  
                    cd.CreationDate = File.GetCreationTime(pathFileName);  
                    cd.ModificationDate = File.GetLastWriteTime(pathFileName);  
                    cd.ReadDate = File.GetLastAccessTime(pathFileName);  
                    mail.Attachments.Add(attachment);  
  
                }  
            }  
            SmtpClient client = new SmtpClient();  
            client.Host = txtSmtpServer.Text;  
            client.Port = 25;  
            //是否使用安全套接字层加密连接  
            client.EnableSsl = rbtnUseSSL.Checked;  
            //不使用默认凭证,注意此句必须放在 client.Credentials 的上面  
            client.UseDefaultCredentials = false;  
            client.Credentials = new NetworkCredential(txtSend.Text, txtPassword.Text);  
            //邮件通过网络直接发送到服务器  
            client.DeliveryMethod = SmtpDeliveryMethod.Network;  
            try  
            {  
                client.Send(mail);  
                MessageBox.Show("发送成功");  
            }  
            catch (SmtpException ex)  
            {  
                MessageBox.Show("发送失败:" + ex.Message);  
            }  
            catch (Exception ex)  
            {  
                MessageBox.Show("发送失败:" + ex.Message);  
            }  
            finally  
            {  
                mail.Dispose();  
                client = null;  
                this.Cursor = Cursors.Default;  
            }  
        }  
    }  
} 


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值