sending email in vb.net in HTML formatted page - html

I made email form in vb.net it works but i need to send a mail using html format which i made that also works but when i receive mail the value written in text box is not coming in mail. what is problem in this?
Imports System
Imports System.Net.Mail
Imports System.Net
Imports System.IO
Imports System.Configuration
Partial Class Form
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
End Sub
Protected Sub submit_button_Click(sender As Object, e As EventArgs) Handles submit_button.Click
SendHTMLMail()
End Sub
' Method Which is used to Get HTML File and replace HTML File values with dynamic values and send mail
Public Sub SendHTMLMail()
Dim reader As New StreamReader(Server.MapPath("html.html"))
Dim readFile As String = reader.ReadToEnd()
Dim myString As String = ""
myString = readFile
myString = myString.Replace("$$Company_Name$$", "Company_Name")
myString = myString.Replace("$$Contact_Person_Name$$", "Txt_ContactPerson.Text")
myString = myString.Replace("$$Contact_Person_Mobile_No$$", "Txt_Contact_No.Text")
myString = myString.Replace("$$Telephone_No$$", "Txt_TelNo.Text")
myString = myString.Replace("$$Email_Id$$", "Txt_Email.Text")
myString = myString.Replace("$$Registered_Address$$", "Txt_Address.Text")
myString = myString.Replace("$$Year_Of_Commencement_Of_Business$$", "Txt_CommenceYear.Text")
myString = myString.Replace("$$Income_TAX_PAN_No$$", "Txt_IncomeTax.Text")
myString = myString.Replace("$$CST_No$$", "Txt_CSTNo.Text")
myString = myString.Replace("$$VAT_TIN_No$$", "Txt_VatNo.Text")
myString = myString.Replace("$$Type_Of_Business_Entity$$", "RadioButtonList1.SelectedValue.ToString")
myString = myString.Replace("$$Full_Details$$", "")
myString = myString.Replace("$$Business_History$$", "Txt_BusinessHistory.Text")
myString = myString.Replace("$$No_Of_Employees$$", "Txt_NoOfEmployees.Text ")
myString = myString.Replace("$$Annual_Turnover$$", "")
myString = myString.Replace("$$References$$", "")
Dim MyMailMessage As New MailMessage()
MyMailMessage.From = New MailAddress("test#eternalbs.com", "Eternal")
MyMailMessage.To.Add("test#eternalbs.com")
MyMailMessage.Subject = "Eternal"
MyMailMessage.Body = myString.ToString()
MyMailMessage.IsBodyHtml = True
' Dim SMTPServer As New SmtpClient("smtp.gmail.com")
Dim SMTPServer As New SmtpClient()
SMTPServer.Host = "208.91.198.227"
SMTPServer.Port = 587
SMTPServer.Credentials = New System.Net.NetworkCredential("test#eternalbs.com", "admin123")
'SMTPServer.EnableSsl = True
Try
SMTPServer.Send(MyMailMessage)
Response.Write("Successfull")
Catch ex As SmtpException
Response.Write(ex)
End Try
End Sub
End Class

For receving the value of any textbox which user has inserted, the value must not be in double inverted commas. In the SendHTMLmail() function, the second parameter where you want the textbox value to be captured, u just need to remove the double inverted commas
myString = myString.Replace("$$Contact_Person_Name$$", Txt_ContactPerson.Text)
Try this for every textbox whose value u want to capture. Hope it helps.

Related

How do I put a serialized object into a json file with visual basic?

I currently have data being pulled from textboxes and comboboxes that are converted into properties of an object, and then that object is seralized into json. I then use the streamwriter function to put that into a json file that has already been created but I just end up with a bunch of weird formatting and things not updating and being added.
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Dim monsterName As String
monsterName = TextBox2.Text
TextBox2.Text = ""
Dim monsterCB As Integer
monsterCB = TextBox3.Text
TextBox3.Text = ""
Dim monsterType As String
monsterType = ComboBox3.SelectedItem
ComboBox3.Text = ""
Dim Monster As New Monster
Monster.MonsterName = monsterName
Monster.MonsterCombatRating = monsterCB
Monster.MonsterType = monsterType
Dim output As String
output = JsonConvert.SerializeObject(Monster)
Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter("C:\Program Files (x86)\D&DLoot\data.json", True)
file.WriteLine(Monster)
file.Close()
End Sub
This is within my form code and is the snippet of when my button is clicked for the current form.
Then, in my json file it just writes a new line of
WinFormsApp1.Monster
So I'm not entirely sure whats going on and what I have missed. I can't see whats wrong with the code above.
Should be using output in the code below instead of monster. Since the serialized object is in the output, not the monster.
file.WriteLine(Monster)
output = JsonConvert.SerializeObject(Monster)

Consuming data from webservice with vb.net

Im doing a webform in vb.net I'm consuming a webservice, Which returns me to all the countries
Only have 1 button Enviar that calls the countries.
Imports service_country = WebServiceVB2.country
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim serv_country As New service_country.country '--Create object'
Dim MyDoc As New System.Xml.XmlDocument
Dim MyXml As String = serv_country.GetCountries() '--Execute procedure from webservice'
MyDoc.LoadXml(MyXml) '--Read Myxml and convert to XML'
Dim SymbolText As String = MyDoc.SelectSingleNode("//NewDataSet/Table/Name").InnerText '--select the node'
Label1.Text = SymbolText
End Sub
My question is How can I select all the values that are inside the 'name'.
Actually it only shows one.
For Example:
Thanks in advance.
This was an interesting problem. Since data is coming as a webpage the open bracket was coming as "& l t ;" while the closing bracket was coming as "& g t ;". So these had to be replaced. I used xml linq to get the names :
Imports System.Xml
Imports System.Xml.Linq
Module Module1
Const URL As String = "http://www.webservicex.net/country.asmx/GetCountries"
Sub Main()
Dim doc1 As XDocument = XDocument.Load(URL)
Dim docStr As String = doc1.ToString()
docStr = docStr.Replace(">", ">")
docStr = docStr.Replace("<", "<")
Dim doc2 As XDocument = XDocument.Parse(docStr)
Dim root As XElement = doc2.Root
Dim defaultNs As XNamespace = root.GetDefaultNamespace()
Dim names() As String = doc2.Descendants(defaultNs + "Name").Select(Function(x) CType(x, String)).ToArray()
End Sub
End Module
Using WebUtility
Imports System.Xml
Imports System.Xml.Linq
Imports System.Text
Imports System.Net
Module Module1
Const URL As String = "http://www.webservicex.net/country.asmx/GetCountries"
Sub Main()
Dim xReader As XmlReader = XmlTextReader.Create(URL)
xReader.MoveToContent()
Dim doc As XDocument = XDocument.Parse(WebUtility.HtmlDecode("<?xml version=""1.0"" encoding=""iso-8859-9"" ?>" & xReader.ReadOuterXml))
Dim root As XElement = doc.Root
Dim defaultNs As XNamespace = root.GetDefaultNamespace()
Dim names() As String = doc.Descendants(defaultNs + "Name").Select(Function(x) CType(x, String)).ToArray()
End Sub
End Module

VB.net Get text/string from html element

I'm having major trouble trying to get bits of elements and returning the strings.
I have a few exmaples of trying to get the strings and what not but failing hard.
HTML Phrasing is difficult for me to do so help would be appreciated.
Explantion of what I need:
I need to get the strinsg of different elements off this site when entering a IP
http://www.ip-tracker.org/
I need pretty much all the details but into labels or text boxes.
Or this with xml phrasing
http://ip-api.com/xml/8.8.8.8
So here is the exmaple that i've used so far but haven't got far with it.
Exmaple 1:
Dim client As New WebClient
Dim ip As String
Dim city As String
Dim Region As String
Private Function GetIp()
Try
Dim Page As String = client.DownloadString("http://www.ip-tracker.org/locator/ip-lookup.php?ip=82.16.38.43/")
ip = Page.Substring(Page.IndexOf("IP Address:") + 80)
ip = ip.Substring(0, city.IndexOf(" </td") + 30)
TextBox2.Text = ("IP Address: " + ip)
Catch ex As Exception
city = "Unable to lookup"
End Try
Return 0
End Function
To call it:
getViews()
Try xml linq
Imports System.Xml
Imports System.Xml.Linq
Module Module1
Dim url As String = "http://ip-api.com/xml/8.8.8.8"
Sub Main()
Dim query As XElement = XElement.Load(url)
Dim status As String = query.Element("status").Value
Dim country As String = query.Element("country").Value
Dim region As String = query.Element("region").Value
Dim regionName As String = query.Element("region").Value
Dim city As String = query.Element("city").Value
Dim zip As String = query.Element("zip").Value
Dim lat As Double = query.Element("lat").Value
Dim lon As Double = query.Element("lon").Value
Dim timezone As String = query.Element("timezone").Value
Dim isp As String = query.Element("isp").Value
Dim org As String = query.Element("org").Value
Dim _as As String = query.Element("as").Value
Dim subQuery As String = query.Element("query").Value
End Sub
End Module

Visual Basic - How to use a variable from one function in another

I have checked Google, and the suggested answers here, but have had no luck unfortunately.
The last thing I need to do is have an email read the rateNbr variable into the email body, but it just comes up empty.
I tried to make Public Function FuncRateCheckFile read as Public Function FuncRateCheckFile(ByVal rateNbr As String), to try and enable it to be called outside the function, but this then breaks the function when it is called elsewhere. :(
Here is the code, with comments as to where I am referring:
Public Function FuncRateCheckFile()
Dim blnContinue As Boolean
Dim strLine As String
Dim strSearchFor, strSearchWrd, LineCount, objFSO, objTextFile, arrLines
Dim dteNow As Date
Dim newDate As String
'//==============================================================================================
'// DECLARED
Dim rateNbr As String
'//==============================================================================================
FuncRateCheckFile = False
blnContinue = True
If blnContinue Then
Const ForReading = 1
'Get todays date and reformat it
dteNow = DateValue(Now)
newDate = Format(dteNow, "dd/MM/yy")
strSearchWrd = newDate
'Read the whole file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(m_RateCheckFile, ForReading)
LineCount = 0
Do Until objTextFile.AtEndOfStream
strLine = objTextFile.ReadLine()
If InStr(strLine, strSearchWrd) <> 0 Then
arrLines = Split(strLine, vbCrLf)
LineCount = LineCount + 1
End If
Loop
'Log a message to state how many lines have todays day, and if there are none, log an error
If LineCount <> 0 Then
'//==============================================================================================
'// "rateNbr" IS WHAT I AM TRYING TO GET TO PUT IN THE EMAIL
LogMessage "Rate file date is correct"
rateNbr = "Number of rates for " & newDate & " in the file recieved on " & newDate & " is " & LineCount
LogMessage rateNbr
EmailAdvice2
objTextFile.Close
'//==============================================================================================
Else
blnContinue = False
LogError "Failed to retrieve Current Rate date, please check rate file.."
EmailAdvice
objTextFile.Close
End If
End If
FuncRateCheckFile = blnContinue
LogMessage "Completed Check Rate file"
End Function
Private Function EmailAdvice2()
Dim strSMTPFrom As String
Dim strSMTPTo As String
Dim strSMTPRelay As String
Dim strTextBody As String
Dim strSubject As String
Dim oMessage As Object
'//==============================================================================================
'// DECLARED AGAIN
Dim rateNbr As String
'//==============================================================================================
Set oMessage = CreateObject("CDO.Message")
strSMTPFrom = "no-reply#work.com.au"
strSMTPTo = "me#work.com.au"
strSMTPRelay = "smtp.relay.com"
'//==============================================================================================
'// THIS MAKES THE TEXT BODY BLANK, BUT THE EMAIL STILL SENDS
strTextBody = rateNbr
'//==============================================================================================
strSubject = "Todays rates"
'strAttachment = "full UNC path of file"
oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strSMTPRelay
oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
oMessage.Configuration.Fields.Update
oMessage.Subject = strSubject
oMessage.From = strSMTPFrom
oMessage.To = strSMTPTo
oMessage.textbody = strTextBody
'oMessage.AddAttachment strAttachment
oMessage.Send
End Function
I am positive that it is blank because I have declared rateNbr under EmailAdvice2() and then not given it anything to fill the variable with. But I don't know how to make it call the variable under FuncRateCheckFile().
Thanks to all for any assistance.
As Plutonix stated, this is a scope issue.
Move the declaration of your 'rateNbr' variable out to class level, and remove the local declarations inside your functions:
Dim rateNbr As String ' <-- out at class level it will be accessible from both functions
Public Function FuncRateCheckFile()
...
' REMOVE both the decalarations of "rateNbr" that are INSIDE your functions
...
End Function
Private Function EmailAdvice2()
...
' REMOVE both the decalarations of "rateNbr" that are INSIDE your functions
...
End Function

Trying to use a HTML file as my email body using iMsg in VB.NET

I've wrote a script to create a HTML file based on a SQL Query.... It has become necessary to have that HTML be emailed. Most of our execs use blackberry's and I want to send the HTML file as the body. I have found a round about way to get this done, by adding a WebBrowser, and having the web browser then load the file, and then using the below code to send. The problem i'm facing is if I automate the code fully, it will only email part of the HTML document, now if I add a button, and make it do the email function, it sends correctly. I have added a wait function in several different location, thinking it may be an issue with the HTML not being fully created before emailing. I have to get this 100% automated. Is there a way I can use the .HTMLBody to link to the actual HTML file stored on the C:(actual path is C:\Turnover.html). Thanks all for any help.
Public Sub Email()
Dim strdate
Dim iCfg As Object
Dim iMsg As Object
strdate = Date.Today.TimeOfDay
iCfg = CreateObject("CDO.Configuration")
iMsg = CreateObject("CDO.Message")
With iCfg.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "xxxxx.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendemailaddress") = """Turnover Report"" <TurnoverReports#xxxxx.com>"
.Update()
End With
With iMsg
.Configuration = iCfg
.Subject = "Turnover Report"
.To = "xxxxx#xxxxx.com"
'.Cc = ""
.HTMLBody = WebBrowserReportView.DocumentText
.Send()
End With
iMsg = Nothing
iCfg = Nothing
End Sub
used the below function to read in a local html file. then set
TextBox2.Text = getHTML("C:\Turnover2.html")
and also
.HTMLBody = TextBox2.Text
Private Function getHTML(ByVal address As String) As String
Dim rt As String = ""
Dim wRequest As WebRequest
Dim wResponse As WebResponse
Dim SR As StreamReader
wrequest = WebRequest.Create(address)
wResponse = wrequest.GetResponse
SR = New StreamReader(wResponse.GetResponseStream)
rt = SR.ReadToEnd
SR.Close()
Return rt
End Function