VB.Net Save HTML Mail Message to TXT or RTF File Format

JB4375
JB4375 used Ask the Experts™
on
I have a simple subroutine that takes data pulled from a web form and submits an email request.

Ultimately, rather that sending the email I want it to save the email as an RTF file that retains the formatting.

If this isn't possible could someone give me a simple example of how to create the RTF with the same parts of the email? Code below.

Thanks,

JB


    Public Sub Send_Email()
        Dim Mail As New MailMessage()
        Dim mailBody As String

        Mail.From = New MailAddress("netadmin@domain.com")
        Mail.To.Add(strUser & "@domain.com")
        Mail.Subject = "Issue with Request Form"
        mailBody = "The following issue: " & DropDownList1.SelectedItem.Text & " has been reported by " & strUser & "." & "<br/>" & "<br/>"
        mailBody &= "Problem Description: " & TextBox5.Text & "<br/>" & "<br/>"
        mailBody &= "Thank you," & "<br/>" & "<br/>"
        mailBody &= "Information Services" & "<br/>"
        Mail.Body = mailBody
        Mail.Priority = MailPriority.High ' Normal, Low or High
        Mail.IsBodyHtml = True

        Dim smtp As New SmtpClient("mail.domain.com")
        Try
            smtp.Send(Mail)
        Catch ex As Exception
            MsgBox(Err.Description & vbCrLf & ex.ToString)
            Exit Sub
        End Try
    End Sub
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Try this - http://www.aspsnippets.com/Articles/Contact-Us-Form-with-Rich-TextBox-in-ASP.Net.aspx

VB.Net

Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As EventArgs)

   Dim mm As New MailMessage("sender@gmail.com", "receiver@gmail.com")

   mm.Subject = txtSubject.Text

   mm.Body = "Name: " & txtName.Text & "<br /><br />Email: " & txtEmail.Text & "<br />" & txtBody.Text

   If FileUpload1.HasFile Then

       Dim FileName As String = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName)

       mm.Attachments.Add(New Attachment(FileUpload1.PostedFile.InputStream, FileName))

   End If

   mm.IsBodyHtml = True

   Dim smtp As New SmtpClient()

   smtp.Host = "smtp.gmail.com"

   smtp.EnableSsl = True

   Dim NetworkCred As New System.Net.NetworkCredential()

   NetworkCred.UserName = "sender@gmail.com"

   NetworkCred.Password = "xxxxx"

   smtp.UseDefaultCredentials = True

   smtp.Credentials = NetworkCred

   smtp.Port = 587

   smtp.Send(mm)

   lblMessage.Text = "Email Sent SucessFully."

End Sub
Most Valuable Expert 2012
Top Expert 2014
Commented:

Author

Commented:
I think we have a miscommunication. I have a lot of forms that send notifications via email and I don't need to do that type of notification any longer. I just need the simplest way to turn what I have into a file.

Example:

Rather than:  smtp.Send(Mail).... save it as mail.RTF

OR

Instead of mail.body use body and create an RTF File.
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

Most Valuable Expert 2012
Top Expert 2014
Commented:
Do a

IO.File.WriteAllText("path", mail.body)
Try the following :

SaveFormsData
true to save the data entered by a user in a form as a data record.

http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.document.saveas(VS.80).aspx

Example
The following code example uses the SaveAs method to save the document in RTF format using the new file name myfile.doc.

Visual Basic
Private Sub DocumentSaveAs()

    Me.SaveAs(FileName:="myfile.doc", FileFormat:=Word.WdSaveFormat.wdFormatRTF, _
        LockComments:=False, AddToRecentFiles:=True, ReadOnlyRecommended:=False, _
        EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=True, _
        SaveFormsData:=True, SaveAsAOCELetter:=False, _
        Encoding:=Office.MsoEncoding.msoEncodingUSASCII, _
        InsertLineBreaks:=False, AllowSubstitutions:=False, _
        LineEnding:=Word.WdLineEndingType.wdCRLF, _
        AddBiDiMarks:=False)
End Sub

Author

Commented:
Hey Code Cruiser,

It's been a while. The second answer was definitely what I needed, and I'm going to need the info supplied in the first answer for some formatting.

To use a Mr. Deeds reference:

Genius!! Genius!! Genius!!!

Many thanks,

JB

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial