Android Question SMTP.MailFrom not working

Discussion in 'Android Questions' started by JohnC, Apr 19, 2019.

  1. JohnC

    JohnC Well-Known Member Licensed User

    I'm using NET lib 1.80 with B4a 9.00.

    I have the below code:

    SMTP.HtmlBody = False
    SMTP.Body = EmailBody
    SMTP.Subject = EmailSubject
    SMTP.MailFrom = ""
    The email is sent OK, but the "From:" is the username "" and not the .MailFrom = "" that I set it to.
  2. Erel

    Erel Administrator Staff Member Licensed User

    Moved to the questions forum.
  3. Erel

    Erel Administrator Staff Member Licensed User

    The MAIL FROM command is sent correctly. Many mail servers ignore it and use your username to prevent mail spoofing.
  4. JohnC

    JohnC Well-Known Member Licensed 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 site) and specify "" and "John" as the "user info". And in this same account I setup the SMTP account with the username of "" and it's password. And when I send an email using this account, it comes up as coming from "John (" and NOT the smtp login username of "".

    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 = "" and when it sends an email, it will show "" 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: Apr 20, 2019
  5. JohnC

    JohnC Well-Known Member Licensed 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
    cmd: MAIL FROM:<>
    rsp: 250 OK <> 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
    250 OK

    cmd: MAIL FROM: <>
    rsp: 250 OK <> 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
    cmd: MAIL FROM: <>
    rsp: 250 OK <> 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 "<>", 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: Apr 22, 2019
  6. JohnC

    JohnC Well-Known Member Licensed 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 <>

    Whereas the B4A email header has:

    From: <

    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.

    Last edited: Apr 22, 2019
  7. Erel

    Erel Administrator Staff Member Licensed User

    You can add additional headers with the AdditionalHeaders map.

    I don't think that the space is a real issue. The command is formatted by the underlying Apache Commons Net library which does follow the formal specifications.
  8. JohnC

    JohnC Well-Known Member Licensed User

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

    Dim M As Map
    SMTP.AdditionalHeaders = M
    The From: address in emails now properly reflect "" 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):

    M.Put("From","John <>")
    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: Apr 22, 2019
  9. JohnC

    JohnC Well-Known Member Licensed User


    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:

    Wed, 1 May 2019 21:31:39 -0700
    From: <>  <-------------this 
    is the header my code adds using .AdditionalHeaders
    Type: multipart/mixed; boundary="asdasdwdwqd__HV_qwdqwdddwq"
    From:  <--------- 
    is your SMTP adding this header? and if so, it might be not be providing the .MAILFROM value correctly
    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: May 2, 2019
  10. Erel

    Erel Administrator Staff Member Licensed User

    Please send a message without doing anything special except of setting MailFrom and post the captured network traffic.
  11. JohnC

    JohnC Well-Known Member Licensed 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:

    I am using the MailFrom command

    SMTP.MailFrom = ""
    And here is the captured data using Microsoft Network Monitor:

    Smtp: Data Payload, 1460 bytes
      + Data: Content-
    Type: multipart/mixed; boundary="asdasdwdwqd__HV_qwdqwdddwq"
      + Data: From: thisisusername
      + Data: 
      + 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.

    Last edited: May 2, 2019
  12. Erel

    Erel Administrator Staff Member Licensed User

    I've tested it with this code:
    Dim s As SMTP
    s.StartTLSMode = 
    s.Sender = 
    "" 'support2 vs support.
    s.Subject = "this is a test"
    Wait For smtp_MessageSent(Success As Boolean)
    The message did arrive from I've examined the raw headers and they look like this:
    Content-Type: text/plain; charset=us-ascii
    Content-Transfer-Encoding: quoted-printable
    Subject: this 
    is a test
    There was a single From header.
  13. JohnC

    JohnC Well-Known Member Licensed 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 :)
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice