Sending Email Through Exchange Server

corteplaneta

Member
Joined
Nov 14, 2008
Messages
10
Programming Experience
3-5
Hello,

I've been attempting to write an application that sends mail through the company exchange server. This exchange server does not have basic authentication enabled, only TLS.

I've quickly borrowed some code from somewhere on the net (sorry, I would cite if I remembered where I got it) to test out connecting to our exchange server & sending mail via SMTP.

Here's the code...
VB.NET:
Imports System.Net.Mail

Namespace TNE.DocGen
    Public Class Mailer
        Public Shared Sub SendEmail(ByVal sender As String, _
          ByVal recipient As String, ByVal subject As String, _
          ByVal body As String, Optional ByVal attachmentString As String = "")

            Dim fromAddress As New MailAddress(sender)
            Dim toAddress As New MailAddress(recipient)
            Dim message As New MailMessage(fromAddress, toAddress)

            Dim mailSender As SmtpClient = New SmtpClient("exchsvr", 25)
            With mailSender
                .Credentials = New Net.NetworkCredential("testuser", "!testuserpw!")
                .UseDefaultCredentials = False
                .EnableSsl = True
            End With

            message.Bcc.Add(fromAddress)
            message.Subject = subject
            message.IsBodyHtml = False
            message.Body = body

            If Not attachmentString = "" Then
                Dim msgAttach As New Attachment(attachmentString)
                message.Attachments.Add(msgAttach)
            End If

            Try
                mailSender.Send(message)
                MsgBox("Message successfully sent.", MsgBoxStyle.Information)
            Catch ex As Exception
                MsgBox(ex.ToString, MsgBoxStyle.Critical)
                Throw New Exception(ex.ToString)
            End Try

        End Sub

    End Class
End Namespace

When attempting to run this code, I'm given an error stating "System.Security.AUthentication.AuthenticationException: The remote certificate is invalid according to the validation procedure at..." blah blah blah.

If I disable SSL for the SMTP server, I receive error "System.Net.Mail.SmtpFailedRecipientException: Mailbox unavailable. The server response was: 5.7.1 Unable to relay...blah blah blah"

Now to go a bit off topic.......

Being I couldn't seem to connect using either method, I decided to attempt to telnet into the exchange server. With just TLS enabled, I run the "AUTH LOGIN" command and simply receive and error stating that this is an unrecognized authentication type.

Reading a little more into this, I enabled "Basic Authentication" for the receive connector, and this time actually got the "AUTH LOGIN" command to execute--but this time, using both my username & username@domain.com were rejected and the message "Authentication unsuccessful" was returned.

If anyone has any knowledge in exchange 2007 or any ideas of what to do from here, they would be GREATLY appreciated.

Thanks,

corteplaneta.
 
Dan's WebDAV 101 : System.Net.Mail with SSL to authenticate against port 465

Looks like your out of luck sending this way.

Any chance you can send it through the network and have the server pick it up?

VB.NET:
Dim smtp As New SmtpClient("server.domain.net", 25)
smtp.Credentials = New Net.NetworkCredential("user@domain.com", "password", "domain.com")
smtp.DeliveryMethod = SmtpDelivertyMethod.Network (or SmtpDeliveryMethod.SpecifiedPickupDirectory)

Try
    smtp.Send(mm)
    ...
 
Thanks for the reponse MattP.

I've attempted using SmtpDeliveryMethod.Network to no avail..how exactly would I go about using SmtpDeliveryMethod.SpecifiedPickupDirectory? I've Google'd the subject but all examples I'm seeing are quite unclear.
 
Haven't used this method using the .NET framework but I have a .bat file that uses the same principles to send email.

I would assume it's something like:

VB.NET:
smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory
smtp.PickupDirectoryLocation = "\\server\c$\Inetpub\mailroot\pickup"

Odds are you aren't going to have the admin share available to you so you'll need to contact IT to get access to the directory.
 
Matt, I owe you a beer!!

Your assumption proved to be absolutely correct--the following code performed the task much more efficiently than connecting & authenticating with the domain's exchange server..

VB.NET:
            Dim mailSender As SmtpClient = New SmtpClient("exchsvr", 25)
            With mailSender
                .DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory
                .PickupDirectoryLocation = "\\exchsvr\Pickup"
            End With

Thanks!
 
Back
Top