Skip to main content
 首页 » 编程设计

C# 使用地址列表发送电子邮件

2024年08月06日45jillzhang

只是有一个简短的问题要问你。前段时间我做了一个小应用程序,最后向 4 个收件人发送了一封电子邮件。但是,我对地址进行了硬编码。回去更改内容,我想将地址放在一个 .csv 文件中,我会读入它。这样,我可以编辑 .csv 来控制电子邮件地址。

我只是不确定您如何在代码中设置收件人。显然 MailAddress 的变量名称必须不同,所以我真的不知道如何设置循环,或者正确的方法是什么。

下面是我使用的代码,注释掉的是我的新代码,它将文件中的所有电子邮件地址读入列表。您能否指出正确的方向,告诉我如何从那里将电子邮件发送到列表中的所有地址?

谢谢,

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.IO; 
using System.Net; 
using System.Net.Mail; 
using System.Net.Mime; 
using System.Data; 
using Microsoft.Office.Interop; 
using Microsoft.Office.Interop.Excel; 
using Microsoft.Office.Core; 
using System.Reflection; 
using System.Drawing; 
using System.Globalization; 
 
namespace ReportEdit 
{ 
    class sendEmail 
    { 
        public static void SendMyMail() 
        { 
 
 
           /* !!NEW!! READING IN THE .CSV TO GET LIST OF ADDRESSES. Below comment is old  
 
            List<string> addyList = new List<string>(); 
            foreach (string line in File.ReadLines(Properties.Settings.Default.AddyList)) 
            { 
                addyList.Add(line); 
            } 
            */ 
 
            SmtpClient companySmtpClient = new SmtpClient("smtprelay.company.com"); 
 
            companySmtpClient.UseDefaultCredentials = true; 
 
            MailAddress from = new MailAddress("ActiveBatchRunReport@company.com", "ActiveBatchRunReport"); 
            MailAddress to = new MailAddress("GrpDstISOne@company.com", "DstOne"); 
            MailAddress Recipient1 = new MailAddress("Bill@company.com", "Bill"); 
            MailAddress Recipient2 = new MailAddress("Tom@company.com", "Tom"); 
            MailAddress Recipient3 = new MailAddress("Gena@company.com", "Gena"); 
            MailAddress Recipient4 = new MailAddress("Clifford@company.com", "Clifford"); 
            MailAddress ccKate = new MailAddress("Kate@company.com", "Kate"); 
 
 
            MailMessage myMail = new System.Net.Mail.MailMessage(from, to); 
 
            myMail.To.Add(Recipient1); 
            myMail.To.Add(Recipient2); 
            myMail.To.Add(Recipient3); 
            myMail.To.Add(Recipient4); 
 
            myMail.CC.Add(ccKate); 
 
            myMail.Subject = "Daily Job Runs"; 
            myMail.SubjectEncoding = System.Text.Encoding.UTF8; 
 
            myMail.Body = "Attached you will find an Excel spreadsheet" + 
            "Total Job counts are listed at the bottom."; 
 
            myMail.BodyEncoding = System.Text.Encoding.UTF8; 
            myMail.IsBodyHtml = true; 
 
            myMail.Attachments.Add(new Attachment(@"PathToAttachment")); 
 
            companySmtpClient.Send(myMail); 
        } 
 
    } 
} 

请您参考如下方法:

这里是你如何做到的。

/* !!新!!阅读 .CSV 以获取地址列表。下面的评论是旧的 */

        List<string> addyList = new List<string>(); 
        foreach (string line in File.ReadLines(Properties.Settings.Default.AddyList)) 
        { 
            addyList.Add(line); 
            // to add Name, you need to store emailAddress and name in  certain way so that you can parse Name out of the line in here 
        } 
 
        SmtpClient companySmtpClient = new SmtpClient("smtprelay.company.com"); 
 
        companySmtpClient.UseDefaultCredentials = true; 
        MailAddress from = new MailAddress("ActiveBatchRunReport@company.com", "ActiveBatchRunReport"); 
 
        foreach(string address in addyList) 
        { 
            MailAddress to = new MailAddress(address); 
            myMail.To.Add(to) 
        } 
 
        MailAddress ccKate = new MailAddress("Kate@company.com", "Kate"); 
        MailMessage myMail = new System.Net.Mail.MailMessage(from, to); 
 
        myMail.CC.Add(ccKate); 
 
        myMail.Subject = "Daily Job Runs"; 
        myMail.SubjectEncoding = System.Text.Encoding.UTF8; 
 
        myMail.Body = "Attached you will find an Excel spreadsheet" + 
        "Total Job counts are listed at the bottom."; 
 
        myMail.BodyEncoding = System.Text.Encoding.UTF8; 
        myMail.IsBodyHtml = true; 
 
        myMail.Attachments.Add(new Attachment(@"PathToAttachment")); 
 
        companySmtpClient.Send(myMail);