Reading emails...

R

Richard Browne

I have been developing a program that takes a database of
email bodies and then displays them to the user in a
similar way to Outlook Express would.

The problem I'm having is showing only the message of the
email and not some of the unnecessary jargon of the
message body.

Does anyone know of a really simple and clean way to
display only the message of the email body.

Below is a copy of the code that I'm currently using:

Code:
Private Sub MessageList_SelectedIndexChanged(ByVal
sender As System.Object, ByVal e As System.EventArgs)
Handles MessageList.SelectedIndexChanged
Try
'Lets make sure that we're not selecting
multiple emails to view, as this would be a bit difficult.
If (MessageList.SelectedItems.Count <> 0) And
(MessageList.SelectedItems.Count = 1) Then
'Connector/ODBC 3.51 connection string
Dim MyConString As String = "DRIVER=
{MySQL ODBC 3.51 Driver};" & _
"SERVER=localhost;" & _
"DATABASE=temp;" & _
"UID=temp;" & _
"PASSWORD=temp;" & _
"OPTION=3;"

'Connection

Dim MyConnection As New
Odbc.OdbcConnection(MyConString)
MyConnection.Open()
Dim MyCommand As New Odbc.OdbcCommand
MyCommand.Connection = MyConnection

'Select the specific email using the data
taken from the selected entry of the Messages List view
box.
If iFolder_Id <> "Deleted" Then
MyCommand.CommandText = "SELECT *
FROM webmail_mails WHERE reciever='" & iUser_Id & "' AND
folder='" & iFolder_Id & "' AND subject='" &
MessageList.SelectedItems(0).Text & "' LIMIT 1"
Else
MyCommand.CommandText = "SELECT *
FROM webmail_mails WHERE reciever='" & iUser_Id & "' AND
trashed='true' AND subject='" & MessageList.SelectedItems
(0).Text & "' LIMIT 1"
End If
Console.WriteLine(MyCommand.CommandText)
Dim MyDataReader As Odbc.OdbcDataReader
MyDataReader = MyCommand.ExecuteReader
(CommandBehavior.Default)

'Some variables being declared.
Dim sSubject As String
Dim sSender As String
Dim sSenderName As String
Dim sMsg As String
Dim dDate As String
Dim bAttachments As Boolean

While MyDataReader.Read
'Assign the data to the variables
sSubject = MyDataReader
("subject").ToString
sSender = MyDataReader
("sender").ToString
sSenderName = MyDataReader
("sendername").ToString
sMsg = MyDataReader("body").ToString
dDate = MyDataReader("date").ToString
dDate = dDate.Substring(5, 20)
bAttachments = True
End While

'Display the message in a formatted way.
'First we need to find out what content
type the email is
Dim strString As String = sMsg
Dim strString2 As String = sMsg
Dim type As Integer
Dim start As Integer
Dim finish As Integer
type = strString.IndexOf("Content-Type:")


Dim content As String
Dim content2 As String

content = sMsg.Substring(type + 14, 11)
'Now that we have the content type just
make it all lower for checking.
content = content.ToLower

'Is the content type:
'Text/Plain?
If content = "text/plain;" Then
start = strString.IndexOf(vbNewLine &
vbNewLine)
content2 = sMsg.Substring(start)
content2 = content2.Trim

email.Text = content2
End If
'Multipart/m?
If content = "multipart/m" Then
start = strString.IndexOf("<html>")
'MessageBox.Show(start)
If start < 1 Then
'This means that although it's
Multipart it may be <HTML>
start = strString.IndexOf
("<HTML>")
'MessageBox.Show(start)
If start < 1 Then
'There is no message!
email.Text = ""
Else
finish = strString.IndexOf
("</HTML>")
content2 = sMsg.Substring
(start, ((finish - start) + 7))
email.Text = content2
End If
Else
finish = strString.IndexOf
("</html>")
content2 = sMsg.Substring(start,
((finish - start) + 7))
email.Text = content2
End If
End If
'Multipart/a?
If content = "multipart/a" Then
start = strString.IndexOf("<html>")
'MessageBox.Show(start)
If start < 1 Then
'This means that although it's
Multipart it may be <HTML>
start = strString.IndexOf
("<HTML>")
'MessageBox.Show(start)
If start < 1 Then
'There is no message!
email.Text = "[There is no
message to display.]"
Else
finish = strString.IndexOf
("</HTML>")
content2 = sMsg.Substring
(start, ((finish - start) + 7))
email.Text = content2
End If
Else
finish = strString.IndexOf
("</html>")
content2 = sMsg.Substring(start,
((finish - start) + 7))
email.Text = content2
End If
End If
'Text/HTML?
If content = "text/html; " Then
start = strString.IndexOf("<html>")
'MessageBox.Show(start)
If start < 1 Then
'This means that although it's
Multipart it may be <HTML>
start = strString.IndexOf
("<HTML>")
'MessageBox.Show(start)
If start < 1 Then
'There is no message!
email.Text = "[There is no
message to display.]"
Else
finish = strString.IndexOf
("</HTML>")
content2 = sMsg.Substring
(start, ((finish - start) + 7))
email.Text = content2
End If
Else
finish = strString.IndexOf
("</html>")
content2 = sMsg.Substring(start,
((finish - start) + 7))
email.Text = content2
End If
End If

'Now lets add the data to the infobar
infobar.Text = "Subject: " & sSubject &
vbNewLine & "From:     " & sSenderName & vbNewLine
& "Date:      " & dDate
'infobar2.Text = "From: " & sSenderName
'Close the connection and the reader
MyDataReader.Close()
MyCommand.Connection.Close()

End If

'The following deals with any errors that
have occured in the process of the above.
'At present they only output to the console.

'Catch ODBC Exception
Catch MyOdbcException As Odbc.OdbcException
Dim i As Integer
Console.WriteLine(MyOdbcException.ToString)

'Catch program exception
Catch MyException As Exception
Console.WriteLine(MyException.ToString)
End Try
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top