How to send email from windows form app with log4net

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?

13.10.2009 17:30:29
1 ОТВЕТ
РЕШЕНИЕ

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);
        }
    }
1
13.10.2009 17:49:22
Thanks, I'm not sure of what to use for the host string. Is it the IP address of the PC? So to work on every machine I should use CredentialCache.DefaultNetworkCredentials for authentication?
Maggie 13.10.2009 20:22:38
try asking your sysadmin for the host name. as for credentials, default network credentials should work on all machines. you should only supply your mailbox username and password.
geva30 14.10.2009 01:31:39