Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / March 2008

Tip: Looking for answers? Try searching our database.

strange problem with smtpmail

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Michel Posseth [MCP] - 12 Mar 2008 09:19 GMT
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,
"NePros@istanederland.nl", 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
Cor Ligthert [MVP] - 12 Mar 2008 13:33 GMT
Michel,

You most probably make Herfried happy.

Try (UrlEncode (From))

etc

:-)

Cor

> This is weird
>
[quoted text clipped - 152 lines]
>
> Michel
Michel Posseth [MCP] - 12 Mar 2008 13:49 GMT
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

> Michel,
>
[quoted text clipped - 164 lines]
> >
> > Michel
Andrew Morton - 12 Mar 2008 17:51 GMT
> Note that remote server is a BLS server ( not a web server ) but a
> windows service wich starts dll`s through reflection
[quoted text clipped - 11 lines]
>
> 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
Michel Posseth  [MCP] - 12 Mar 2008 20:40 GMT
>> Note that remote server is a BLS server ( not a web server ) but a
>> windows service wich starts dll`s through reflection
[quoted text clipped - 17 lines]
>
> 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
kimiraikkonen - 12 Mar 2008 20:59 GMT
> >> Note that remote server is a BLS server ( not a web server ) but a
> >> windows service wich starts dll`s through reflection
[quoted text clipped - 42 lines]
>
> - 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.
Michel Posseth [MCP] - 27 Mar 2008 11:08 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.