我试图使用Mailaddress类发送多封电子邮件(>10封),但显然,它不喜欢它。有没有办法在6号之后把电子邮件附加到CC上?
或者其他的工作?
我有:
(<email1@test.com>; <email2@test.com>; <email3@test.com>, <email4@test.com>; <email5@test.com>; \r\n\t<email6@test.com>, <email7@test.com>; <email8@test.com>\r\n\TEXT)我使用Environment.NewLine,将<、>、\t和"“替换为”“(不知道其他更好的格式化方法)
当我试图通过邮件地址类发送它时,我得到了一个格式错误。但是,当电子邮件的数量减少时,它就能正常工作了。
解决: 字符串to = "";字符串cc = "";int i= 0;foreach ( multiAddress.Split(',‘)中的字符串项){i += 1;if (i < 10) { to += item + ",";}{ cc +=项+ ",";} to = to.Remove(to.Length - 1);cc = cc.Remove(cc.Length - 1);
发布于 2016-06-21 08:16:22
为什么你不能用常规的方式发送电子邮件到多个地址?变成一个字符串,用逗号分隔如下:
string recipients="email1@test.com,email2@test.com,email3@test.com" etc..当我试图发送到9个以上的收件人时,我个人遇到了一个错误,所以我编写了下面的代码片段,在第9个收件人之后,它会自动将收件人移动到CC字段。
var emailAddresses= "YourEmailAddresses";
//conccatenat all the email addresses into one variable
//if there is more than 9 recipients it moves them to the CC field
string to="";
string cc = "";
int i = 0;
foreach (string item in emailAddresses) {
i += 1;
if (i < 10) {
to += item + ",";
}
else
{
cc += item + ",";
}
to = to.Remove(to.Length - 1);如果您使用循环,请确保删除字符串上的最后一个逗号(因为它在每个条目之后添加了一个逗号,因此在最后一封电子邮件之后将有一个额外的逗号)。
发布于 2016-06-21 08:34:32
您可以使用正则表达式将邮件地址与其他字符隔离开来,例如:
(<)(\w+@\w+\.\w+)(>)将匹配任意角度之间的邮件地址。
string source = "(<email1@test.com>; <email2@test.com>; <email3@test.com>, <email4@test.com>; <email5@test.com>; \r\n\t<email6@test.com>, <email7@test.com>; <email8@test.com>\r\nTEXT)";
Regex regx = new Regex(@"(<)(\w+@\w+\.\w+)(>)");
MatchCollection matches = regx.Matches(source);
foreach (Match match in matches) {
Console.WriteLine(match.Groups[2].Value);
}这是一个运行示例。
发布于 2016-06-21 10:07:39
您还可以编写代码以获得cc邮件,如下所示,使用常规的C#代码,使用skip方法
public static string[] GetCCEmails()
{
const int limitToIncludeCC = 10 ;// no of email to be skipped
string[] emailAdresses = new string[] { "emailadress1@xx.com", "emailadress2@xx.com", "emailadress2@xx.com", "emailadress3@xx.com", "emailadress4@xx.com", "emailadress5@xx.com" };
var ccmailIDs = emailAdresses.Skip(limitToIncludeCC);
return ccmailIDs.ToArray();
}并将此数组分配给System.Net.Mail的System.Net.Mail类的CC属性。
https://stackoverflow.com/questions/37938935
复制相似问题