Link to home
Start Free TrialLog in
Avatar of balwynhigh
balwynhighFlag for Australia

asked on

Problem sending mail from Visual Studio 2008 with VB.NET

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
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

If you break your code in file B, can you check the values of mail.from and mail.replyTo.
What is the right value of fullName & " <" & field(1) & ">"

Probably it's holding a value of a mailbox that doesn't exist !
Avatar of balwynhigh

ASKER

You're right, field(1) probably isn't holding a value that exists locally. field(1) as you may have guessed is obtained from a form, so the value is dynamic. In the past this has not been a problem.

One application that utilises the Webmail class is a contact form. In this case, the user supplies their name and email address which is cleaned and populated into 'fullName' and  'field(1)'. field(1) could be any email address. The contact form then sends an email to an internal user so when they hit reply does not reply to noreply@abc.net, rather replies to the 'field(1)' address. If we were to set field(1) to an email that existed on the local mail server (ie. noreply@abc.net), hitting reply would reply to an unattended mailbox.
Glad to see that your problem is solved by looking at the field(1) :)
Well, I'm not sure if it's a solution because how would the contact form where field(1) is dynamic be implemented?
I can't follow completely. You have a contactform, and when the user fills in all the items (email, name, ...), what exactly do you want to do than ?
File B is an extract from a contact form. The contact form contains a list of contact names, but no email addresses are listed. An external user selects a contact name from the list, enters mandatory fields (email, name, message) then hits send.

Let's assume an external visitor with the following details:
Name: Bill Blog
Email: b.blog@bb.com
Message: Hi

Let's suppose Bill selects a user "Andrew Smith" which has the email address "a.smith@abc.net"; enters his name, email and message and submits the form.

The contact form then reaches the following lines of code:

Dim internalUser = someProcedureThatGetsAnInternalEmail("Andrew Smith") ' internalUser now contains string "a.smith@abc.net"

Dim emails() As String = { internalUser }
                           
mail.from = fullName & " <" & field(1) & ">"       ' fullName contains "Bill Blog", field(1) contains "b.blog@bb.com"

mail.replyTo = fullName & " <" & field(1) & ">"   ' same deal as above

mail.sendTo = emails     ' Assigns the email array (in this example only a.smith@abc.net; some times the class will send to multiple recipients)


This is so when an internal user a.smith@abc.net receives the email, can hit reply directly in the mail client and it will reply to b.blog@bb.com rather then noreply@abc.net (account "mailbox" which is what the server is authenticating as against the mail server; ie. user account: domain\mailbox).

I hope this is clear; please let me know if further clarification is required. Thank you for your prompt responses so far.
ASKER CERTIFIED SOLUTION
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So basically what you are implying is that mail.from must be an address that exists on the mail server? If that's the case, on Monday morning when I have access to the dev box, will try setting mail.from to "Mailbox <noreply@abc.net>" while keeping the mail.replyTo the same. I will post back my findings.

In the meantime, if anyone can shed some light on this bizarre problem it would be greatly appreciated.
I have tested and everything works fine. So it seems correct that you must specify a valid from address that is local to the mail server. Any other address will not work regardless of the authenticating account (ie. domain\mailbox). Thank you for your help Dhaest.