发送邮件给多个收件人时,如何解决抛出的异常?人时、多个、如何解决、抛出

2023-09-07 15:39:49 作者:感动中国

在下面的code段,我越来越对this.Recipients出现FormatException。更具体地讲,该消息是无效的字符被发现在邮件标题:';'。

In the code snippet below, I'm getting a FormatException on 'this.Recipients'. More specifically, the message is "An invalid character was found in the mail header: ';'".

收件人是三个电子邮件地址字符串用分号分隔(的';'字符)。收件人的列表是从一个app.config读取和数据使之成为收件人变量

Recipients is a string of three email addresses separated by semicolons (the ';' character). The list of recipients is read from an app.config and the data is making it into the Recipients variable.

我怎么能得到这个错误时,多个收件人应该用分号隔开?有什么建议么?与往常一样,感谢您的帮助!

How can I be getting this error when multiple recipients should be separated by a semicolon? Any suggestions? As always, thanks for your help!

public bool Send()
{
    MailMessage mailMsg = 
       new MailMessage(this.Sender, this.Recipients, this.Subject, this.Message);

    SmtpClient smtpServer = new SmtpClient(SMTP);
    smtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;

编辑#1 - 这说,使用分号

推荐答案

我无法看到的 MailMessage构造文档建议你可以指定多个收件人之类的。我建议你​​创建 MailMessage 对象,然后分别添加每个电子邮件地址。

I can't see anything in the MailMessage constructor documentation to suggest you can specify multiple recipients like that. I suggest you create the MailMessage object and then add each email address separately.

请注意,MailAddressCollection.Add方法进行了说明,接受的逗号的 - 分隔地址......所以这是的可能的,这将在构造函数中工作了。

Note that the MailAddressCollection.Add method is documented to accept comma-separated addresses... so it's possible that that would work in the constructor too.