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

C# Sending an email using a list for addresses

刚刚有一个简短的问题要问你。前段时间我做了一个小应用程序,最后向 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);

我建议您将 addyList 设为字典而不是列表。这将使您同时存储电子邮件和姓名,因为您似乎在硬编码版本中同时使用了这两者。假设您这样做并成功解析了您的 CSV(看起来您还没有完成),如果您用它替换代码中类似的硬编码调用,则以下应该可以解决问题:

foreach (var kvp in addyList) {
    myMail.To.Add(new MailAddress(kvp.Key, kvp.Value);
}

此循环假定您在字典中使用电子邮件地址作为键,使用名称作为值。如果你交换它,只需交换 .Key 和 .Value 用法。

此外,如果 app.config 或 web.config 设置可以代替,CSV 对您的目的来说可能有点矫枉过正。我建议您将其作为一个选项进行研究。

如果我没理解错的话,你可以获取你的CSV文件的内容。使用该列表,您可以将它们添加到邮件消息对象的 'To' 属性。

        List<string> addyList = new List<string>();

        //populate string list from csv file

        MailMessage myMail = new System.Net.Mail.MailMessage();
        //populate this
        myMail.From = new MailAddress("ActiveBatchRunReport@company.com", "ActiveBatchRunReport");
        myMail.To.Add(string.Join(";", addyList));

如果您需要有关 csv 文件解析的帮助,请告诉我。