我正在尝试将电子邮件发送给单个“To'收件人,以及' Bcc'收件人。 密件抄送收件人列表是一个字符串列表,它们已成功添加到mailMessage的密件抄送集合中,但实际上并未发送。如果我将相同的列表添加到消息“Cc'集合它工作正常。不是Bcc系列。 我使用的代码是:
public void SendEmailMessage(String FromAddress, String ToAddress, String Subject, String Body, List<String> CCAddress, List<String> BccAddress, String Filepath)
{
using (SmtpClient mailClient = new SmtpClient())
{
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(FromAddress);
mailMessage.To.Add(new MailAddress(ToAddress));
foreach (String _email in CCAddress)
{
mailMessage.CC.Add(new MailAddress(_email));
}
foreach (String _email in BccAddress)
{
mailMessage.Bcc.Add(new MailAddress(_email));
}
mailMessage.Priority = MailPriority.Normal;
mailMessage.Subject = Subject;
if (Filepath != string.Empty)
{
Attachment _attachment = new Attachment(Filepath, MediaTypeNames.Application.Octet);
mailMessage.Attachments.Add(_attachment);
}
AlternateView plainTextView = AlternateView.CreateAlternateViewFromString(GetTextonly(Body), null, "text/plain");
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(Body, null, "text/html");
mailMessage.AlternateViews.Add(plainTextView);
mailMessage.AlternateViews.Add(htmlView);
SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
smtpClient.Send(mailMessage);
}
}
任何想法?
答案 0 :(得分:2)
我没有提到的一件事是邮件放在皮卡目录中而不是直接发送。 我找到了一个博客,说明如果使用拾取目录,则不会发送密件抄送地址,您可以将它们放在重试目录中。这通过简单的修复解决了我的问题: Unable to send Bcc using System.Net.Mail when specifying a Pickup Directory(Exchange 2007/Exchange 2010) in code
答案 1 :(得分:0)
我创建了一个测试应用程序并运行SmptForDev来捕获从本地IIS发出的任何电子邮件。我使用下面的代码,它工作正常。我对你的代码所做的一切都是整理它并且工作正常。我还反编译了System.Net.Mail.SmtpClient以查看它在幕后的作用,To地址和Bcc地址都被放入一个集合中,如果一个地址发送它,那么它们都可以假设它们都是。< / p>
public void SendEmailMessage(string fromAddress, string toAddress, string subject, string body, IEnumerable<string> ccAddress, IEnumerable<string> bccAddress, string filepath)
{
using (var mailClient = new SmtpClient())
{
var mailMessage = new MailMessage(fromAddress, toAddress);
foreach (var email in ccAddress)
{
mailMessage.CC.Add(new MailAddress(email));
}
foreach (var email in bccAddress)
{
mailMessage.Bcc.Add(new MailAddress(email,"Matty Boy"));
}
mailMessage.Priority = MailPriority.Normal;
mailMessage.Subject = subject;
if (!string.IsNullOrEmpty(filepath))
{
var attachment = new Attachment(filepath, MediaTypeNames.Application.Octet);
mailMessage.Attachments.Add(attachment);
}
var plainTextView = AlternateView.CreateAlternateViewFromString(GetTextonly(body), null, "text/plain");
var htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
mailMessage.AlternateViews.Add(plainTextView);
mailMessage.AlternateViews.Add(htmlView);
mailClient.Send(mailMessage);
}
}
答案 2 :(得分:0)
作为解决方法,您可以将您的电子邮件明确发送到BCC地址。
成功发送电子邮件后:
mailClient.Send(mailMessage);
清除收件人地址集合,然后将您的BCC地址添加为收件人地址,然后重新发送。
mailMessage.To.Clear(); // clear the existing To & Cc fields
mailMessage.Cc.Clear();
mailMessage.To.Add(new MailAddress("bcc@address.com","CopyAddress"));
mailClient.Send(mailMessage);