Android Question SMTP.MailFrom not working

JohnC

Expert
Licensed User
Longtime User
I'm using NET lib 1.80 with B4a 9.00.

I have the below code:

B4X:
SMTP.Initialize("mail.mydomain.com",222,"[email protected]","123Pass","SMTP")
SMTP.To.Add("[email protected]")
SMTP.HtmlBody = False
SMTP.Body = EmailBody
SMTP.Subject = EmailSubject
SMTP.MailFrom = "[email protected]"
SMTP.AuthMethod = SMTP.AUTH_LOGIN
SMTP.Send

The email is sent OK, but the "From:" is the username "[email protected]" and not the .MailFrom = "[email protected]" that I set it to.
 

JohnC

Expert
Licensed User
Longtime User
Hi Erel,

I just tested this on two different email servers using two different apps.

Server 1 = SMTP2GO (smtp paid service)
Server 2 = a VPS on Godaddy where I installed my own "smartermail" email server on the VPS to manage all my domains's emails.

I can setup a new account in my Windows Microsoft Outlook 2003 app (not outlook.com site) and specify "[email protected]" and "John" as the "user info". And in this same account I setup the SMTP account with the username of "[email protected]" and it's password. And when I send an email using this account, it comes up as coming from "John ([email protected])" and NOT the smtp login username of "[email protected]".

I also created a VB6 app that uses nsoftware's SMTP ActiveX control and configured the SMTP control with the SMTP2GO account info (which has a totally different username) and set the control's SMTP.From = "[email protected]" and when it sends an email, it will show "[email protected]" as the "From" address (and not the SMTP2Go login username).

But, when I use the same account info for either server with the B4A NET lib, the "From" will always be the login username and not the value for SMTP.MailFrom that I assigned.

So, I don't know what is going on with B4A's NET lib, but as you can see both of the SMTP servers I tested this with will accept a different "From" then the username.
 
Last edited:
Upvote 0

JohnC

Expert
Licensed User
Longtime User
OK, I did a comparison between the SMTP commands that the B4A NET lib sends (where the MailFrom does not work) and what two other apps send (where MailFrom works).

The B4A NET lib has this relevant sequence of events (MailFrom does NOT work):

rsp: 235 Authentication successful
Authenticated as [email protected]
cmd: MAIL FROM:<[email protected]>
rsp: 250 OK <[email protected]> Sender ok​

My default Samsung Email app on my S4 has this sequence of events in the log (MailFrom works):

rsp: 235 Authentication successful
Authenticated as [email protected]
RSET
250 OK

cmd: MAIL FROM: <[email protected]>
rsp: 250 OK <[email protected]> Sender ok​

You will notice that my S4 client sends the two lines in bold that the B4A NET lib doesn't.

These are the commands that my outlook 2003 sends (MailFrom works):

rsp: 235 Authentication successful
Authenticated as [email protected]
cmd: MAIL FROM: <[email protected]>
rsp: 250 OK <[email protected]> Sender ok​

At first glace this looks identical to what the B4A NET lib outputs, even to the extent that it also does not send the RSET command that the S4 email app does.

But there is one slight difference common between Outlook and my S4 app and what the B4A NET lib does NOT do...

Outlook and S4 will insert a space between "MAIL FROM:" and "<[email protected]>", whereas the B4A lib does not.

I know that adding a space should not make a difference. But, I did spend a lot of time analyzing this as you requested, and that is the only difference I can see, so I think it's at least worth trying.

However, I can not make the modification to the NET lib to test to see if adding a space will fix the problem, so it would be greatly appreciate, Erel, if you could send me a test NET lib that will insert a space between the MAIL FROM: and the <requested from address> so that I can see if this fixes my issue.
 
Last edited:
Upvote 0

JohnC

Expert
Licensed User
Longtime User
I just thought of something that would make much more sense as the cause to my issue instead of the missing space issue I explained above...

I just looked inside the headers of an email that had the desired "From:" (MS Outlook) and then inside the one that the B4A NET lib sent that did not have the proper from: address.

I noticed that in the outlook header it has this line in it:

From: John <[email protected]>

Whereas the B4A email header has:

From: <[email protected])

So, I said to myself, how did the name "John" get into the header of the email that had the proper from address? It just so happens that "John" is the name I provided in outlook's "User Info" setting when setting up the email account in it. And since I did not see "John" sent in the "Mail From:" command in the above protocol event logs, then that means "John" was sent in the DATA packet that outlook later sent to my SMTP server.

So, my thought now is that the "Mail From:" command is being properly executed, but that this command maybe more of a security check and is not the only thing that controls what the "From:" address will be in the header.

My new thinking is that the B4A NET lib needs to properly modify/insert the desired "From:" address in the header before it sends it to the SMTP server. This would explain how "John" is contained in the "From:" line inside the header - because Outlook place "John" in the "From:" line inside the header before it sent the email to my SMTP server.

Maybe the B4A NET lib needs to both send the "Mail From:" command to the SMTP as well as properly insert/provide the same desired "From:" line into the header before sending it to the SMTP server?

Just a thought.

-John
 
Last edited:
Upvote 0

JohnC

Expert
Licensed User
Longtime User
I was right! The problem was with the headers sent by B4A's NET Lib. When I do the following:

B4X:
Dim M As Map
M.Initialize
M.Put("From","<[email protected]>")
SMTP.AdditionalHeaders = M

The From: address in emails now properly reflect "[email protected]" and not the smtp login username.

And you can even add in a persons name to the value as shown below, that will be displayed in emails clients (instead of the from address):

B4X:
M.Put("From","John <[email protected]>")

So, it would be helpful if the NET lib could automatically add this header when a value is provided for the SMTP.MailFrom property because it appears this is also needed for the MailFrom to work properly, so we wont have to add it manually.
 
Last edited:
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Erel,

It looks like there is still a problem with the SMTP component.

I received a bounce back email from Gmail of an email I sent using this SMTP and it said it was rejected due to "multiple FROM: Addresses", and sure enough I found this in the header:

B4X:
   Wed, 1 May 2019 21:31:39 -0700
From: <[email protected]>  <-------------this is the header my code adds using .AdditionalHeaders
Content-Type: multipart/mixed; boundary="asdasdwdwqd__HV_qwdqwdddwq"
From: [email protected]  <--------- is your SMTP adding this header? and if so, it might be not be providing the .MAILFROM value correctly
To: [email protected]
Subject: Test Message

So, is your SMTP inserting the second "From:" header above in the email before it submits it to the SMTP server? If it is, then it may be supplying the "username" value for that "From:" header and not providing the value that my code assigned to the "SMTP.MailFrom" property. And this could be the original cause of my issue (and I won't need to do the .AdditionalHeaders anymore.)
 
Last edited:
Upvote 0

JohnC

Expert
Licensed User
Longtime User
OK, I removed the .AdditionalHeaders line

My SMTP server requires an SSL connection and I could not use it's non-SSL Port 25 because my ISP blocks port 25. So, in order to see the network traffic in plain text, I had to instead use my ISP's SMTP server which does NOT require a username and password to send emails, but I still provide a username in the below init line as a test to prove that the problem is in the B4A NET lib:

B4X:
SMTP.Initialize("mail.myISP.net",25,"thisisusername","","SMTP")

I am using the MailFrom command

B4X:
SMTP.MailFrom = "[email protected]"

And here is the captured data using Microsoft Network Monitor:

B4X:
- Smtp: Data Payload, 1460 bytes
  + Data: Content-Type: multipart/mixed; boundary="asdasdwdwqd__HV_qwdqwdddwq"
  + Data: From: thisisusername
  + Data: To: [email protected]
  + Data: Subject: test Message

As you can see, the NET lib does in fact send a "FROM:" header to the SMTP server, and it seems like the value of the From address is hardcoded to ALWAYS provide the login username even if a different value is provided for the .MAILFROM property.

So, it seems the fix all along is for the NET library to first check and see if a value was provided for the .MAILFROM property, and if so, then send that value in the FROM: header, but if there was no value provided for the MAILFROM, then send the auth/login USERNAME value in the FROM: header.

It would be greatly appreciated if you could fix the NET lib to correct this issue.

Thanks
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I've tested it with this code:
B4X:
Dim s As SMTP
s.Initialize("...", 587, "[email protected]", "...", "smtp")
s.StartTLSMode = True
s.Sender = "[email protected]" 'support2 vs support.
s.Subject = "this is a test"
s.To.Add("[email protected]")
s.Send
Wait For smtp_MessageSent(Success As Boolean)
Log(Success)

The message did arrive from [email protected]. I've examined the raw headers and they look like this:
B4X:
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable
From: [email protected]
Subject: this is a test

There was a single From header.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
I guess we both missed that I did use the .MailFrom, but I did not use the .Sender in the OP code, and that's what made it work :)
 
Upvote 0
Top