MailMessage removing consecutive spaces - vb.net

I'm using an SmtpClient object to send a MailMessage from one MailAddress to another. I'm trying to format the body of the MailMessage to look centered using a monospace font, but the consecutive spaces are always being replaced by a single space.
So:
With smtpMessage
.IsBodyHtml = True
.Body = "<body style=""font-family:monospace"">"
.Body &= " ************************************************************<br>"
.Body &= " * This email was generated by an automated system. *<br>"
.Body &= " * Please do not reply to this email. *<br>"
.Body &= " ************************************************************<br>"
.Body &= "</body>"
End With
Is being received as:
************************************************************
* This email was generated by an automated system. *
* Please do not reply to this email. *
************************************************************
I've searched around but all I'm finding are topics about line breaks not displaying properly, nothing about the spaces themselves.

The spaces are removed not by MailMessage, but by the use of HTML. HTML standard is to ignore extra spaces. If you truly wish to keep the extra spaces, you should either add the "<pre>" tag, or use .

In HTML, multiple spaces in code are replaced by one space. You can try to force a space to appear.
OR you can try composing your message as plain text (IsHtml = false) (and remove any HTML from message body).

Related

VB.Net Hyperlinking in EWS email with class properties

I need help getting the hyperlink to work as well as getting the value stored in YestFile.FileName to show up.
I have a list of objects of class FileFromYesterday (Collection2) with 3 properties. Two properties are just Strings (FileName and Entity) but one property (URL) contains a URL as a String. I am trying to hyperlink the URL to FileName in the main.
I have a feeling that I need to use HTML to format the email somehow but I have no Idea where to begin
Dim HyperLink As String
Dim FileLine As String
Dim NewBody As String
Dim message As New EmailMessage(EWS)
message.Subject = "Monthly Financial Imported into Docushare on " & String.Format("{0:MM-dd-yyyy}", Yesterday)
NewBody = "Total of " & Collection2.Count & " Files Imported." & "\n"
For Each YestFile In Collection2
HyperLink = YestFile.FileName.Replace(".pdf", "")
FileLine = HyperLink & "Entity: " & YestFile.Entity & Environment.NewLine
NewBody = NewBody & FileLine
Next
You should be creating a HTML document in your NewBody varible so you need the pre and post tags for a HTML document eg
<html>
<body>
</body>
</html>
the body content you want should go within between the Body Tags and for thing like NewLines you need to use the appropriate HTML tags such as
<br />
Eg what ever you end up creating in you NewBody variable you should be able to write out to a file or test in any Online HTML validator https://validator.w3.org/#validate_by_input to see if its valid and how it will look before trying to send it.

Find and Replace specific string in Outlook message

My code basically searches for the string "#XX" in the email body. The "#XX" is usually followed by a text like "#XXApple". And this "#XXApple" can be seen multiple times in the email message.
The code below works in such a way that it only replaces the first hit with spaces. However, the rest of the "#XXApple" will only be changed to "Apple"
Is there a way where I can do a "Find and Replace All" in Outlook?
obj.HTMLBody = Replace(obj.HTMLBody, "#XX", " ", 15)
Not tested or verified but this is the general idea of what I meant to say in the comment above.
Dim Cet
Dim TesPos As Int, i As Int
Cet = Split(obj.HTMLBody, " ")
For i=LBound(Cet) to Ubound(Cet)
TestPos = 5
TestPos = InStr(1,Cet(i), "#XX", CompareMethod.Text)
if TestPos = 1 then
Cet(i) = ""
Else: End if
Next i
obj.HTMLBody = ""
For i=LBound(Cet) to Ubound(Cet)
obj.HTMLBody = obj.HTMLBody & " " & Cet(i)
Next i
Debug.Print obj.HTMLBody
The Outlook object model provides three main ways for working with item bodies:
Body - a string representing the clear-text body of the Outlook item.
HTMLBody - a string representing the HTML body of the specified item.
Word editor - the Microsoft Word Document Object Model of the message being displayed. The WordEditor property of the Inspector class returns an instance of the Document class from the Word object model which you can use to set up the message body.
You can read more about all these ways in the Chapter 17: Working with Item Bodies. It us up to you which way is to choose to deal with the message body. But the Word object model provides all the required methods to get the job done.

Sending email issue VB.net

I have command line app that send an email and the issue is when i open the console and type all parameters (SendEmail.exe recipient subject message Sender Senderpass) email goes through OK, but when I do that from other application
Dim p As New ProcessStartInfo
Dim Tsubject As String = "TEST "
p.FileName = "SendEmail.exe"
p.Arguments = EmailReceivers & " " & subject & " " & " Message " & EmailSender & " " & SenderPass
p.WindowStyle = ProcessWindowStyle.Normal
Process.Start(p)
Then I get no error from SendEmail.exe it says Email Successfully Sent but I don't receive anything.
Maybe the character encoding messes around. Especially the "#" character might get messed up. Writing a batch application echo your argument will show you, if you can exclude this.
Do u have enclosed the subject in quotes? Otherwise a space might mess up the arguments.
Because you attach an executable file(.exe) in your email, so the target email server may filter your mail as mailware or sth dangerous and can harm the receiver.

How to copy mailItem.Body with line breaks to a website Textarea?

I'm using objItem.Body to fill in a website text area.
With HTML emails it adds spaces for line breaks or something, how do I fix that? Here is an example e-mail.
Using ie.document.getElementById("message").Value = objItem.Body you can see that two extra spaces/returns are added. It also adds HYPERLINK="mailto:xxxxx".
Converting to plain text with something like:
objitem.BodyFormat = olFormatPlain
objItem.Body = Replace(objItem.Body, " " & vbCrLf, vbCr)
retains the extra spacings and ruins the original email (I want to keep screenshots etc).
How can I get the mailItem.Body to transfer correctly?
The only thing left is removing Hyperlinks! IE HYPERLINg "mailto:email"email and HYPERLINK"website"website
This is how to get the objitem.body to do correct spacings on html emails..
ie.document.getElementById("message").Value = Replace(objItem.Body, vbCrLf & vbCrLf, vbCrLf)

Forward an email and append additional text without losing format of original message

I'm trying to forward an email that I received and append an additional message on top of it. The following code I wrote kinda does this, but I lose all the formatting of the original message. Is there any way to maintain the format of the original message and yet able to append additional test to the email?
MY CODE:
Sub xForward()
myMessage = "You recently requested access to the table. We are requiring all requests to complete a mandatory training session." & vbCr & vbCr & "Thank you, " & vbCr & "Ricky"
Dim itmOld As MailItem, itmNew As MailItem
Set itmOld = ActiveInspector.CurrentItem
Set itmNew = itmOld.Forward
itmNew.Body = myMessage & vbCr & vbCr & itmOld.Body
itmNew.Subject = "Access Request"
itmNew.Display
Set itmOld = Nothing
Set itmNew = Nothing
End Sub
If I don't update the body of itmNew, then I maintain the format of the original message. The moment I update itmNew.Body, then itmOld.Body is written in simple text and I lose all the formatting.
I think JP's comment points you in the right direction but I assume your questions results from limited knowledge of HTML. This is not a complete tutorial on HTML but I hope it gets your started.
If you use Debug.Print to output .HTMLBody to the immediate window you will see something like:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html
xmlns="http://www.w3.org/1999/xhtml"> <head>
Lots of stuff here
</head> <body>
Lots of stuff here
</body> </html>
You will only get "<!DOCTYPE html ... " if the package that created the message supports the XML version of HTML. The minimum you should see is:
<html><head> Lots of stuff here </head><body> Lots of
stuff here </body></html>
If you place your extra message in front or at the end of this then you are breaking the rules of HTML. What happens will depend on how forgiving the receiver's email package is. To conform to the rules of HTML, you must place your extra message somewhere between "<body>" and "</body>".
If you look through a few messages, you will see how much they can vary. Some will be black text on white, some white text on black with every variation in between. Your message must be readable no matter what the message's authors have done. My suggestion is you create a one cell table at the top and you set the font and background colours. Try the following and then adapt it to your requirements:
Dim AddedMsg As String
Dim Pos As Long
' Create message to be inserted
' =============================
' Start a table with white background and blue text
AddedMsg = "<table border=0 width=""100%"" style=""Color: #0000FF"" bgColor=#FFFFFF>"
' Add row with single cell
AddedMsg = AddedMsg & "<tr><td><p>Cool stuff you must see!!</p></td></tr>"
' End table
AddedMsg = AddedMsg & "</table>"
' Code to add message once you have checked there is an HTML body
'================================================================
Pos = InStr(1, LCase(.HTMLBody), "<body")
If Pos = 0 Then
' This should be impossible.
Call MsgBox("<Body> element not found in HTML body", vbCritical)
' Code to handle impossible situation
End If
Pos = InStr(Pos, .HTMLBody, ">")
If Pos = 0 Then
' This should be impossible.
Call MsgBox("Terminating > for <Body> element not found in HTML body", vbCritical)
' Code to handle impossible situation
End If
'Insert your message
.HTMLBody = Mid(.HTMLBody, 1, Pos) & AddedMsg & Mid(.HTMLBody, Pos + 1)