Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
2.22/5 (2 votes)
I am trying to send an email using vb.net 2008 via our corporate exchange server and I am receiving the following message. Mailbox unavailable. The server response was: 5.7.1 Unable to relay for MyEmailAdrress@MyDomain.com. The email addresses are mine for the corporate domain, and the login credentials are mine for the domain. I have not tried to send email outside of the domain thus i don't understand the RELAY part.

Thanks.

Ed

Try
       ' Create a new MailMessage object and specify the "From" and "To" addresses
       Dim message As New MailMessage("MyEmailAdrress@MyDomain.com", "MyEmailAdrress@MyDomain.com", "Subject HERE", "Body message HERE")
       Dim emailClient As New SmtpClient("MySMTPServer.MyDomain.local")
       ' Authentication
       Dim basicAuthenticationInfo As New System.Net.NetworkCredential("MyNetworkUSERID", "MyNetworkUSERPASSWORD", "MyDomain")
       emailClient.UseDefaultCredentials = False
       emailClient.Credentials = basicAuthenticationInfo
       message.IsBodyHtml = True
       emailClient.Send(message)
   Catch ex As Exception
       Debug.Print(ex.Message)
   End Try

Below is the OUTPUT
VB
Mailbox unavailable. The server response was: 5.7.1 Unable to relay for MyEmailAdrress@MyDomain.com
Posted
Comments
Richard C Bishop 3-Feb-14 15:49pm    
You have to use actual data to send an email. Real SMTP client and what not.
TheITManager 3-Feb-14 15:58pm    
I am using REAL smtp information. The above code is just an example. The actual code I am using for my corporate application contains the name of my corporate smtp server, my actual email address, username, passowrd etc...
Richard C Bishop 3-Feb-14 16:00pm    
lol, well we can rule that issue out then.
mgoad99 3-Feb-14 18:26pm    
I have a few applications that have used SMTP to send an email to a local SMTP server inside my domain. I also send email within the domain. For me, I had to go onto the box that was setup as the SMTP server and add the IP address of my PC (where i was testing) and any other PC or server that is sending the mail. In the properties for the SMTP server there are relay restrictions. I added the ip addresses i need there.

If that is the problem, here is an article that may help
http://technet.microsoft.com/en-us/library/aa996446(v=exchg.65).aspx

I hope that helps or at least points you in the right direction. Good luck!
TheITManager 4-Feb-14 14:19pm    
ANSWER!! Had my development laptop's IP address added to the ALLOW RELAY list on the mail server. -- This resolved the problem, and as it turned out every block of EMAIL code that I had tested and failed before all now work, atleast from my PC. I am just curious as to how to expand upon this. What i was not understanding was that I was using a valid network user id and password for the domain, and it too was failing. Thanks for the feedback! Ed

Try this code making necessary changes.


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      Try
          Dim Smtp_Server As New SmtpClient
          Dim e_mail As New MailMessage()
          Smtp_Server.UseDefaultCredentials = False
          Smtp_Server.Credentials = New Net.NetworkCredential("username@gmail.com", "password")
          Smtp_Server.Port = 587
          Smtp_Server.EnableSsl = True
          Smtp_Server.Host = "smtp.gmail.com" //Replace with your domain host name.

          e_mail = New MailMessage()
          e_mail.From = New MailAddress(txtFrom.Text)
          e_mail.To.Add(txtTo.Text)
          e_mail.Subject = "Email Sending"
          e_mail.IsBodyHtml = False
          e_mail.Body = txtMessage.Text
          Smtp_Server.Send(e_mail)
          MsgBox("Mail Sent")

      Catch error_t As Exception
          MsgBox(error_t.ToString)
      End Try

   End Sub
 
Share this answer
 
Comments
TheITManager 4-Feb-14 10:20am    
I am trying to use Exchange server smtp..
This returns error:

System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 11.70.30.80:587
Vamsi Krishnna 4-Feb-14 10:43am    
Check if the ports are active and listening on the target machine(server port-587 in above case),if not configure the firewall on target machine to listen on that port.
Vamsi Krishnna 4-Feb-14 10:48am    
Check this list for different ports on exchange and their functionality.

https://support.prolateral.com/index.php?/Knowledgebase/Article/View/179/0/what-ports-does-ms-exchange-use
ANSWER!!

Had my development laptop's IP address added to the ALLOW RELAY list on the server. -- This resolved the problem. When I deploy this onto a production server I will have to get the servers IP address added also.

Thanks to mgoad99 http://www.codeproject.com/script/Membership/View.aspx?mid=3894223[^]

UPDATED VB CODE NO LONGER REQUIRES USER NAME, PASSWORD or DOMAIN
VB
 Dim MailFrom As System.Net.Mail.MailAddress
 Dim MailTo As System.Net.Mail.MailAddress
 Dim MailSubject As String
 Dim MailBody As String
 Dim Mail As New MailMessage
 Dim MailClient As SmtpClient

 MailBody = "Test Email"
 MailSubject = "Test Subject"
'Here I do not care about the from Email address, it is a Dummy-Email made up
 MailFrom = New MailAddress("DUMMY-Email-Here@SomePlace.com")
 MailTo = New MailAddress("JDoe@SomePlace.com")

 Mail.From = MailFrom
 Mail.To.Add(MailTo)
 Mail.Subject = MailSubject
 Mail.Body = MailBody

'25 is the port of the SMTP host
 MailClient = New SmtpClient("MySMTPServer.someplace.local", 25) 

 Try
     MailClient.Send(Mail)
 Catch ex As Exception
     Debug.Print("Mail Error: " & ex.Message)
 End Try
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900