Sending email in VB .Net via Exchange Server 2007

G

Guest

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.
 
M

MasterGaurav \(www.edujini-labs.com\)

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
-----------------------------------------
 
G

Guest

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
 
A

Alois_Mair

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
 
M

MasterGaurav \(www.edujini-labs.com\)

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
-----------------------------------------
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top