Xojo Developer Conference
25/27th April 2018 in Denver.
MBS Xojo Conference
6/7th September 2018 in Munich, Germany.

Port changes after SendMail call - normal SMTPSocket happening? (Real Studio network user group Mailinglist archive)

Back to the thread list
Previous thread: SpecialFolder.SharedDocuments exists, but it's nil?
Next thread: Simpel REALSQLDatabase question


Reading *CSV Files versus *.Txt Files   -   Claude Stone
  Port changes after SendMail call - normal SMTPSocket happening?   -   Tim Jones
   Re: Port changes after SendMail call - normal SMTPSocket happening?   -   Charles Yeomans
   Re: Port changes after SendMail call - normal SMTPSocket happening?   -   Tim Jones
   Re: Port changes after SendMail call - normal SMTPSocket happening?   -   Joe Strout
   Re: Port changes after SendMail call - normal SMTPSocket happening?   -   Charles Yeomans
   Re: Port changes after SendMail call - normal SMTPSocket happening?   -   Joe Strout
   Re: Port changes after SendMail call - normal SMTPSocket happening?   -   Tim Jones
   Re: Port changes after SendMail call - normal SMTPSocket happening?   -   Charles Yeomans
   Re: Port changes after SendMail call - normal SMTPSocket happening?   -   Joe Strout
   Re: Port changes after SendMail call - normal SMTPSocket happening?   -   Tim Jones

Port changes after SendMail call - normal SMTPSocket happening?
Date: 01.08.08 17:57 (Fri, 1 Aug 2008 09:57:16 -0700)
From: Tim Jones
I'm working on a Console app mail helper tool for a couple of projects
and have noticed an odd occurrence with the SMTPSocket - namely, the
port changes after a call to theSocket.SendMail.

Going into the mail effort, the port is set to 25 by default. The
mail is assembled and appended to the SMPTSocket, and then SendMail is
called. The call "appears" to work as no errors are raised, but the
port get reset to some random number. Is this correct operation after
the Send occurs?

Here's a pared down set of code:

Dim theSocket As New SMTPSocket
Dim theMail As New EmailMessage
theSocket.Port = 25
theSocket.Username = theUser
theSocket.Address = theSMTPServer
theSocket.Password = theSMTPPassword
// r is a string() containing the recipients list
theMail.Subject = theSubject
for y = 0 to r.Ubound
theMail.AddRecipient(Trim(r(y)))
next
theMail.BodyPlainText = "Send me some mail, Quick!" +
EndOfLine.Windows
theMail.FromAddress = "<email address removed>"
theMail.Headers.AppendHeader "Content-Type", "text/plain"
theMail.Headers.AppendHeader "Content-Transfer-Encoding", "7bit"
theMail.Headers.AppendHeader "X-Mailer","BRU LE Mail Agent"

theSocket.Messages.Append theMail
theSocket.SendMail

Watching this in the debugger results in the port changing as soon as
the SendMail call returns.

Is that normal? I'm curious as the mail does not seem to be delivered
and I can find nothing in the mail logs on either my local system or
the mail server I'm sending to (internal OS X Server system). As an
aside, similar code in a Desktop app works and doesn't result in the
port changing.

Thanks for any help,
Tim



_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Port changes after SendMail call - normal SMTPSocket happening?
Date: 01.08.08 18:22 (Fri, 1 Aug 2008 13:22:09 -0400)
From: Charles Yeomans

On Aug 1, 2008, at 12:57 PM, Tim Jones wrote:

> I'm working on a Console app mail helper tool for a couple of
> projects and have noticed an odd occurrence with the SMTPSocket -
> namely, the port changes after a call to theSocket.SendMail.
>
> Going into the mail effort, the port is set to 25 by default. The
> mail is assembled and appended to the SMPTSocket, and then SendMail
> is called. The call "appears" to work as no errors are raised, but
> the port get reset to some random number. Is this correct operation
> after the Send occurs?
>
> Here's a pared down set of code:
>
> Dim theSocket As New SMTPSocket
> Dim theMail As New EmailMessage
> theSocket.Port = 25
> theSocket.Username = theUser
> theSocket.Address = theSMTPServer
> theSocket.Password = theSMTPPassword
> // r is a string() containing the recipients list
> theMail.Subject = theSubject
> for y = 0 to r.Ubound
> theMail.AddRecipient(Trim(r(y)))
> next
> theMail.BodyPlainText = "Send me some mail, Quick!" +
> EndOfLine.Windows
> theMail.FromAddress = "<email address removed>"
> theMail.Headers.AppendHeader "Content-Type", "text/plain"
> theMail.Headers.AppendHeader "Content-Transfer-Encoding", "7bit"
> theMail.Headers.AppendHeader "X-Mailer","BRU LE Mail Agent"
>
> theSocket.Messages.Append theMail
> theSocket.SendMail
>
> Watching this in the debugger results in the port changing as soon
> as the SendMail call returns.
>
> Is that normal? I'm curious as the mail does not seem to be
> delivered and I can find nothing in the mail logs on either my local
> system or the mail server I'm sending to (internal OS X Server
> system). As an aside, similar code in a Desktop app works and
> doesn't result in the port changing.

I think what you're seeing the the port value being changed to the
outbound port on the client side. You might be able to verify this
using lsof.

Charles Yeomans

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Port changes after SendMail call - normal SMTPSocket happening?
Date: 01.08.08 22:20 (Fri, 1 Aug 2008 14:20:20 -0700)
From: Tim Jones
On Aug 1, 2008, at 10:22 AM, Charles Yeomans wrote:

> On Aug 1, 2008, at 12:57 PM, Tim Jones wrote:
>
>> Dim theSocket As New SMTPSocket
>> Dim theMail As New EmailMessage
>> theSocket.Port = 25
>> theSocket.Username = theUser
>> theSocket.Address = theSMTPServer
>> theSocket.Password = theSMTPPassword
>> // r is a string() containing the recipients list
>> theMail.Subject = theSubject
>> for y = 0 to r.Ubound
>> theMail.AddRecipient(Trim(r(y)))
>> next
>> theMail.BodyPlainText = "Send me some mail, Quick!" +
>> EndOfLine.Windows
>> theMail.FromAddress = "<email address removed>"
>> theMail.Headers.AppendHeader "Content-Type", "text/plain"
>> theMail.Headers.AppendHeader "Content-Transfer-Encoding", "7bit"
>> theMail.Headers.AppendHeader "X-Mailer","BRU LE Mail Agent"
>>
>> theSocket.Messages.Append theMail
>> theSocket.SendMail
>>
>> I'm curious as the mail does not seem to be delivered and I can
>> find nothing in the mail logs on either my local system or the mail
>> server I'm sending to (internal OS X Server system). As an aside,
>> similar code in a Desktop app works and doesn't result in the port
>> changing.
>
> I think what you're seeing the the port value being changed to the
> outbound port on the client side. You might be able to verify this
> using lsof.

That's correct as the new port is the reassigned port from the spawned
receiving process - and it turns out that the desktop app changes it
as well.

Okay, then I'm now tracking a different issue with that same code -
any thoughts on why this code would work in a Desktop GUI app, but
fail silently in a Console app?

Thanks,
Tim


_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Port changes after SendMail call - normal SMTPSocket happening?
Date: 01.08.08 23:06 (Fri, 1 Aug 2008 16:06:31 -0600)
From: Joe Strout
On Aug 1, 2008, at 3:20 PM, Tim Jones wrote:

> Okay, then I'm now tracking a different issue with that same code -
> any thoughts on why this code would work in a Desktop GUI app, but
> fail silently in a Console app?

No... but just to give you some hope, I successfully send mail with
SMTPSocket from console apps, so it can work.

Best,
- Joe

Re: Port changes after SendMail call - normal SMTPSocket happening?
Date: 01.08.08 23:10 (Fri, 1 Aug 2008 18:10:51 -0400)
From: Charles Yeomans

On Aug 1, 2008, at 5:20 PM, Tim Jones wrote:

> On Aug 1, 2008, at 10:22 AM, Charles Yeomans wrote:
>
>> On Aug 1, 2008, at 12:57 PM, Tim Jones wrote:
>>
>>> Dim theSocket As New SMTPSocket
>>> Dim theMail As New EmailMessage
>>> theSocket.Port = 25
>>> theSocket.Username = theUser
>>> theSocket.Address = theSMTPServer
>>> theSocket.Password = theSMTPPassword
>>> // r is a string() containing the recipients list
>>> theMail.Subject = theSubject
>>> for y = 0 to r.Ubound
>>> theMail.AddRecipient(Trim(r(y)))
>>> next
>>> theMail.BodyPlainText = "Send me some mail, Quick!" +
>>> EndOfLine.Windows
>>> theMail.FromAddress = "<email address removed>"
>>> theMail.Headers.AppendHeader "Content-Type", "text/plain"
>>> theMail.Headers.AppendHeader "Content-Transfer-Encoding", "7bit"
>>> theMail.Headers.AppendHeader "X-Mailer","BRU LE Mail Agent"
>>>
>>> theSocket.Messages.Append theMail
>>> theSocket.SendMail
>>>
>>> I'm curious as the mail does not seem to be delivered and I can
>>> find nothing in the mail logs on either my local system or the
>>> mail server I'm sending to (internal OS X Server system). As an
>>> aside, similar code in a Desktop app works and doesn't result in
>>> the port changing.
>>
>> I think what you're seeing the the port value being changed to the
>> outbound port on the client side. You might be able to verify this
>> using lsof.
>
> That's correct as the new port is the reassigned port from the
> spawned receiving process - and it turns out that the desktop app
> changes it as well.
>
> Okay, then I'm now tracking a different issue with that same code -
> any thoughts on why this code would work in a Desktop GUI app, but
> fail silently in a Console app?

Probably you're not waiting until theSocket sends the mail and closes
the connection.

Charles Yeomans

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Port changes after SendMail call - normal SMTPSocket happening?
Date: 01.08.08 23:27 (Fri, 1 Aug 2008 16:27:04 -0600)
From: Joe Strout
On Aug 1, 2008, at 4:10 PM, Charles Yeomans wrote:

> Probably you're not waiting until theSocket sends the mail and
> closes the connection.

There's a point -- in a console app, you have to make yourself an
event loop using App.DoEvents (finally, we get a chance to put
App.DoEvents to its intended use!). If you don't do that, or at the
very least poll the socket, it won't have a chance to do its work.

Best,
- Joe

Re: Port changes after SendMail call - normal SMTPSocket happening?
Date: 01.08.08 23:31 (Fri, 1 Aug 2008 15:31:22 -0700)
From: Tim Jones
On Aug 1, 2008, at 3:10 PM, Charles Yeomans wrote:

> Probably you're not waiting until theSocket sends the mail and
> closes the connection.

theSocket.Messages.Append theMail
theSocket.SendMail

So should I have some sort of wait loop here? I don't need it in the
UI app...

Tim

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Port changes after SendMail call - normal SMTPSocket happening?
Date: 02.08.08 00:19 (Fri, 1 Aug 2008 19:19:59 -0400)
From: Charles Yeomans

On Aug 1, 2008, at 6:31 PM, Tim Jones wrote:

> On Aug 1, 2008, at 3:10 PM, Charles Yeomans wrote:
>
>> Probably you're not waiting until theSocket sends the mail and
>> closes the connection.
>
> theSocket.Messages.Append theMail
> theSocket.SendMail
>
> So should I have some sort of wait loop here? I don't need it in
> the UI app...
>

That's right, you don't, because GUI apps are already running an event
loop. Recall that REALbasic sockets fiddle with their reference
counts. When you call Connect, the socket's reference count is
incremented, It is decremented when the connection is closed (or
never happens). So if you do something like

dim s as new SMTPSocket
s.SendMail

the socket will live on even after s goes out of scope until it's
finished sending mail. Meanwhile, the GUI app will wait for some
input. Unlike a GUI app, a console app exits when its Run event
handler exits. You prevent that with an event loop. You need to call
App.DoEvents so that the socket is polled and its event handlers are
called.

And there is one more trick. Consider the following code.

dim s as new SMTPSocket
s.SendMail
while s.IsConnected
app.DoEvents
wend

This won't quite work either. s.IsConnected will usually return false
the first time it is evaluated, because the socket has not had time to
connect. To my socket subclasses, I add a property IsConnecting as
Boolean. I override Connect, and set it to true --

Sub Connect()
me.IsConnecting = true
super.Connect
End Sub

and set it back to false both in the Connected and Error event
handlers. Now I can write

dim s as new SMTPSocket
s.SendMail
while s.IsConnecting or s.IsConnected
app.DoEvents
wend

For SMTPSocket, this trick is implemented slightly differently, of
course.

Charles Yeomans

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Port changes after SendMail call - normal SMTPSocket happening?
Date: 02.08.08 02:57 (Fri, 1 Aug 2008 19:57:44 -0600)
From: Joe Strout
On Aug 1, 2008, at 4:31 PM, Tim Jones wrote:

> theSocket.Messages.Append theMail
> theSocket.SendMail
>
> So should I have some sort of wait loop here? I don't need it in
> the UI app...

The UI app has a built-in event loop. Console apps do not. Make one
with App.DoEvents in a loop.

Best,
- Joe

Re: Port changes after SendMail call - normal SMTPSocket happening?
Date: 02.08.08 05:58 (Fri, 1 Aug 2008 21:58:17 -0700)
From: Tim Jones
On Aug 1, 2008, at 6:57 PM, Joe Strout wrote:

> On Aug 1, 2008, at 4:31 PM, Tim Jones wrote:
>
>> theSocket.Messages.Append theMail
>> theSocket.SendMail
>>
>> So should I have some sort of wait loop here? I don't need it in
>> the UI app...
>
> The UI app has a built-in event loop. Console apps do not. Make
> one with App.DoEvents in a loop.

With your and Charles' pointers, I now have things working.

Thanks.

Tim

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>