views:

305

answers:

5

Hey,

I've got a windows service that sends mails to users. The code that is used, is the basic way for sending mail.

SmtpClient client = new SmtpClient("servername");
MailMessage msg = new Mailmsg(); ...// configuration
client.send(msg);

If I set the credentials from client , i can send a mail through the Exchange server (2007). The exchange server is configured to allow to send emails without authentication(anonymously).

So if I dont provide credentials with usedefaultcredentials on false, it works in my console application, but not in my windows service.

I get the message "the smtp server requires a secure connection or the client was not authenticated". Anyone an idea why I get this when I use a windows service , instead of a console app (identical code)

A: 

Your windows account used to run your service must have not enough privileges. Please check these.

Vinzz
A: 

May be the user under which the service is running may not have permission to send email. Right click the service and under 'Log on" tab you will see under which user the service is running. You can set the user there.

Shoban
A: 

You need to run the service under an account that has permissions to access the exchange server. In the properties of the service set the account:

alt text

Darin Dimitrov
Thanks for the swift response.The server where the windows service is on, has permission to send emails in exchange with anonymous users checked. What rights would the account need on the exchange then if his credentials are not used with the send?
Rogue101
A: 

Hi,

I would create a log file of the SMTP sessions, just to verif you are actually connecting to the server you think you are. You can then also visually inspect the log file, and see the actual responses coming back from the SMTP server.

To create a log file of the SMTP session you will need to enable logging switches in your .config file.

The following example of trace switches will cause the SMTP session to be logged to a file named "System.Net.trace.log".

<configuration>
  <system.diagnostics>
    <trace autoflush="true" />

    <sources>

      <source name="System.Net" >
        <listeners>
          <add name="MyTraceFile"/>
        </listeners>
      </source>

      <source name="System.Net.Sockets">
        <listeners>
          <add name="MyTraceFile"/>
        </listeners>
      </source>

    </sources>


    <sharedListeners>
      <add
        name="MyTraceFile"
        type="System.Diagnostics.TextWriterTraceListener"
        initializeData="System.Net.trace.log"                />
    </sharedListeners>

    <switches>
      <add name="System.Net" value="Verbose" />
      <add name="System.Net.Sockets" value="Verbose" />
    </switches>
 </configuration>

I've got more infomrmation here: http://systemnetmail.com/faq/4.10.aspx

Cheers!

Dave

dave wanta
A: 

Just a thought but; does your exchange service rely on secure communications using certificates? If so, you might want to double check your service has access to a Certificate store that actually holds the right certificate.(based on user credentials of the service)

Marvin Smit