c#发送邮件——支持网易和腾讯邮箱,可同时发送多人、多附件

该博客详细介绍了如何使用C#编程语言发送邮件,特别支持网易和腾讯邮箱,同时涵盖多收件人管理和附件添加功能,适合开发者进行邮件自动化处理。
摘要由CSDN通过智能技术生成

发送类

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Emeil
{
    public class SendReceiveEmail
    {
        #region ///属性声明
        private string _from;       //发件邮箱
        private string[] _to;       //收件邮箱列表
        private string _subject;    //主题
        private string _body;       //正文
        private string[] _filepath; //附件列表

        private string _usename;    //登录邮箱账号
        private string _password;   //密码

        private string _smtp;       //服务器
        private int _port;          //端口号

        public string From
        { get { return _from; } set { _from = value; } }
        public string[] To
        { get { return _to; } set { _to = value; } }

        public string Subject
        { get { return _subject; } set { _subject = value; } }

        public string Body
        { get { return _body; } set { _body = value; } }

        public string[] Filepath
        { get { return _filepath; } set { _filepath = value; } }


        public string Usename
        { get { return _usename; } set { _usename = value; } }

        public string Password
        { get { return _password; } set { _password = value; } }

        public string Smtp
        { get { return _smtp; } set { _smtp = value; } }

        public int Port
        { get { return _port; } set { _port = value; } }
        #endregion

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="from 发件邮箱"></param>
        /// <param name="to 收件邮箱"></param>
        /// <param name="subject 主题"></param>
        /// <param name="body 正文"></param>
        /// <param name="attachmentpath 附件"></param>
        /// <returns></returns>
        public bool SendEmail()
        {
            try
            {
                //定义一个mail对象
                MailMessage mailmessage = new MailMessage();
                //发信人地址
                mailmessage.From= new MailAddress(_from);
                //添加收件人
                for (int t = 0; t < _to.Length; t++)
                {
                    mailmessage.To.Add(new MailAddress(_to[t]));
                }
                //主题
                mailmessage.Subject = _subject;
                //正文
                mailmessage.Body = _body;
                //邮件优先级
                mailmessage.Priority = MailPriority.Normal; 
                    
                //添加附件
                Attachment attachment = null;
                //添加所有附件
                if (_filepath.Length > 0)
                {
                    for (int i = 0; i < _filepath.Length; i++)
                    {
                        string pathFileName = _filepath[i].ToString();
                        string extName = Path.GetExtension(pathFileName).ToLower(); //获取扩展名
                        if (extName == ".rar" || extName == ".zip") //.rar和.zip的文件属于压缩文件类型
                        {
                            attachment = new Attachment(pathFileName, MediaTypeNames.Application.Zip);
                        }
                        else
                        {
                            attachment = new Attachment(pathFileName, MediaTypeNames.Application.Octet);
                        }
                        //设置附件的MIME信息
                        ContentDisposition cd = attachment.ContentDisposition;
                        cd.CreationDate = File.GetCreationTime(pathFileName);//设置附件的创建时间
                        cd.ModificationDate = File.GetLastWriteTime(pathFileName);//设置附件的修改时间
                        cd.ReadDate = File.GetLastAccessTime(pathFileName);//设置附件的访问时间
                        mailmessage.Attachments.Add(attachment);//将附件添加到mailmessage对象
                    }
                }


            
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wyhmtt

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值