如何在C#中发送电子邮件,即使收件人之一,是无效的?收件人、发送电子邮件、如何在

2023-09-03 06:14:26 作者:自私又冷血-

我试图将电子邮件发送到多个接收者,它工作正常时,所有的收件人有有效的电子邮件地址。

I am trying to send email to multiple recipients and it works fine when all the recipients have valid email address.

但是,当收件人之一有无效的电子邮件地址,电子邮件甚至不给其他收件人的电子邮件地址是有效的,我得到一个异常发送:

But when one of the recipients have invalid email address, email is not sent even to other recipients whose email address is valid and I am getting an exception:

服务器拒绝一个或多个收件人地址。服务器   回答是:550#5.1.0地址被拒绝

The server rejected one or more recipient addresses. The server response was: 550 #5.1.0 Address rejected.

有没有什么办法可以发送邮件给其他有效的接收者即使电子邮件地址之一是无效的?

Is there any way I can send the email to other valid recipients even if one of the email address is invalid?

public static void sendMails(string ptxtSubject, string ptxtBody)
{
    string txtTo = "valid1@aaa.com,valid2@aaa.com,invalid1@aaa.com";
    string txtFrom = "valid@aaa.com";
    string txtSubject = ptxtSubject;
    string txtBody = ptxtBody;

    MailMessage mail = new MailMessage();
    mail.To = txtTo;
    mail.From = txtFrom;
    mail.Subject = txtSubject;
    mail.Body = txtBody;

    try
    {
        SmtpMail.SmtpServer ="smtp.aaa.com";
        SmtpMail.Send(mail);
    }
    catch (Exception ex)
    {
       //log the exception
       throw;  
    }
}

我可以给单独的邮件给他们每个人,但用户(收件人)的不知道还有谁在电子邮件通讯组列表。我的要求是每个人都应该能够知道还有谁在接收电子邮件。

I can send separate mail to each of them but users(recipients) will not know who else is in the email distribution list. My requirement is everyone should be able to know who else is receiving the email.

Outlook发送电子邮件给所有的合法用户,并通知我们回到无效用户。有反正,我们可以做同样的使用C#?

Outlook sends the email to all the valid users and notifies us back of invalid users. Is there anyway we can do the same using C#?

推荐答案

除非所有收件人肯定知道对方(他们不介意别人知道他们正在接受来自您的电子邮件),你应该反正单独发送电子邮件

Unless all the recipients definitely know each other (and they don't mind other people knowing they are receiving email from you), you should be sending separate emails anyway.

这也将照顾您的问题,也就是说,如果一个发送操作失败,也不会破坏别人。请注意,在你的情况,似乎最初的中继失败,因为这些地址都来自同一个主机作为 SMTP 服务器。

This would also take care of your problem, i.e. if one send operation fails, it won't disrupt the others. Note that in your case, it appears that the initial relay is failing because the addresses are from the same host as the SMTP server.

在一个电子邮件路由到多个主机,成功/失败是不再相互依存。例如,一个gmail.com服务器可能不知道/关心一个yahoo.com服务器拒绝收件人。

Once an email is routed to multiple hosts, the success/failure is no longer interdependent. For example, a gmail.com server probably doesn't know/care that a yahoo.com server rejected a recipient.

如果性能是一个问题,您可以发送消息异步的得到实现更好的吞吐量。需要注意的是异步发送时,你仍然可以处理异常。

If performance is a concern, you can send the messages asynchronously to get achieve better throughput. Note that you can still handle exceptions when sending asynchronously.

与往常一样,如果您要发送电子邮件的任何数量的,它可能是最好使用第三​​方的服务。

As always, if you are sending any quantity of email, it's probably advisable to use a 3rd party service.