strange problem with smtpmail

  • Thread starter Thread starter Michel Posseth [MCP]
  • Start date Start date
M

Michel Posseth [MCP]

This is weird



i have a method :

Public Function SendMailMessage(ByVal Recipient As String, ByVal Body As
String, ByVal From As String, Optional ByVal BCC As String = "", Optional
ByVal CC As String = "", Optional ByVal Subject As String = "", Optional
ByVal IsBodyHtml As Boolean = True) As Boolean
Try
Dim mMailMessage As New MailMessage()
mMailMessage.From = New MailAddress(From)
mMailMessage.To.Add(New MailAddress(Recipient))

If Not String.IsNullOrEmpty(BCC) Then
mMailMessage.Bcc.Add(New MailAddress(BCC))
End If

If Not String.IsNullOrEmpty(CC) Then
mMailMessage.CC.Add(New MailAddress(CC))
End If

mMailMessage.Subject = Subject
mMailMessage.Body = Body

mMailMessage.IsBodyHtml = IsBodyHtml
mMailMessage.Priority = MailPriority.Normal

Dim mSmtpClient As New SmtpClient(My.Settings.SMTPHost,
My.Settings.SMTPPort)
mSmtpClient.Send(mMailMessage)
Catch ex As Exception
Return False
End Try
Return True
End Function

wich works perfect on a local system when sending e-mail in HTML format
however when the method is run in a server process the e-mails that i
receive loose there HTML markup


so short :
Local nice formated html message , remote running on unattended sewrver
process but method in the same dll with all the same settings mail arrives
as HTML source code

anyone an idea ??? what i am missing

here is the full source of the server process ( wich is started by a
service with reflection )

MAIL
Imports System.Text.RegularExpressions
Imports System.Net.Mail
Public Class EMail
Public Function ValidEMail(ByVal email As String) As Boolean
'ruwe controle dmv regex of een mail adres wel geldig kan zijn
Dim reg As New Regex("^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}
\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)")
Return reg.IsMatch(email)
End Function
Public Function SendMailMessage(ByVal Recipient As String, ByVal Body As
String, ByVal From As String, Optional ByVal BCC As String = "", Optional
ByVal CC As String = "", Optional ByVal Subject As String = "", Optional
ByVal IsBodyHtml As Boolean = True) As Boolean
Try
Dim mMailMessage As New MailMessage()
mMailMessage.From = New MailAddress(From)
mMailMessage.To.Add(New MailAddress(Recipient))

If Not String.IsNullOrEmpty(BCC) Then
mMailMessage.Bcc.Add(New MailAddress(BCC))
End If

If Not String.IsNullOrEmpty(CC) Then
mMailMessage.CC.Add(New MailAddress(CC))
End If

mMailMessage.Subject = Subject
mMailMessage.Body = Body

mMailMessage.IsBodyHtml = IsBodyHtml
mMailMessage.Priority = MailPriority.Normal

Dim mSmtpClient As New SmtpClient(My.Settings.SMTPHost,
My.Settings.SMTPPort)
mSmtpClient.Send(mMailMessage)
Catch ex As Exception
Return False
End Try
Return True
End Function
End Class


Method that uses Mail class

Private Sub CreateXML()
SortByRadioMeter()
Dim _HTMWORowS As String = String.Empty
Dim sBodyHTML As String = My.Settings.HTMMail
sBodyHTML = sBodyHTML.Replace("#var:gebn#", Me.ProgNaam)
sBodyHTML = sBodyHTML.Replace("#var:start#", Date.Now.ToString)
Dim iCount As Integer = 0
Try
For Each Me._WONr In ParVals
If CancellProcess Then Exit Sub 'wanneer ontvangen dan
stoppen met verwerken
Try
Using clsRep As New clsReport(_WONr, Me.GebruikersNaam,
Me.ProgNaam, Me.PrinterId)
'events binden aan handler methods
AddHandler clsRep.ErrorOccured, AddressOf clsRepError
AddHandler clsRep.eSuccess, AddressOf clsRepeSuccess
'start genereren rapport
clsRep.CreateRep()
iCount += 1
_HTMWORowS = String.Concat(_HTMWORowS,
My.Settings.HTMWoRow.Replace("#var:wo#", _WONr.ToString), Environment.NewLine)
End Using
Catch ex As Exception
MyBase.RaiseError("W.O." & Environment.NewLine &
Environment.NewLine & ex.ToString & Environment.NewLine & " Values :
ObjectNr= " & _WONr.ToString & " User started =" & Me.GebruikersNaam)
End Try
Next
Catch ex As Exception
MyBase.RaiseProcessCancell("W.O. Print " & Environment.NewLine &
ex.ToString & Environment.NewLine & Environment.NewLine & " Values :
ObjectNr= " & _WONr.ToString & " User started =" & Me.GebruikersNaam)
Finally
sBodyHTML = sBodyHTML.Replace("#var:start#", Date.Now.ToString)
sBodyHTML = sBodyHTML.Replace("#var:body#", _HTMWORowS)
Try
Dim email As New ista.EMail
If email.ValidEMail(My.Settings.SendWoMail) Then

email.SendMailMessage(My.Settings.SendWoMail, sBodyHTML,
"(e-mail address removed)", My.Settings.SendWoMailBCC,
My.Settings.SendWoMailCC, String.Concat("W.O. Print ",
Date.Now.ToShortDateString, " aantal=", iCount.ToString), True)
End If
Catch ex As Exception
MyBase.RaiseError(ex.ToString)
End Try
End Try
End Sub


:-(

Michel
 
Michel,

You most probably make Herfried happy.

Try (UrlEncode (From))

etc

:-)

Cor





Michel Posseth said:
This is weird



i have a method :

Public Function SendMailMessage(ByVal Recipient As String, ByVal Body As
String, ByVal From As String, Optional ByVal BCC As String = "", Optional
ByVal CC As String = "", Optional ByVal Subject As String = "", Optional
ByVal IsBodyHtml As Boolean = True) As Boolean
Try
Dim mMailMessage As New MailMessage()
mMailMessage.From = New MailAddress(From)
mMailMessage.To.Add(New MailAddress(Recipient))

If Not String.IsNullOrEmpty(BCC) Then
mMailMessage.Bcc.Add(New MailAddress(BCC))
End If

If Not String.IsNullOrEmpty(CC) Then
mMailMessage.CC.Add(New MailAddress(CC))
End If

mMailMessage.Subject = Subject
mMailMessage.Body = Body

mMailMessage.IsBodyHtml = IsBodyHtml
mMailMessage.Priority = MailPriority.Normal

Dim mSmtpClient As New SmtpClient(My.Settings.SMTPHost,
My.Settings.SMTPPort)
mSmtpClient.Send(mMailMessage)
Catch ex As Exception
Return False
End Try
Return True
End Function

wich works perfect on a local system when sending e-mail in HTML format
however when the method is run in a server process the e-mails that i
receive loose there HTML markup


so short :
Local nice formated html message , remote running on unattended sewrver
process but method in the same dll with all the same settings mail
arrives
as HTML source code

anyone an idea ??? what i am missing

here is the full source of the server process ( wich is started by a
service with reflection )

MAIL
Imports System.Text.RegularExpressions
Imports System.Net.Mail
Public Class EMail
Public Function ValidEMail(ByVal email As String) As Boolean
'ruwe controle dmv regex of een mail adres wel geldig kan zijn
Dim reg As New Regex("^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}
\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)")
Return reg.IsMatch(email)
End Function
Public Function SendMailMessage(ByVal Recipient As String, ByVal Body
As
String, ByVal From As String, Optional ByVal BCC As String = "", Optional
ByVal CC As String = "", Optional ByVal Subject As String = "", Optional
ByVal IsBodyHtml As Boolean = True) As Boolean
Try
Dim mMailMessage As New MailMessage()
mMailMessage.From = New MailAddress(From)
mMailMessage.To.Add(New MailAddress(Recipient))

If Not String.IsNullOrEmpty(BCC) Then
mMailMessage.Bcc.Add(New MailAddress(BCC))
End If

If Not String.IsNullOrEmpty(CC) Then
mMailMessage.CC.Add(New MailAddress(CC))
End If

mMailMessage.Subject = Subject
mMailMessage.Body = Body

mMailMessage.IsBodyHtml = IsBodyHtml
mMailMessage.Priority = MailPriority.Normal

Dim mSmtpClient As New SmtpClient(My.Settings.SMTPHost,
My.Settings.SMTPPort)
mSmtpClient.Send(mMailMessage)
Catch ex As Exception
Return False
End Try
Return True
End Function
End Class


Method that uses Mail class

Private Sub CreateXML()
SortByRadioMeter()
Dim _HTMWORowS As String = String.Empty
Dim sBodyHTML As String = My.Settings.HTMMail
sBodyHTML = sBodyHTML.Replace("#var:gebn#", Me.ProgNaam)
sBodyHTML = sBodyHTML.Replace("#var:start#", Date.Now.ToString)
Dim iCount As Integer = 0
Try
For Each Me._WONr In ParVals
If CancellProcess Then Exit Sub 'wanneer ontvangen dan
stoppen met verwerken
Try
Using clsRep As New clsReport(_WONr, Me.GebruikersNaam,
Me.ProgNaam, Me.PrinterId)
'events binden aan handler methods
AddHandler clsRep.ErrorOccured, AddressOf
clsRepError
AddHandler clsRep.eSuccess, AddressOf
clsRepeSuccess
'start genereren rapport
clsRep.CreateRep()
iCount += 1
_HTMWORowS = String.Concat(_HTMWORowS,
My.Settings.HTMWoRow.Replace("#var:wo#", _WONr.ToString),
Environment.NewLine)
End Using
Catch ex As Exception
MyBase.RaiseError("W.O." & Environment.NewLine &
Environment.NewLine & ex.ToString & Environment.NewLine & " Values :
ObjectNr= " & _WONr.ToString & " User started =" & Me.GebruikersNaam)
End Try
Next
Catch ex As Exception
MyBase.RaiseProcessCancell("W.O. Print " & Environment.NewLine
&
ex.ToString & Environment.NewLine & Environment.NewLine & " Values :
ObjectNr= " & _WONr.ToString & " User started =" & Me.GebruikersNaam)
Finally
sBodyHTML = sBodyHTML.Replace("#var:start#", Date.Now.ToString)
sBodyHTML = sBodyHTML.Replace("#var:body#", _HTMWORowS)
Try
Dim email As New ista.EMail
If email.ValidEMail(My.Settings.SendWoMail) Then

email.SendMailMessage(My.Settings.SendWoMail,
sBodyHTML,
"(e-mail address removed)", My.Settings.SendWoMailBCC,
My.Settings.SendWoMailCC, String.Concat("W.O. Print ",
Date.Now.ToShortDateString, " aantal=", iCount.ToString), True)
End If
Catch ex As Exception
MyBase.RaiseError(ex.ToString)
End Try
End Try
End Sub


:-(

Michel
 
i narrowed it a bit down

Note that remote server is a BLS server ( not a web server ) but a windows
service wich starts dll`s through reflection

If i copy a test project ( exe ) to this server and run in the same user
context as the service is running , everything works as expected , but if
the service invokes the method (through interfacing and reflection ) the
e-mails are send but arrive as HTML source instead of formatted HTML

So the problem must be in the combination Reflection and SMTP class

( for the time beeing i send my mails from this service now as plain text ,
wich works fine )

but i wonder why it wil not work with HTML formatting

Michel








Cor Ligthert said:
Michel,

You most probably make Herfried happy.

Try (UrlEncode (From))

etc

:-)

Cor





Michel Posseth said:
This is weird



i have a method :

Public Function SendMailMessage(ByVal Recipient As String, ByVal Body As
String, ByVal From As String, Optional ByVal BCC As String = "", Optional
ByVal CC As String = "", Optional ByVal Subject As String = "", Optional
ByVal IsBodyHtml As Boolean = True) As Boolean
Try
Dim mMailMessage As New MailMessage()
mMailMessage.From = New MailAddress(From)
mMailMessage.To.Add(New MailAddress(Recipient))

If Not String.IsNullOrEmpty(BCC) Then
mMailMessage.Bcc.Add(New MailAddress(BCC))
End If

If Not String.IsNullOrEmpty(CC) Then
mMailMessage.CC.Add(New MailAddress(CC))
End If

mMailMessage.Subject = Subject
mMailMessage.Body = Body

mMailMessage.IsBodyHtml = IsBodyHtml
mMailMessage.Priority = MailPriority.Normal

Dim mSmtpClient As New SmtpClient(My.Settings.SMTPHost,
My.Settings.SMTPPort)
mSmtpClient.Send(mMailMessage)
Catch ex As Exception
Return False
End Try
Return True
End Function

wich works perfect on a local system when sending e-mail in HTML format
however when the method is run in a server process the e-mails that i
receive loose there HTML markup


so short :
Local nice formated html message , remote running on unattended sewrver
process but method in the same dll with all the same settings mail
arrives
as HTML source code

anyone an idea ??? what i am missing

here is the full source of the server process ( wich is started by a
service with reflection )

MAIL
Imports System.Text.RegularExpressions
Imports System.Net.Mail
Public Class EMail
Public Function ValidEMail(ByVal email As String) As Boolean
'ruwe controle dmv regex of een mail adres wel geldig kan zijn
Dim reg As New Regex("^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}
\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)")
Return reg.IsMatch(email)
End Function
Public Function SendMailMessage(ByVal Recipient As String, ByVal Body
As
String, ByVal From As String, Optional ByVal BCC As String = "", Optional
ByVal CC As String = "", Optional ByVal Subject As String = "", Optional
ByVal IsBodyHtml As Boolean = True) As Boolean
Try
Dim mMailMessage As New MailMessage()
mMailMessage.From = New MailAddress(From)
mMailMessage.To.Add(New MailAddress(Recipient))

If Not String.IsNullOrEmpty(BCC) Then
mMailMessage.Bcc.Add(New MailAddress(BCC))
End If

If Not String.IsNullOrEmpty(CC) Then
mMailMessage.CC.Add(New MailAddress(CC))
End If

mMailMessage.Subject = Subject
mMailMessage.Body = Body

mMailMessage.IsBodyHtml = IsBodyHtml
mMailMessage.Priority = MailPriority.Normal

Dim mSmtpClient As New SmtpClient(My.Settings.SMTPHost,
My.Settings.SMTPPort)
mSmtpClient.Send(mMailMessage)
Catch ex As Exception
Return False
End Try
Return True
End Function
End Class


Method that uses Mail class

Private Sub CreateXML()
SortByRadioMeter()
Dim _HTMWORowS As String = String.Empty
Dim sBodyHTML As String = My.Settings.HTMMail
sBodyHTML = sBodyHTML.Replace("#var:gebn#", Me.ProgNaam)
sBodyHTML = sBodyHTML.Replace("#var:start#", Date.Now.ToString)
Dim iCount As Integer = 0
Try
For Each Me._WONr In ParVals
If CancellProcess Then Exit Sub 'wanneer ontvangen dan
stoppen met verwerken
Try
Using clsRep As New clsReport(_WONr, Me.GebruikersNaam,
Me.ProgNaam, Me.PrinterId)
'events binden aan handler methods
AddHandler clsRep.ErrorOccured, AddressOf
clsRepError
AddHandler clsRep.eSuccess, AddressOf
clsRepeSuccess
'start genereren rapport
clsRep.CreateRep()
iCount += 1
_HTMWORowS = String.Concat(_HTMWORowS,
My.Settings.HTMWoRow.Replace("#var:wo#", _WONr.ToString),
Environment.NewLine)
End Using
Catch ex As Exception
MyBase.RaiseError("W.O." & Environment.NewLine &
Environment.NewLine & ex.ToString & Environment.NewLine & " Values :
ObjectNr= " & _WONr.ToString & " User started =" & Me.GebruikersNaam)
End Try
Next
Catch ex As Exception
MyBase.RaiseProcessCancell("W.O. Print " & Environment.NewLine
&
ex.ToString & Environment.NewLine & Environment.NewLine & " Values :
ObjectNr= " & _WONr.ToString & " User started =" & Me.GebruikersNaam)
Finally
sBodyHTML = sBodyHTML.Replace("#var:start#", Date.Now.ToString)
sBodyHTML = sBodyHTML.Replace("#var:body#", _HTMWORowS)
Try
Dim email As New ista.EMail
If email.ValidEMail(My.Settings.SendWoMail) Then

email.SendMailMessage(My.Settings.SendWoMail,
sBodyHTML,
"(e-mail address removed)", My.Settings.SendWoMailBCC,
My.Settings.SendWoMailCC, String.Concat("W.O. Print ",
Date.Now.ToShortDateString, " aantal=", iCount.ToString), True)
End If
Catch ex As Exception
MyBase.RaiseError(ex.ToString)
End Try
End Try
End Sub


:-(

Michel
 
Michel said:
Note that remote server is a BLS server ( not a web server ) but a
windows service wich starts dll`s through reflection

If i copy a test project ( exe ) to this server and run in the same
user context as the service is running , everything works as
expected , but if the service invokes the method (through
interfacing and reflection ) the e-mails are send but arrive as HTML
source instead of formatted HTML

So the problem must be in the combination Reflection and SMTP class

( for the time beeing i send my mails from this service now as plain
text , wich works fine )

but i wonder why it wil not work with HTML formatting

Can you grab a copy of the email from the maildrop folder before it gets
sent? Maybe comparing that file with one grabbed from a working system will
explain it.

Andrew
 
Andrew Morton said:
Can you grab a copy of the email from the maildrop folder before it gets
sent? Maybe comparing that file with one grabbed from a working system
will explain it.

Andrew

Until sofar i was unsuccesfull as the SMTP server is running somewhere in
Germany and is managed there by our shared services IT and i am in the
Netherlands :-(

But as i said i now know that nothing is wrong with my code , as i can send
HTML formatted mail from anny computer even on that specific server the only
way it doesn`t work in HTML formatted modus is when the e-mail is send from
a dll that is invoked with reflection from our queu service . if i invoke
that same dll and it`s processing method with a button in a test executable
it works fine ( the HTML template is stored in the dll`s config file )


When the e-mail is received outlook says it is HTML formatted I noticed that
the "<" characters become "&lt;" and the ">" character becomes "&gt;" in
the message source in the Outlook view window it shows up like my HTML
template was designed ( just as if you want to display HTML source code
from a HTML page )

I just get frustrated if i can`t explain something like this :-( ( my boss
is already happy with the plain text mails i now send ,, but i just wonder
why it doesn`t work )

Michel
 
"Andrew Morton" <[email protected]> schreef in bericht







Until sofar i was unsuccesfull as the SMTP server is running somewhere in
Germany and is managed there  by our shared services IT   and i am in the
Netherlands :-(

But as i said i now know that nothing is wrong with my code , as i can send
HTML formatted mail from anny computer even on that specific server the only
way it doesn`t work in HTML formatted modus is when the e-mail is send from
a dll that is invoked  with reflection from our queu service  . if i invoke
that same dll and it`s processing method  with a button in a test executable
it works fine ( the HTML template is stored in the dll`s config file )

When the e-mail is received outlook says it is HTML formatted I noticed that
the  "<" characters become "&lt;" and the ">" character becomes "&gt;"  in
the message source in the Outlook view window it shows up like my HTML
template was designed    ( just as if you want to display HTML source code
from a HTML page )

I just get frustrated if i can`t explain something like this :-(   ( my boss
is already happy with the plain text mails i now send ,, but i just wonder
why it doesn`t work  )

Michel- Hide quoted text -

- Show quoted text -

Michel,
Does target machine (server) have trouble receving HTML-based mail
just specific to yours or other clients? If it's common with the
server, it shouldn't be wrong in your code as i agree.

If your situation is urgent, then make your software send the HTML
file (.html or .htm) as attachment using SMTPClient for quick solution
not to lose the HTML markup till you or target machine solve the
problem.

Hope you solve.
 
I was verry bussy with a project so , i just went on with sending the
e-mails in plain text .

However yesterday i had a lightbulb moment :-) checked it and indeed i
found the solution

My html template comes from the settings file , and this is going wrong in
the situation that the dll that sends the e-mail is beeing started through
reflection .

the texts is then beeing encoded

i solved this by letting the dll read the HTML template from a text file

so problem is solved and the cause is now known

HTML stored in the config file`s property icw reflection is a no go ! (
with assemblies started in the normall way this is not a problem )


Regards

Michel
 
Back
Top