将纯文本转换为电子邮件文本 [英] converting plain text into email text
本文介绍了将纯文本转换为电子邮件文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,我正在做一个vb程序,它是一个连接到电子邮件服务器并连接到电子邮件地址的电子邮件客户端。一切正常,问题在于,当我阅读它时,它以纯文本形式提供给我,html格式的所有字符使其难以理解,有没有办法使它好像我在hotmail中读取它或外表。这是我的代码
Hi everyone, well i am doing a vb program which is a email client which connects to the server of the email and connects to the email address. it all works fine the problem is that when I read it it gives it to me in plain text with all the characters of the html format making it hard to understand, is there a way to make it as if i was reading it in hotmail or outlook. here is my code
Imports System.IO
Imports System.Net.Sockets
Imports System.Text
Imports System.Net.Security
Imports System.Net
Class Form1
Private Function StripHTML(ByVal source As String) As String
Try
Dim result As String
' Remove HTML Development formatting
' Replace line breaks with space
' because browsers inserts space
result = source.Replace(vbCr, " ")
' Replace line breaks with space
' because browsers inserts space
result = result.Replace(vbLf, " ")
' Remove step-formatting
result = result.Replace(vbTab, String.Empty)
' Remove repeating spaces because browsers ignore them
result = System.Text.RegularExpressions.Regex.Replace(result, "( )+", " ")
' Remove the header (prepare first by clearing attributes)
result = System.Text.RegularExpressions.Regex.Replace(result, "])*>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
result = System.Text.RegularExpressions.Regex.Replace(result, "()", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
result = System.Text.RegularExpressions.Regex.Replace(result, "().*()", String.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
' remove all scripts (prepare first by clearing attributes)
result = System.Text.RegularExpressions.Regex.Replace(result, "])*>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
result = System.Text.RegularExpressions.Regex.Replace(result, "()", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
'result = System.Text.RegularExpressions.Regex.Replace(result,
' @"()([^(\.)])*()",
' string.Empty,
' System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, "().*()", String.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
' remove all styles (prepare first by clearing attributes)
result = System.Text.RegularExpressions.Regex.Replace(result, "])*>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
result = System.Text.RegularExpressions.Regex.Replace(result, "()", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
result = System.Text.RegularExpressions.Regex.Replace(result, "().*()", String.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
' insert tabs in spaces of tags
result = System.Text.RegularExpressions.Regex.Replace(result, "])*>", vbTab, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
' insert line breaks in places of and tags()
result = System.Text.RegularExpressions.Regex.Replace(result, "", vbCr, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
result = System.Text.RegularExpressions.Regex.Replace(result, "", vbCr, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
' insert line paragraphs (double line breaks) in place
' if,and tags
result = System.Text.RegularExpressions.Regex.Replace(result, "])*>", vbCr & vbCr, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
result = System.Text.RegularExpressions.Regex.Replace(result, "])*>", vbCr & vbCr, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
result = System.Text.RegularExpressions.Regex.Replace(result, "])*>", vbCr & vbCr, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
' Remove remaining tags like , links, images,
' comments etc – anything that’s enclosed inside
result = System.Text.RegularExpressions.Regex.Replace(result, "]*>", String.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
' replace special characters:
result = System.Text.RegularExpressions.Regex.Replace(result, " ", " ", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
result = System.Text.RegularExpressions.Regex.Replace(result, "•", " * ", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
result = System.Text.RegularExpressions.Regex.Replace(result, "‹", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
result = System.Text.RegularExpressions.Regex.Replace(result, "™", "(tm)", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
result = System.Text.RegularExpressions.Regex.Replace(result, "⁄", "/", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
'result = System.Text.RegularExpressions.Regex.Replace(result, "<", "", ">", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
result = System.Text.RegularExpressions.Regex.Replace(result, "©", "(c)", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
result = System.Text.RegularExpressions.Regex.Replace(result, "®", "(r)", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
' Remove all others. More can be added, see
' http://hotwired.lycos.com/webmonkey/reference/special_characters/
result = System.Text.RegularExpressions.Regex.Replace(result, "&(.{2,6});", String.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
' for testing
'System.Text.RegularExpressions.Regex.Replace(result,
' this.txtRegex.Text,string.Empty,
' System.Text.RegularExpressions.RegexOptions.IgnoreCase);
' make line breaking consistent
result = result.Replace(vbLf, vbCr)
' Remove extra line breaks and tabs:
' replace over 2 breaks with 2 and over 4 tabs with 4.
' Prepare first to remove any whitespaces in between
' the escaped characters and remove redundant tabs in between line breaks
result = System.Text.RegularExpressions.Regex.Replace(result, "(" & vbCr & ")( )+(" & vbCr & ")", vbCr & vbCr, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
result = System.Text.RegularExpressions.Regex.Replace(result, "(" & vbTab & ")( )+(" & vbTab & ")", vbTab & vbTab, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
result = System.Text.RegularExpressions.Regex.Replace(result, "(" & vbTab & ")( )+(" & vbCr & ")", vbTab & vbCr, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
result = System.Text.RegularExpressions.Regex.Replace(result, "(" & vbCr & ")( )+(" & vbTab & ")", vbCr & vbTab, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
' Remove redundant tabs
result = System.Text.RegularExpressions.Regex.Replace(result, "(" & vbCr & ")(" & vbTab & ")+(" & vbCr & ")", vbCr & vbCr, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
' Remove multiple tabs following a line break with just one tab
result = System.Text.RegularExpressions.Regex.Replace(result, "(" & vbCr & ")(" & vbTab & ")+", vbCr & vbTab, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
' Initial replacement target string for line breaks
Dim breaks As String = vbCr & vbCr & vbCr
' Initial replacement target string for tabs
Dim tabs As String = vbTab & vbTab & vbTab & vbTab & vbTab
For index As Integer = 0 To result.Length - 1
result = result.Replace(breaks, vbCr & vbCr)
result = result.Replace(tabs, vbTab & vbTab & vbTab & vbTab)
breaks = breaks & vbCr
tabs = tabs & vbTab
Next
' That’s it.
Return result
Catch
'MessageBox.Show("Error")
Return source
End Try
End Function
Dim PopHost As String
Dim UserName As String
Dim Password As String
Dim PortNm As Integer
Dim POP3 As New TcpClient
Dim Read_Stream As StreamReader
Dim NetworkS_tream As NetworkStream
Dim m_sslStream As SslStream
Dim server_Command As String
Dim ret_Val As Integer
Dim m_buffer() As Byte
Dim StatResp As String
Dim server_Stat(2) As String
Dim Index_Num As Integer
Dim StrRetr As String
Dim Server_Reponse As String
Private Sub Label10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label10.Click
Application.Exit()
End Sub
Function SendCommand(ByVal SslStrem As SslStream, ByVal Server_Command As String) As String
Server_Command = Server_Command & vbCrLf
m_buffer = System.Text.Encoding.ASCII.GetBytes(Server_Command.ToCharArray())
m_sslStream.Write(m_buffer, 0, m_buffer.Length)
Read_Stream = New StreamReader(m_sslStream)
Server_Reponse = Read_Stream.ReadLine()
Return Server_Reponse
End Function
Private Sub CmdDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdDownload.Click
If POP3.Connected = True Then
CloseServer()
POP3 = New TcpClient(PopHost, Integer.Parse(TxtPort.Text))
ret_Val = 0
Exit Sub
Else
PopHost = TxtHost.Text
UserName = TxtUsrNm.Text
Password = TxtPass.Text
PortNm = Integer.Parse(TxtPort.Text)
Cursor = Cursors.WaitCursor
POP3 = New TcpClient(PopHost, Integer.Parse(TxtPort.Text))
NetworkS_tream = POP3.GetStream()
m_sslStream = New SslStream(NetworkS_tream)
m_sslStream.AuthenticateAsClient(PopHost)
Read_Stream = New StreamReader(m_sslStream)
ListBox1.Items.Add(Read_Stream.ReadLine())
server_Command = "USER " + UserName + vbCrLf
m_buffer = System.Text.Encoding.ASCII.GetBytes(server_Command.ToCharArray())
m_sslStream.Write(m_buffer, 0, m_buffer.Length)
ListBox1.Items.Add(Read_Stream.ReadLine())
server_Command = "PASS " + Password + vbCrLf
m_buffer = System.Text.Encoding.ASCII.GetBytes(server_Command.ToCharArray())
m_sslStream.Write(m_buffer, 0, m_buffer.Length)
ListBox1.Items.Add(Read_Stream.ReadLine())
'Send STAT command to get information ie: number of mail and size
server_Command = "STAT " + vbCrLf
m_buffer = System.Text.Encoding.ASCII.GetBytes(server_Command.ToCharArray())
m_sslStream.Write(m_buffer, 0, m_buffer.Length)
ListBox1.Items.Add(Read_Stream.ReadLine())
End If
Cursor = Cursors.Default
CmdClose.Enabled = True
CmdDownload.Enabled = False
Label8.Enabled = True
'Get Messages count
StatResp = ListBox1.Items(ListBox1.Items.Count - 1)
server_Stat = StatResp.Split(" ")
MsgCount.Text = server_Stat(1)
ret_Val = 1
'Get list of E-mails, where E-mail count = msgcount.text (3-Oct-2012)
If ret_Val = 0 Then
ListBox2.Items.Add("You are not connected, please connect")
Exit Sub
Else
End If
End Sub
Private Sub MinimizeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MinimizeToolStripMenuItem.Click
If Me.WindowState = FormWindowState.Normal Then
Me.WindowState = FormWindowState.Minimized
End If
End Sub
Private Sub Form1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
If e.Button = Windows.Forms.MouseButtons.Right Then
ContextMenuStrip1.Show(Me, e.Location)
End If
End Sub
Sub getMesgs(ByVal Num_Emails As Integer)
Dim List_Resp As String
Dim I As Integer
Cursor = Cursors.WaitCursor
ProgressBar1.Value = 0
ProgressBar1.Maximum = Num_Emails
ProgressBar1.Step = 1
Cursor = Cursors.WaitCursor
For I = 1 To Num_Emails
ProgressBar1.PerformStep()
List_Resp = SendCommand(m_sslStream, "LIST " & I.ToString)
ListBox2.Items.Add(List_Resp)
Next I
ProgressBar1.Value = 0
Label8.Enabled = False
Cursor = Cursors.Default
End Sub
Sub CloseServer()
StatResp = SendCommand(m_sslStream, "QUIT ") & vbCrLf
ListBox1.Items.Add(StatResp)
ListBox2.Items.Clear()
TextBox1.Text = String.Empty
MsgCount.Clear()
POP3.Close()
ret_Val = 0
End Sub
Private Sub CmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdClose.Click
Cursor = Cursors.Default
CmdClose.Enabled = False
CmdDownload.Enabled = True
Label8.Enabled = False
TxtHost.Text = ""
TxtPass.Text = ""
TxtPort.Text = ""
TxtUsrNm.Text = ""
CloseServer()
End Sub
Private Sub Label9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label9.Click
_Settings.Show()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
On Error Resume Next
'Load last profile selected.
Dim ProfileName As String = GetSetting("EmailProfiles", "Selected", "Name")
TxtHost.Text = GetSetting("EmailProfiles", ProfileName, "HostName")
TxtUsrNm.Text = GetSetting("EmailProfiles", ProfileName, "username")
TxtPass.Text = GetSetting("EmailProfiles", ProfileName, "password")
TxtPort.Text = GetSetting("EmailProfiles", ProfileName, "PortNo")
End Sub
Private Sub Label8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label8.Click
If Not IsNumeric(MsgCount.Text) Then
MsgCount.Text = "error : Restart App."
Exit Sub
End If
getMesgs(MsgCount.Text)
End Sub
Private Sub ListBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox2.SelectedIndexChanged
'Retrieve E-mails by Index
Dim XX As String
Dim sZTMP As String
Cursor.Current = Cursors.WaitCursor
Try
TextBox1.Clear()
Index_Num = (ListBox2.SelectedIndex + 1).ToString
XX = ("RETR " + Index_Num.ToString & vbCrLf)
m_buffer = System.Text.Encoding.ASCII.GetBytes(XX.ToCharArray())
m_sslStream.Write(m_buffer, 0, m_buffer.Length)
Read_Stream = New StreamReader(m_sslStream)
Read_Stream.ReadLine()
sZTMP = Read_Stream.ReadLine + vbCrLf
Do While Read_Stream.Peek <> -1
sZTMP = Read_Stream.ReadLine + vbCrLf
TextBox1.Text += (sZTMP)
Loop
Cursor.Current = Cursors.Default
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
txtCon.Text = (StripHTML(TextBox1.Text))
TextBox1.Text = txtCon.Text
End Sub
End Class
'End here
This is the part that does the output of email to textbox:
This is the part that does the output of email to textbox:
Private Sub ListBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox2.SelectedIndexChanged
'Retrieve E-mails by Index
Dim XX As String
Dim sZTMP As String
Cursor.Current = Cursors.WaitCursor
Try
TextBox1.Clear()
Index_Num = (ListBox2.SelectedIndex + 1).ToString
XX = ("RETR " + Index_Num.ToString & vbCrLf)
m_buffer = System.Text.Encoding.ASCII.GetBytes(XX.ToCharArray())
m_sslStream.Write(m_buffer, 0, m_buffer.Length)
Read_Stream = New StreamReader(m_sslStream)
Read_Stream.ReadLine()
sZTMP = Read_Stream.ReadLine + vbCrLf
Do While Read_Stream.Peek <> -1
sZTMP = Read_Stream.ReadLine + vbCrLf
TextBox1.Text += (sZTMP)
Loop
Cursor.Current = Cursors.Default
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
推荐答案
You don’’t need to strip HTML. You can either make content type of all message \"text/html\", or you can make is a multipart email, with all or some of the parts of this content type. Besides, via content disposition you can use some part as alternatives to be used by some mail reader software not capable of HTML rendering.
And first of all, you need to use the classSystem.Net.Mail.MailMessage
:
http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx[^].
Please see my past answers:
Send Email using SMTP in C#[^],
Add control in Outlook email body[^].
—SA
You don''t need to strip HTML. You can either make content type of all message "text/html", or you can make is a multipart email, with all or some of the parts of this content type. Besides, via content disposition you can use some part as alternatives to be used by some mail reader software not capable of HTML rendering.
And first of all, you need to use the classSystem.Net.Mail.MailMessage
:
http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx[^].
Please see my past answers:
Send Email using SMTP in C#[^],
Add control in Outlook email body[^].
—SA
Ok i get that but how would I use that in connection with the whole actual mail browser I have going on?
Ok i get that but how would I use that in connection with the whole actual mail browser I have going on?
这篇关于将纯文本转换为电子邮件文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文