刚刚给你一个简单的问题。我之前做了一个小应用程序,最后发送了一封电子邮件给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);
}
}
}
答案 0 :(得分:2)
以下是如何做到这一点。
/ * !!新!!阅读.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);
答案 1 :(得分:1)
我建议您将addyList
设为字典而不是列表。这将允许您存储电子邮件和名称,因为您似乎在硬编码版本中使用这两者。假设你这样做,并且成功地解析了你的CSV(它看起来你还没有完全存在),如果用你的代码替换类似的硬编码调用,下面应该可以解决这个问题:
foreach (var kvp in addyList) {
myMail.To.Add(new MailAddress(kvp.Key, kvp.Value);
}
此循环假定您使用电子邮件地址作为键,并将名称作为字典中的值。如果你交换它,只需交换.Key和.Value用法。
此外,如果app.config或web.config设置可以使用CSV,则CSV可能会出现问题。我建议您将其作为一种选择。
答案 2 :(得分:0)
如果我理解正确,您可以获取CSV文件的内容。使用该列表,您可以将它们添加到&#39; To&#39;邮件消息对象的属性。
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文件解析的帮助,请告诉我。