簡體   English   中英

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成為一個Dictionary而不是List。 這將允許您存儲電子郵件和名稱,因為您似乎在硬編碼版本中使用這兩者。 假設你這樣做,並成功解析你的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文件解析的幫助,請告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM