troubleshooting Question

Problem sending mail from Visual Studio 2008 with VB.NET

Avatar of balwynhigh
balwynhighFlag for Australia asked on
Email ServersMicrosoft DevelopmentEmail Protocols
9 Comments1 Solution942 ViewsLast Modified:
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
ASKER CERTIFIED SOLUTION
Dirk Haest
Project manager

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 9 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros