Last time I have added a post on Sending email using Gmail. Now, I have been working on a project which needs to send emails to the different user's using Godaddy's smtp service. But problem was that I can not send emails to the godaddy server by using its default smtp address as smtpout.secureserver.net at port 80 from the webhosting server. After googling I found out that this smtp is an outside smtp service of godaddy which is used to send emails coming from outside of its server environment. But it will not work when the application is hosted inside of the server environment, which was the case I was having. So, I found out that the correct smtp server I should use from inside the webshosting server is relay-hosting.secureserver.net at port 25.
Now, I assumed that my problem has been resolved. But now here comes the actuall problem. Every time I needed to work on the project I had to change the smtp and port number to smtpout.secureserver.net instead of relay-hosting.secureserver.net because it is in inside relaying smtp hosts so it is not accessible from the outside world. And this has become many times a real problem as I usually forgets to update the settings again and upload it back on the server and then email doesn't work. 
Then I came up with a trick to get the smtp and port number automatically by its own and here is how I did it.
I have created a file name Mail.xml. Save this file on your hosting service's Virtual directory which are you using on the internet.
<?xml version="1.0" encoding="utf-8" ?>
<setting host="relay-hosting.secureserver.net" port="25" />
</mail>
And, we will create a second version of this xml file which will be saved in our local develoment PC i.e at the wwwroot/inetpub/project directory.
<?xml version="1.0" encoding="utf-8" ?>
<setting host="smtpout.secureserver.net" port="80" />
</mail>
Then I have created a class file to introduce the functionality of getting the configuration and then send emails.
Imports Microsoft.VisualBasic
Imports System.Net
Imports System.Net.Mail
Imports System.Net.Configuration
Imports System.Xml
Public Class ModuleClass
Public function sendmail(ByVal body As String, ByVal too As String, ByVal subject As String) As String
Try
Dim arr As New ArrayListDim xtr As XmlTextReader = New XmlTextReader(System.Web.HttpContext.Current.Server.MapPath("Mail.xml"))
xtr.WhitespaceHandling = WhitespaceHandling.None
Dim xd As XmlDocument = New XmlDocument
xd.Load(xtr)
Dim xnodRoot As XmlNode = xd.DocumentElement
Dim xnodWorking As XmlNode
If xnodRoot.HasChildNodes Then
xnodWorking = xnodRoot.FirstChild
While Not IsNothing(xnodWorking) arr = ProcessChildren(xnodWorking, 0)
' ProcessChildren is a function that will return an arraylist with first parameter as server name and second as port number.
xnodWorking = xnodWorking.NextSibling
End While
End If
Dim mail As New MailMessage
mail.To.Add(New MailAddress(too))
' I am hardcoding here that email will be sent by xxxx@abc.com , if you want you can add one more parameter to the function.
mail.From = New MailAddress("xxxx@abc.com")mail.Bcc.Add(New MailAddress("yyyy@123.com"))
mail.Body = body
mail.Subject = subject
Dim client As SmtpClient = New SmtpClient(arr(0).ToString(), CInt(arr(1).ToString()))
client.DeliveryMethod = SmtpDeliveryMethod.Network
client.Credentials = New System.Net.NetworkCredential(mail.From.Address.ToString(), "^&%&^%&^%")
' this is the password of the account used in the from field of the email. Remember this must be a valid account and it should have the rights to send emails.
client.Send(mail)
Return "Success"
Catch ex As Exception
Return "Failed"
End Try
End Function
Private Function ProcessChildren(ByVal xnod As XmlNode, ByVal Depth As Integer) As ArrayList Dim arr As New ArrayList
Dim strNode As String
Dim intI, intJ As Integer
Dim atts As XmlAttributeCollection
'we're only going to process Text and Element nodes
If (xnod.NodeType = XmlNodeType.Element) Or (xnod.NodeType = XmlNodeType.Text) Then
strNode = ""
'attributes too
atts = xnod.Attributes
If Not atts Is Nothing Then
For intJ = 0 To atts.Count - 1
strNode = ""
For intI = 1 To Depth + 1
strNode &= "-"
Next
arr.Add(atts("host").Value.ToString()) arr.Add(atts("port").Value.ToString())
Next
End If
End If
Return arr
End Function
End Class
What happens now is that whenever you tries to send the email, it will load the configuration from the xml file in both the PC and server environment and then you can send email from both of them.
[Note: I have googled this code but I am not aware of the person who wrote this code of getting things from XML file. So I am hereby giving credit to that person who created this function. Thanks Man.]