Click here to Skip to main content
15,893,663 members

Response to: Sending mail via Exchange Server

Revision 4
Hi, try to use System.Net.Mail.SmtpClient. I used the following code
This code works even in .NET 2.0 with domain credentials. Some parameters has been taken from configuration file.
C#
string mail_server = Properties.Settings.Default.MailServer;
int mail_port = Properties.Settings.Default.MailPort;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(mail_server, mail_port);
    string smtp_login = "my_login";
    string smtp_pwd = "my_pwd";
    smtp.Credentials = new System.Net.NetworkCredential(smtp_login, smtp_pwd);
    //Creating a mail message
    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
    message.From = new System.Net.Mail.MailAddress("my_login@my_domain.my");
    int i = 0;
    // Getting recipient list from configuration file
    if( Properties.Settings.Default.Recipients.Count > 0 ){
        foreach( string mail_addr in Properties.Settings.Default.Recipients ){
            if (mail_addr.Length > 0) message.To.Add(new System.Net.Mail.MailAddress(mail_addr));
        }
        message.Subject = "My message";
        message.Priority = System.Net.Mail.MailPriority.Normal;
        message.Body = "This is a mail body message! :)";

        // Adding attachments
        string[] attachments = new string[2]{"my_attachment1", "my_attachment2"};
        foreach (string attachment in attachments){
            System.Net.Mail.Attachment att = new System.Net.Mail.Attachment(attachment);
            message.Attachments.Add(att);
        }
        smtp.Send(message);
    }else{
        // No recepients error
        //...
    }

I hope this code will be helpful.
Posted 14-Nov-12 0:59am by skydger.