Solved

Email strips crlf and replaces with spaces

Posted on 2009-04-08
3
325 Views
Last Modified: 2012-05-06
I have a piece of code that sends out emails, it works absolutely fine except when I want to include crlfs in the body. It replaces them with spaces. Well actually, the first crlf works OK, but the ones after the first do not. I wonder if that is important?

In other applications, I'm inserting the emails into an Oracle table, then using an email application that runs every 30 mins to check this table and send out any pending emails. When I am originally inserting the email into the table, I'm using the vbCrLf character if that makes any difference.

The code is below and if you want to test it, you can use something like this for the body of the email:

Customer ID: 123456
Customer Name: TOOL & EQUIPMENT SUPPLIES
Order ID: C12345
Order Amount: $500.00
Requested By: BRANEY

If you notice in the code below, the debug.print line checks what it is right before it sends and it seems to be perfectly fine at this point. I've also tried it as an HTML email and that doesnt work either.
Private Function SendEmail(ByVal strTo As String, ByVal strSubject As String, ByVal strBody As String, ByVal strAttachment As String, ByVal strFrom As String, Optional ByVal CCTo As String = "") As Boolean
 
        ' Take the parameters passed in the function and create an email message
        Dim oMessage As MailMessage = New MailMessage(strFrom, strTo, strSubject, strBody)
 
        oMessage.IsBodyHtml = False
 
        ' Does the message have a cc address?
        If CCTo <> "" Then
            oMessage.CC.Add(CCTo)
        End If
        
        ' Does the message have an attachment?
        Dim oAttachment As Attachment = Nothing
        If strAttachment <> "" Then
            oAttachment = New Attachment(strAttachment)
            oMessage.Attachments.Add(oAttachment)
        End If
 
        Try
            Dim oClient As New SmtpClient(My.Settings.SMTPServer, My.Settings.PortNumber)
 
            Debug.Print(oMessage.Body)
 
            oClient.Send(oMessage)
 
            ' Release the attachment if it exists
            If oAttachment IsNot Nothing Then
                oAttachment.Dispose()
                oAttachment = Nothing
            End If
 
            ' Email sent successfully
            Return True
 
        Catch ex As System.Net.WebException
 
            ' Email Failed
            Console.WriteLine("0", ex.Message)
            Console.Write("0", ex.ToString())
            Return False
 
        End Try
    End Function

Open in new window

0
Comment
Question by:braney
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
burningmace earned 50 total points
ID: 24100377
Try replacing the vbCrLfs with the <br /> HTML tag. For example "Customer Name: Bob<br />Order Total: $500". I think the SmtpClient defaults to HTML email these days - but don't quote me on that.
0
 

Author Comment

by:braney
ID: 24100697
Worked great that, thanks!

I just switched it to

oMessage.IsBodyHtml = True

and added a line before it makes the message

strBody = strBody.Replace(vbCrLf, "<br />")

and seems to have worked fine!
0
 
LVL 5

Expert Comment

by:burningmace
ID: 24100948
Glad to be of help :)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
The purpose of this video is to demonstrate how to set up a Mailchimp campaign. This will include styling and adding elements to a newsletter/email. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchim…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question