C#个人邮箱发邮件给多个邮箱

时间:2022-09-24 12:57:22
using NPOI.SS.UserModel;  
using NPOI.XSSF.UserModel;  
using System;  
using System.Collections.Generic;  
using System.IO;  
using System.Linq;  
using System.Net;  
using System.Net.Mail;  
using System.Runtime.Remoting.Contexts;  
using System.Text;  
using System.Threading;  
using System.Threading.Tasks;  
namespace 发邮件  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            ///<summary>  
            ///读取到excel中的邮箱  
            ///</summary>  
            SmtpClient client = new SmtpClient();//生成SmtpClient实例,用它发送电子邮件  
            client.UseDefaultCredentials = true;  
            client.Host = "smtp.163.com";//指定SMTP服务器主机  
            client.Port = 25;//指定要使用的端口,这个是默认的端口  
            string username = "发件人邮箱";  
            string pwd = "发件人邮箱密码";  
            System.Net.NetworkCredential nc = new System.Net.NetworkCredential(username, pwd);//发件人邮箱的用户和密码.中的密码更改为授权码号  
            client.Credentials = nc.GetCredential(client.Host, client.Port, "NTLM");  
            MailMessage message = new MailMessage();  
            MailAddress from = new MailAddress("发件人邮箱");//获取输入的发件人的邮箱地址  
            message.From = from;//设置邮件发件人   
            IWorkbook workbook = null;//新建IWorkbook对象  
            string fileName = @"你的Excel表格";  
            FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);  
            if (fileName.IndexOf(".xlsx") > 0)  
            {  
                workbook = new XSSFWorkbook(fileStream);  
            }  
            ISheet sheet = workbook.GetSheetAt(0);//获取第一个工作表  
            IRow row;//新建当前工作表行数据  
            string cellValue = "";  
            StreamWriter sw = null;  
            StreamWriter sw1 = null;  
            MailAddress to = null;  
            Attachment attach = new Attachment(@"添加你的附件路径");//获取选择的附件  
            Attachment attach1 = new Attachment(@"添加你的附件路径");  
            message.Attachments.Add(attach);//将附件添加到邮件中,MailMessage类的Attachments属性可以Add多个附件  
            message.Attachments.Add(attach1);  
            for (int i = 1; i <= sheet.LastRowNum; i++)  
            {  
  
                row = sheet.GetRow(i);//row读取第i行数据  
                if (row != null)  
                {  
                    for (int j = 0; j < row.LastCellNum; j++)  //对工作表每一列    
                    {  
                        cellValue = row.GetCell(0).ToString(); //获取i行0列数据      
                        message.Subject = "发送邮件的主题";//获取输入的邮件标题  
                        message.Body = "发送邮件的内容";  
                        message.IsBodyHtml = true;//设置为HTML格式  
                        to = new MailAddress(cellValue);  
                    }  
                }  
                 
                //随机设置休眠时间  
                Random random = new Random();  
                int time = random.Next(120, 180) * 1000;  
                Thread.Sleep(time);  
                string shijian = "当前已经休眠"+(time / 1000) + "秒钟";//休眠时间尽量2分钟以上,企业邮箱可以发送1000份邮件,免费企业邮箱可以发送500份邮箱。
                string shoujianren = "邮箱:" + cellValue + "已发送";  
                string shoujianrenRow = "当前行数----第" + i + "行";  
                try {   
                client.Send(message);  
                Console.WriteLine(shijian);  
                Console.WriteLine(shoujianren);  
                message.To.Clear();//清空收件人集合  
                message.To.Add(to);//收件人集合 
                //记录收件人信息文本  
                sw = new StreamWriter("D:\\发邮件\\发送收件人的详细信息(休眠时间、是否发送、读取excel当前行数、系统当前时间格式)", true);  
                System.DateTime currentTime = new System.DateTime();//获取系统时间及时间格式  
                currentTime = System.DateTime.Now;  
                sw.WriteLine(shijian);  
                sw.WriteLine(currentTime);  
                sw.WriteLine(shoujianren);  
                sw.WriteLine(shoujianrenRow);  
                sw.Flush();  
                  
                //只记录收件人邮箱  
                sw1 = new StreamWriter("D:\\发邮件\\收件人的邮箱.txt", true);//收件人邮箱  
                sw1.WriteLine(cellValue);  
                sw1.Flush();  
                  
                }  
                catch(Exception ex)  
                {  
                    StreamWriter sw2 = null;  
                    sw2 = new StreamWriter("D:\\发邮件\\错误的收件人的邮箱.txt", true);//错误的收件人邮箱  
                    sw2.WriteLine(cellValue);//开始写入  
                    sw2.Flush();//清空缓冲区  
                    sw2.Close();  
                }  
                sw1.Close();  
                sw.Close();//关闭流                  
            }  
            Console.ReadLine();  
            fileStream.Close();//关闭流    
            workbook.Close();//关闭流    
        }  
    }  
}