如何使用 log4net 从 Windows 窗体应用程序发送电子邮件

发布于 2024-08-07 10:06:22 字数 276 浏览 4 评论 0原文

我已经在我的 C# 3.5 windows 窗体应用程序中设置了 log4net。我正在寻找如何使用 log4net 从客户端电脑发送电子邮件。 SMTPAppender 需要 SMTPHost 的知识,我见过的示例适用于 Web 应用程序。

有没有一种方法可以从可在任何客户端计算机上运行的应用程序发送电子邮件,无论该计算机是否位于域或网络中。我想我还需要能够检查是否有可用的连接。

我一直在寻找答案,但我没有太多使用电子邮件或网络进行编程的经验,不知道要寻找什么。有什么想法可以引导我走向正确的方向吗?

I have set up log4net in my C# 3.5 windows form application. I am looking for how to send email from a client pc with log4net. The SMTPAppender requires knowledge of SMTPHost and the examples I've seen are for web applications.

Is there a way to send email from an application that will work on any client's computer that may or may not be in a domain or network. I guess I would also need to be able to check if there is a connection available.

I've searched for an answer but I don't have much experience programming with email or the web to know what to look for. Any ideas to steer me in the right direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盛夏尉蓝 2024-08-14 10:06:22

c# SmtpClient 非常适合您的需求。这是一些示例代码(主机是IP地址或主机名,端口通常是25,但不一定):

public static void SendMail(
                                    string host,
                                    int port,
                                    SmtpAuthentication authentication,
                                    string userName,
                                    string password,
                                    string from,
                                    string to,
                                    string cc,
                                    string subject,
                                    string body,
                                    string[] attachments)
        {
            // Create and configure the smtp client
            SmtpClient smtpClient = new SmtpClient();

            if (host != null && host.Length > 0)
            {
                smtpClient.Host = host;
            }

            smtpClient.Port = port;
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;

            if (authentication == SmtpAuthentication.Basic)
            {
                // Perform basic authentication
                smtpClient.Credentials = new System.Net.NetworkCredential(userName, password);
            }
            else if (authentication == SmtpAuthentication.Ntlm)
            {
                // Perform integrated authentication (NTLM)
                smtpClient.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
            }

            MailMessage mailMessage = new MailMessage();

            mailMessage.Body = body;
            mailMessage.From = new MailAddress(from);
            mailMessage.To.Add(to);
            mailMessage.CC.Add(cc);

            foreach (string attachement in attachments)
            {
                mailMessage.Attachments.Add(new Attachment(attachement));
            }

            mailMessage.Subject = subject;
            mailMessage.Priority = MailPriority.Normal;

            smtpClient.Send(mailMessage);
        }
    }

c# SmtpClient is quite right for your needs. here's some sample code (host is an ip address or host name, and the port is usually 25, but not necessarily) :

public static void SendMail(
                                    string host,
                                    int port,
                                    SmtpAuthentication authentication,
                                    string userName,
                                    string password,
                                    string from,
                                    string to,
                                    string cc,
                                    string subject,
                                    string body,
                                    string[] attachments)
        {
            // Create and configure the smtp client
            SmtpClient smtpClient = new SmtpClient();

            if (host != null && host.Length > 0)
            {
                smtpClient.Host = host;
            }

            smtpClient.Port = port;
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;

            if (authentication == SmtpAuthentication.Basic)
            {
                // Perform basic authentication
                smtpClient.Credentials = new System.Net.NetworkCredential(userName, password);
            }
            else if (authentication == SmtpAuthentication.Ntlm)
            {
                // Perform integrated authentication (NTLM)
                smtpClient.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
            }

            MailMessage mailMessage = new MailMessage();

            mailMessage.Body = body;
            mailMessage.From = new MailAddress(from);
            mailMessage.To.Add(to);
            mailMessage.CC.Add(cc);

            foreach (string attachement in attachments)
            {
                mailMessage.Attachments.Add(new Attachment(attachement));
            }

            mailMessage.Subject = subject;
            mailMessage.Priority = MailPriority.Normal;

            smtpClient.Send(mailMessage);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文