Sending email in VB .Net via Exchange Server 2007

Discussion in 'ASP .Net' started by =?Utf-8?B?RG9taW5v?=, Jun 19, 2007.

  1. Greetings all,

    I'm trying to send an e-mail from a Windows application thru MS Exchange
    Server 2007 that is in my LAN. The setup for Exchange server is very basic,
    it's running on Server 2003 R2 x64 machine. Here is the code I'm trying to
    use to send the email:

    Public Sub SendMail()
    Dim strTo As String = frmMain.txtTo.Text
    Dim strFrom As String = frmMain.txtFrom.Text
    Dim strSubject As String = frmMain.txtSubject.Text
    Dim strBody As String = frmMain.txtBody.Text
    Dim mailMessage As New System.Net.Mail.MailMessage(strFrom, strTo,
    strSubject, strBody)
    Dim mailClient As New
    System.Net.Mail.SmtpClient("IpAddressOfComputerRunningExchangeServer", 25)
    Dim mailCredentials As New
    System.Net.NetworkCredential("MyDomain.com\MyUserName",
    "ExchangeUserPassword", "MyDomain.com")

    mailClient.UseDefaultCredentials = False
    mailClient.Credentials = mailCredentials
    mailClient.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.Network
    'mailClient.EnableSsl = True

    Try
    'mailClient.ClientCertificates.Add
    ((System.Security.Cryptography.X509Certificates.X509Certificate2.CreateFromSignedFile("C:\My Certificate in DER format.cer")))
    mailClient.Send(mailMessage)
    Catch ex As Exception
    frmMain.txtInfo.Text = ex.ToString
    Exit Sub
    End Try

    frmMain.txtInfo.Text = "Your message was sucessfully sent."
    End Sub

    Notice I have two lines of code commented out. If I run the the app and try
    to send the email with these lines commented out I get the following
    exception "The server response was 5.7.1 Unable to relay."

    If I run the app with those lines not commented out, I receive the following
    exception "The remote certificate is invalid according to the validation
    procedure."

    I need help debugging this application. I can usually get by with just the
    exception but I am at my wits end here. I've read some threads about
    enabling tracing thru the use of an app.config file in my project, but I'm
    struggling with that as well. Any help you could provide would be much
    appreciated. Please let me know if you need any additional details. Thank
    you.
     
    =?Utf-8?B?RG9taW5v?=, Jun 19, 2007
    #1
    1. Advertising

  2. > Notice I have two lines of code commented out. If I run the the app and
    > try
    > to send the email with these lines commented out I get the following
    > exception "The server response was 5.7.1 Unable to relay."


    Are you able to send the mail using the specified credentials from Outlook /
    Express / Website?
    The issue is that the user is not authorized to send mails, hence "Unable to
    relay".

    --
    Happy Hacking,
    Gaurav Vaish | www.mastergaurav.com
    www.edujini-labs.com
    http://eduzine.edujini-labs.com
    -----------------------------------------
     
    MasterGaurav \(www.edujini-labs.com\), Jun 20, 2007
    #2
    1. Advertising

  3. Gaurav,

    Thanks so much for you insight on this. It was indeed a credentials issues.
    Two things...

    1. In exchange server, I had to go to Server Configuration-->Hub
    Transport-->Rerceive Connectors. Right-click on Defaut Mail and select
    properties. On the Authentication tab, check off Transport Layer Security
    and Exchange Users. On the Permissions Groups tab, select only Anonymous
    Users and Exchange Users.

    2. I also had to tweak my code. Here it is:

    Public Sub SendMail()
    Dim strTo As String = frmMain.txtTo.Text
    Dim strFrom As String = frmMain.txtFrom.Text
    Dim strSubject As String = frmMain.txtSubject.Text
    Dim strBody As String = frmMain.txtBody.Text
    Dim mailMessage As New System.Net.Mail.MailMessage(strFrom, strTo,
    strSubject, strBody)
    Dim mailClient As New
    System.Net.Mail.SmtpClient("IpAddressOfMyExchangeServer", 25)

    mailClient.Credentials = New
    NetworkCredential("IpAddressOfMyExchangeServer\UserName", "Password")
    mailClient.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.Network

    Try
    mailClient.Send(mailMessage)
    Catch ex As Exception
    frmMain.txtInfo.Text = ex.ToString
    Exit Sub
    End Try

    frmMain.txtInfo.Text = "Your message was sucessfully sent."
    End Sub

    Works like a charm now!! Thanks for you help.

    Cheers,
    Domino Effect
     
    =?Utf-8?B?RG9taW5v?=, Jun 20, 2007
    #3
  4. =?Utf-8?B?RG9taW5v?=

    Alois_Mair Guest

    hi Guarav,
    how can i send a email through a smtp with .net, wich is hosted by an
    inetprovider? i would be very glad for some code samples
    kind regards
     
    Alois_Mair, Jun 21, 2007
    #4
  5. MasterGaurav \(www.edujini-labs.com\), Jun 24, 2007
    #5
  6. > hi Guarav,

    It's Gaurav and not Guarav... ;-)

    > how can i send a email through a smtp with .net, wich is hosted by an
    > inetprovider? i would be very glad for some code samples
    > kind regards



    Look into System.Net.SmtpClient class.

    SmtpClient client = new SmtpClient("smtp.server-servername.com", 25);
    MailMessage message = new MailMessage(....);
    client.Send(message);

    HTH.


    --
    Happy Hacking,
    Gaurav Vaish | www.mastergaurav.com
    www.edujini-labs.com
    http://eduzine.edujini-labs.com
    -----------------------------------------
     
    MasterGaurav \(www.edujini-labs.com\), Jun 24, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Henry
    Replies:
    0
    Views:
    492
    Brian Henry
    Oct 3, 2003
  2. RedEye
    Replies:
    2
    Views:
    3,305
    RedEye
    Nov 18, 2003
  3. Jim in Arizona
    Replies:
    8
    Views:
    5,003
    Jim in Arizona
    Jan 24, 2006
  4. Gilbert Cheung

    Sending Email via Exchange Server from ASP.NET

    Gilbert Cheung, Mar 23, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    17,177
    Christopher Reed
    Mar 25, 2006
  5. Brent Burkart

    asp.net sending email using exchange server

    Brent Burkart, Aug 18, 2003, in forum: ASP General
    Replies:
    1
    Views:
    169
    Aaron Bertrand - MVP
    Aug 18, 2003
Loading...

Share This Page