We help IT Professionals succeed at work.
Get Started

Problem sending mail from Visual Studio 2008 with VB.NET

939 Views
Last Modified: 2013-12-18
I've recently run into a strange problem trying to send email from my vb.net application. I'm using Visual Studio 2008 as my development environment. I'm testing localy. Mail server sits at 172.18.0.21 on port 25.

Mail sending is handled by a Webmail class which imports the following:

Imports Microsoft.VisualBasic
Imports System.Net
Imports System.Net.Mail

----------------------------------------------------------------------------------------------------

Webmail contains a SendMail method that accepts one argument. The argument is a stucture of the following format.

Public Structure mailSettings
        Dim from As String
        Dim replyTo As String
        Dim sendTo() As String
        Dim subject As String
        Dim htmlMail As Boolean
        Dim mailBody As String
End Structure

----------------------------------------------------------------------------------------------------

The SendMail method (in Webmail class):

    Public Function SendMail(ByVal config As mailSettings) As Boolean
        ' Check Required Provided
        If config.from <> "" And config.sendTo.Length > 0 And _
           config.subject <> "" And config.mailBody <> "" Then
            ' Define Mail Message
            Dim mail As New MailMessage

            ' Define Inboxes
            mail.From = New MailAddress(config.from)
            For i As Int32 = 0 To config.sendTo.GetUpperBound(0)
                mail.To.Add(config.sendTo(i))
            Next

            ' Prepare Mail
            mail.Subject = config.subject
            mail.Body = config.mailBody
            If config.replyTo <> String.Empty Then
                mail.ReplyTo = New MailAddress(config.replyTo)
            End If
            If config.htmlMail = True Then
                mail.IsBodyHtml = True
            End If

            ' Perform Send
            ' These are not actually hard coded, I've modified for simplicity
            Dim mailClient As New SmtpClient("172.18.0.21", 25)
            mailClient.Credentials = New NetworkCredential("mailbox", "password", "domain")

            Try
                mailClient.Send(mail)
            Catch
                Return False
            End Try

            Return True
        Else
            Return False
        End If
    End Function

----------------------------------------------------------------------------------------------------

I setup a test file (A) which calls the class and sends mail both internaly and externaly without problems using the webmail class. However, file (B) breaks at mailClient.Send(mail) with the error "Mailbox unavailable. The server response was: 5.7.1 Client does not have permissions to send as this sender".

File A and B snippets below:

----------------------------------------------------------------------------------------------------

File A (WORKS):

                Dim postOffice As New webmail
                Dim mail As webmail.mailSettings
                Dim letter As New webmail.mailTemplate
                Dim emails() As String = {"abc@xyz.com"}
                       
                mail.from = "noreply@abc.net"
                mail.replyTo = "reply@abc.net"
                mail.sendTo = emails
                mail.htmlMail = True
                mail.subject = "Test Mail"
               
                letter.title = "Test"
                letter.autoResponse = True
                letter.spamNotice = True
                letter.body = "Test Body"
                mail.mailBody = postOffice.generateMail(letter)
   
                If Not postOffice.sendMail(mail) Then
                    Response.Write("<div style=""color:#FF0000"">Failed Sending Email.</div>")
                Else
                    Response.Write("<strong>Email Sent to: " & strTo & "</strong>")
                End If


File B:
                ' Send Email
                Dim postOffice As New webmail
                Dim mail As webmail.mailSettings
                Dim letter As New webmail.mailTemplate
                Dim emails() As String = {"abc@xyz.net"}
                           
                mail.from = fullName & " <" & field(1) & ">"
                mail.replyTo = fullName & " <" & field(1) & ">"
                mail.sendTo = emails
                mail.htmlMail = True
                mail.subject = system.web.HttpUtility.HtmlDecode(field(2))
               
                letter.title = field(2) ' "Message Through Website"
                letter.autoResponse = False
                letter.spamNotice = False
               
                mail.mailBody = postOffice.generateMail(letter)
                If postOffice.sendMail(mail) Then
                    promptMsg = "Your message has been sent.<br />" & who & " will get back to you as soon as possible."
                Else
                    promptMsg = "Could not send your message.<br />Please contact us via telephone."
                End If
Comment
Watch Question
Project manager
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 9 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE