开发者

authenticate to Exchange 2007

I've got a simple mail utility that is supposed to send an email through an Exchange 2007 server (which is installed on Windows Server 2008 R2 64bit) and it won't work, giving the following error message at the commandline: "Mailbox unavailable. The server response was: 5.7.1 Unable to relay". I've been told I need to authenticate to the server but evidently I'm not doing it correctly. Any suggestions? My code is below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Mail;

namespace SendMail
{
    class Program
    {
        static void Main(string[] args)
        {
            SmtpClient smtpClient = new SmtpClient("x.x.x.x", 25);
            NetworkCredential basicCredential = new NetworkCredential("username", "password", "domain");
            MailMessage message = new MailMessage();
            MailAddress fromAddress = new MailAddress("user@domain.com");
            smtpClient.Host = "x.x.x.x";

            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials = basicCredential;
            message.From = fromAddress;
            message.Subject = "test message";
            message.Body = "test message";
            message.To.Add("user@domain.com");            
            try
            {
                smtpClient.Send(message);
                Console.WriteLine("Message sent successfully");
            }
            catch (Exception ex)
            {    
                //Error, could not send th开发者_开发问答e message
                Console.WriteLine(ex.Message);
            }
        }
    }
}


I tested and your code is working fine. Please check that the account you using is properly configured in Exchange, or try using another account.


The System.Net.Mail.SmtpClient will not easily authenticate with Exchange unless you make a connector to allow open relay on the Exchange server. You could limit it to only the ip of the sending server. However for sending mail through Exchange you will find the following EWS dll to be the better general solution. It's just as straightforward as your code is now but works better for Exchange.

You can try Microsoft Exchange Web Services using the following dll.

https://www.microsoft.com/en-us/download/details.aspx?id=42951

The object is pretty straight forward and comparable to using System.Net.Mail.

Here is an example I modified from the MSDN example.

    using Microsoft.Exchange.WebServices.Data;


        protected void EmailEws()
        {
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);

            service.Credentials = new WebCredentials("user", "password", "domain(local)");

            service.TraceEnabled = true;
            service.TraceFlags = TraceFlags.All;
// EWS service url.
            service.Url = new Uri("https://example.com/EWS/Exchange.asmx");

            EmailMessage email = new EmailMessage(service);

            email.ToRecipients.Add("a@b.com");

            email.Subject = "HelloWorld";          
            email.Body = new MessageBody("<p>This is the first email I've sent by using the EWS Managed API!</p>");
            // set Body before BodyType or an error is raised!                                                        
            email.Body.BodyType = BodyType.HTML;

            email.SendAndSaveCopy();
        }

The EWS url can be a bit confusing. It also depends if you have access to the Exchange server. Here is some help.

https://msdn.microsoft.com/en-us/library/office/dn509511(v=exchg.150).aspx

Helpful if you don't have access to the Exchange server. http://nuanceimaging.custhelp.com/app/answers/detail/a_id/13098/~/determining-the-exchange-web-services-(ews)-url-for-the-sharescan-exchange

Change last part to Exchange.asmx, not Services.wsdl

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜