C# personal mailbox to send mail to multiple mailboxes

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 send email  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            ///<summary>  
            ///Read the mailbox in excel  
            ///</summary>  
            SmtpClient client = new SmtpClient();//Generate an instance of SmtpClient and use it to send emails  
            client.UseDefaultCredentials = true;  
            client.Host = "smtp.163.com";//Specify the SMTP server host  
            client.Port = 25;//Specify the port to use, this is the default port  
            string username = "Sender Email";  
            string pwd = "Sender Email Password";  
            System.Net.NetworkCredential nc = new System.Net.NetworkCredential(username, pwd);//The user and password of the sender's mailbox. Change the password to the authorization code number  
            client.Credentials = nc.GetCredential(client.Host, client.Port, "NTLM");  
            MailMessage message = new MailMessage();  
            MailAddress from = new MailAddress("Sender's Email");//Get the sender's email address  
            message.From = from;//Set the mail sender   
            IWorkbook workbook = null;//Create a new IWorkbook object  
            string fileName = @"Your Excel Sheet";  
            FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);  
            if (fileName.IndexOf(".xlsx") > 0)  
            {  
                workbook = new XSSFWorkbook(fileStream);  
            }  
            ISheet sheet = workbook.GetSheetAt(0);//Get the first worksheet  
            IRow row;//New current worksheet row data  
            string cellValue = "";  
            StreamWriter sw = null;  
            StreamWriter sw1 = null;  
            MailAddress to = null;  
            Attachment attach = new Attachment(@"Add your attachment path");//Get the selected attachment  
            Attachment attach1 = new Attachment(@"Add your attachment path");  
            message.Attachments.Add(attach);//Add attachments to the email, the Attachments property of the MailMessage class can add multiple attachments  
            message.Attachments.Add(attach1);  
            for (int i = 1; i <= sheet.LastRowNum; i++)  
            {  
  
                row = sheet.GetRow(i);//row reads the data of row i  
                if (row != null)  
                {  
                    for (int j = 0; j < row.LastCellNum; j++) //For each column of the worksheet    
                    {  
                        cellValue = row.GetCell(0).ToString(); //Get i row 0 column data      
                        message.Subject = "Subject to send mail";//Get the subject of the input mail  
                        message.Body = "The content of the message sent";  
                        message.IsBodyHtml = true;//Set to HTML format  
                        to = new MailAddress(cellValue);  
                    }  
                }  
                 
                //Set the sleep time randomly  
                Random random = new Random();  
                int time = random.Next(120, 180) * 1000;  
                Thread.Sleep(time);  
                string shijian = "Currently dormant"+(time / 1000) + "seconds";//The dormancy time should be more than 2 minutes as much as possible, the enterprise mailbox can send 1000 emails, and the free enterprise mailbox can send 500 mailboxes.
                string shoujianren = "Email:" + cellValue + "Sent";  
                string shoujianrenRow = "Current row number----" + i + "row";  
                try {   
                client.Send(message);  
                Console.WriteLine(shijian);  
                Console.WriteLine(shoujianren);  
                message.To.Clear();//Clear the recipient collection  
                message.To.Add(to);//Recipient collection
                //Record recipient information text  
                sw = new StreamWriter("D:\\send email\\send recipient's details (sleep time, whether to send, read the current number of rows in excel, system current time format)", true);  
                System.DateTime currentTime = new System.DateTime();//Get system time and time format  
                currentTime = System.DateTime.Now;  
                sw.WriteLine(shijian);  
                sw.WriteLine(currentTime);  
                sw.WriteLine(shoujianren);  
                sw.WriteLine(shoujianrenRow);  
                sw.Flush();  
                  
                //Only record recipient email  
                sw1 = new StreamWriter("D:\\send email\\recipient's mailbox.txt", true);//recipient mailbox  
                sw1.WriteLine(cellValue);  
                sw1.Flush();  
                  
                }  
                catch(Exception ex)  
                {  
                    StreamWriter sw2 = null;  
                    sw2 = new StreamWriter("D:\\send email\\wrong recipient's mailbox.txt", true);//wrong recipient's mailbox  
                    sw2.WriteLine(cellValue);//Start writing  
                    sw2.Flush();//Empty the buffer  
                    sw2.Close();  
                }  
                sw1.Close();  
                sw.Close();//Close the stream                  
            }  
            Console.ReadLine();  
            fileStream.Close();//Close the stream    
            workbook.Close();//Close the stream    
        }  
    }  
}  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324769762&siteId=291194637
Recommended