我用这个来发送邮件,我定义了一些包含值的包变量。
public MailProperties(ScriptObjectModel dts)
{
if (
!string.IsNullOrEmpty((string)dts.Variables["$Package::mailFrom"].Value) &&
!string.IsNullOrEmpty((string)dts.Variables["$Package::mailTo"].Value) &&
!string.IsNullOrEmpty((string)dts.Variables["$Package::mailPwd"].GetSensitiveValue()) &&
!string.IsNullOrEmpty((string)dts.Variables["$Package::mailSmtp"].Value) &&
!string.IsNullOrEmpty((string)dts.Variables["$Package::mailPort"].Value)
)
{
fromMail = (string)dts.Variables["$Package::mailFrom"].Value;
toMail = (string)dts.Variables["$Package::mailTo"].Value;
bccMail = (string)dts.Variables["$Package::mailBcc"].Value;
accountPassword = (string)dts.Variables["$Package::mailPwd"].GetSensitiveValue();
accountSmtp = (string)dts.Variables["$Package::mailSmtp"].Value;
accountSmtpPort = (string)dts.Variables["$Package::mailPort"].Value;
useSSL = (bool)dts.Variables["$Package::useSSL"].Value;
pathAttachment = new List<string>();
pathAttachment.Add((string)dts.Variables["User::pathFileReject"].Value);
pathAttachment.Add((string)dts.Variables["User::pathFileReject2"].Value);
}
else
{
throw new Exception("error text...");
}
}
我需要向更多的人发送邮件,所以我把mailTo设置为[email protected],mailBcc设置为[email protected],这样就可以了,但是如果我把mailBcc设置为 "[email protected],[email protected] "或者 "[email protected];[email protected] "就不行了,我该怎么做呢?
EDIT:这是sendMail的功能
public static void sendMail(MailProperties mailProperties, ReportETL reportETL)
{
MimeMessage message = new MimeMessage();
message.From.Add(new MailboxAddress("text..", mailProperties.fromMail));
message.To.Add(new MailboxAddress(mailProperties.toMail));
message.Subject = "text...";
if (!string.IsNullOrEmpty(mailProperties.bccMail))
{
message.Bcc.Add(new MailboxAddress(mailProperties.bccMail));
}
BodyBuilder bodyBuilder = new BodyBuilder();
bodyBuilder.HtmlBody = ReportETLService.getHtmlFromReporETL(reportETL);
mailProperties.pathAttachment.Where(x => File.Exists(x)).ToList().ForEach(y => bodyBuilder.Attachments.Add(y));
message.Body = bodyBuilder.ToMessageBody();
try
{
SmtpClient smtpClient = new SmtpClient();
smtpClient.Connect(mailProperties.accountSmtp, int.Parse(mailProperties.accountSmtpPort), mailProperties.useSSL);
smtpClient.Authenticate(mailProperties.fromMail, mailProperties.accountPassword);
smtpClient.Send(message);
smtpClient.Disconnect(true);
}
catch (Exception e) { throw new Exception("text... " + e.Message); }
}
根据文档 message.To
是列表类型.所以你可以像这样添加更多的地址。
InternetAddressList list = new InternetAddressList();
list.add(adress1)
list.add(adress2)
list.add(adress3)
list.add(adress4)
message.To.AddRange(list);
而不是使用BCC(这也是一个选项,但有最大计数的问题)只是迭代所有的邮件地址,并为每个收件人发送一个。(不要太快,否则会有其他问题。)这样一来,应该可以 "更容易 "地发现那些不会被送达的邮件。
那么,假设你的 toMail
, ccMail
和 bccMail
属性只是字符串而不是字符串列表,你可以这样做。
InternetAddressList list;
if (!string.IsNullOrEmpty(mailProperties.toMail) && InternetAddressList.TryParse (mailProperties.toMail, out list))
message.To.AddRange(list);
if (!string.IsNullOrEmpty(mailProperties.ccMail) && InternetAddressList.TryParse (mailProperties.ccMail, out list))
message.Cc.AddRange(list);
if (!string.IsNullOrEmpty(mailProperties.bccMail) && InternetAddressList.TryParse (mailProperties.bccMail, out list))
message.Bcc.AddRange(list);