简体   繁体   English

C#:将电子邮件发送到个人邮件和组邮件(不适用于组邮件)

[英]C#: Sending emails to personal mails and group mails (not working for group mails)

In an ASP.NET/C# application (mvc3) I want to be able to send Mail. 在ASP.NET/C#应用程序(mvc3)中,我希望能够发送邮件。

This is the function I am using for that: 这是我为此使用的功能:

    public static void SendEmail(string fromEmail, string fromName, string toEmail, string toName, string subject, string emailBody, bool isBodyHtml, string[] attachments, string emailServer, int portNumber, string loginName, string loginPassword)
    {
        // setup email header
        System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();

        // Set the message sender
        // sets the from address for this e-mail message.
        mailMessage.From = new System.Net.Mail.MailAddress(fromEmail, fromName);
        // Sets the address collection that contains the recipients of this e-mail message.
        string[] toEmailList = toEmail.Split(',');
        string[] toNameList = toName.Split(',');
        for (int i = 0;i<toEmailList.Length;++i) 
        {
            mailMessage.To.Add(new System.Net.Mail.MailAddress(toEmailList[i],toNameList[i]));
        }
      //  mailMessage.To.(new System.Net.Mail.MailAddress(toEmail, toName));

        // sets the message subject.
        mailMessage.Subject = subject;
        // sets the message body.
        mailMessage.Body = emailBody;
        // sets a value indicating whether the mail message body is in Html.
        // if this is false then ContentType of the Body content is "text/plain".
        mailMessage.IsBodyHtml = isBodyHtml;

        // add all the file attachments if we have any
        if (attachments != null && attachments.Length > 0)
            foreach (string _Attachment in attachments)
                mailMessage.Attachments.Add(new System.Net.Mail.Attachment(_Attachment));

        // SmtpClient Class Allows applications to send e-mail by using the Simple Mail Transfer Protocol (SMTP).
        System.Net.Mail.SmtpClient _SmtpClient = new System.Net.Mail.SmtpClient(emailServer);

        //Specifies how email messages are delivered. Here Email is sent through the network to an SMTP server.
        _SmtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

        // Some SMTP server will require that you first authenticate against the server.
        // Provides credentials for password-based authentication schemes such as basic, digest, NTLM, and Kerberos authentication.
        System.Net.NetworkCredential _NetworkCredential = new System.Net.NetworkCredential(loginName, loginPassword);
        _SmtpClient.UseDefaultCredentials = false;

        _SmtpClient.Credentials = _NetworkCredential;
        _SmtpClient.Port = portNumber;
        //Let's send it
        try
        {
            _SmtpClient.Send(mailMessage);
            mailMessage.Dispose();
            _SmtpClient = null;
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }

Everything works fine for personal Emails. 对于个人电子邮件,一切正常。

The Problem is this: 问题是这样的:

If one of the TO Emails (the emails I am sending this to) is a group Email, it does not send to it. 如果“ TO电子邮件”(我发送给它的电子邮件)之一是组电子邮件,则不会发送给它。

By Group Email, I mean an Email that have multiple contacts attached to it, and they all receive the message when someone send it to that email. 群组电子邮件是指具有多个联系人的电子邮件,当有人将其发送到该电子邮件时,他们都会收到该邮件。

Example of Group Email 组电子邮件示例

If someone send an email to MyFamily@somedomain.com all my family receives it. 如果有人将电子邮件发送到MyFamily@somedomain.com,我的全家人都会收到它。

How can I make it work for all kind of email not just personal emails. 如何使它适用于所有类型的电子邮件,而不仅仅是个人电子邮件。

Thanks a lot for any help 非常感谢您的帮助

I had the same problem with my personal Google Apps Domain. 我的个人Google Apps域存在相同的问题。 But the problem was in the configuration of the Group Email and not in the c# code. 但是问题出在组电子邮件的配置中,而不是c#代码中。

Did you check your permissions on the Group Email? 您是否检查了组电子邮件的权限? Often you can define, who can send Emails to this Group Email. 通常,您可以定义谁可以将电子邮件发送到该组电子邮件。 So you have to use the right email address or to reconfigure the permissions. 因此,您必须使用正确的电子邮件地址或重新配置权限。 Normally all members of the group can send emails to the group. 通常,该论坛的所有成员都可以向该论坛发送电子邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM