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
What is the right value of fullName & " <" & field(1) & ">"
Probably it's holding a value of a mailbox that doesn't exist !