473,385 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Sending email in VB .Net via Exchange Server 2007

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("IpAddressOfComputerRun ningExchangeServer", 25)
Dim mailCredentials As New
System.Net.NetworkCredential("MyDomain.com\MyUserN ame",
"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.X5 09Certificate2.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.

Jun 19 '07 #1
5 23821
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
-----------------------------------------
Jun 20 '07 #2
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("IpAddressOfMyExchangeS erver", 25)

mailClient.Credentials = New
NetworkCredential("IpAddressOfMyExchangeServer\Use rName", "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
Jun 20 '07 #3
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
Jun 21 '07 #4
Thanks so much for you insight on this. It was indeed a credentials
issues.
Great!
And also thanks for posting the update! I'll put it on my FAQs page, with
reference to this posting. :-)
--
Happy Hacking,
Gaurav Vaish | www.mastergaurav.com
www.edujini-labs.com
http://eduzine.edujini-labs.com
-----------------------------------------
Jun 24 '07 #5
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
-----------------------------------------

Jun 24 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rob Meade | last post by:
Hi all, I have no knowledge of Exchange server, and have always used CDO to send emails from ASP in the past. My problem now is that the server in question doesn't have SMTP installed alongside...
1
JamieHowarth0
by: JamieHowarth0 | last post by:
Hi folks, I'm running Exchange Server 2007 Beta2 on my W2K3 machine (and before you all say it, yes, I know it's not supported in production environment but it's the only way I've found so far to...
2
by: LVP | last post by:
Hi Everyone, I am looking to get emails from a Public Folder on our internal server using a filter from exchange server 2007 extract attachments tag or set the status on each email so I don't...
2
by: chelluchittibabu | last post by:
hi, now i need some information regarding exchange server 2007. what i want is how to connect exchange server 2007 and how to retrieve data from exchange server 2007. if any one knows pls let...
1
by: chelluchittibabu | last post by:
hi, pls tell me if you know regarding ms exchange server 2007. In my system i am using windows 2003. what i want is 1. how to connect ms exchange server 2007 from .net 2. how to get data...
1
by: chelluchittibabu | last post by:
hi, i am working with exhange server 2007 using cdo api's. pls tell me how to retrieve appointments from exchange server 2007 using cdo in c#. regards, chittibabu 9945299471
1
rcollins
by: rcollins | last post by:
We have new exch 2007 servers. on a few computers, we get a logon screen when we open outlook promts for user name and password but wont connect. Need to know how to fix this. Not finding much on...
0
by: sudhivns | last post by:
Can anybody help me with info on: 1. How to retrieve all the users configured in Exchange server 2. How to retrieve the mails of each user I have used SMAPI to read the mails based on the...
21
by: DanicaDear | last post by:
I've been trying to figure this out for three weeks. I've been sleeping on it. Not getting answers, just losing sleep. ;-) In a table, I have a field COMPANY_EMAIL. In this field I want to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.