Android Question Send EMail with SMTP and net.lib

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi to all
I need to send mails with B4A, possibly without involving the EMail apps on my phone/Tablet. By the way, sending mails with the installed gMail App of course works on the tablet I am using. As a first step, I try to use not secure protocol, which is a further step. I have a WebService in asp.net which works and sends mails, using a server. Therefore I use same parameters in the attached project, but B4A doesn't send any mail.
Thanks for any hint on possible problems.
 

Attachments

  • TestSendMail.zip
    9.4 KB · Views: 212

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
And the error is:
org.apache.commons.net.smtp.SMTPConnectionClosedException: Connection closed without indication.
rather useful indication ..
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi. Thanks. I will try to use B4XPages and see what happens. Anyway, as I said, I have same parameters, port number included, in a working C# Asp.Net webservice. As a matter of fact, having before tried the B4A examples found here, with no success, I wanted to start from something working. Here, the examples are all relative to smtp and gmail, and they don't work in my case, despite in the tablet the Google App sending mail works. Therefore I tried to use a smtp of another provider which allows a not secure protocol (besides the secure one). Of course I tried with all the numbers that I saw anywhere, like 465 or 567 (if I remember well). No success in any case. In StackOverflow I found somebody talking about using another password for google account, for a double security check., but the procedure indicated there seems obsolete, if I don't miss something. It should be nice to have a working example here, to avoid useless discussions on perhaprs obsolete procedures. Thanks for your attention and forgive me. B4A helped me a lot in my work. This is one of the very few situations where I don't see any solution.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Ok. I will try with Gmail, if they allow not secure connection. I just attach here the c# code working on the server. It may be that in the server it works for some reason. To be sincere, I tried also to put same code in a WindowsForm program in my laptop, and it doesn't work. May be it is an issue related to my laptop in he case of the C# program, but B4a App is running on the tablet..
B4X:
        [WebMethod]
        public bool SpedisciMail(string From,string To, string Oggetto, string Contenuto)
        {
            MailMessage Msg = new MailMessage();
            SmtpClient Smtp = new SmtpClient("smtp.xxxx.it",25);

            try
            {

                Msg.From = new MailAddress(From);
                Msg.To.Add(new MailAddress(To));
                Msg.Subject = Oggetto;
                Msg.Body = Contenuto;
                Msg.IsBodyHtml = true;
                Smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                
                NetworkCredential Credential = new NetworkCredential("myMail@mySite.it","MyPassword");
                Smtp.UseDefaultCredentials = false;
                Smtp.Credentials = Credential;

                //Spediamo la mail
                Smtp.Send(Msg);
        }
            catch (Exception E)
            {
                return false;
            }
            return true;
        }

Thanks again.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Errrr - perhaps I'm misunderstanding what you're trying to do here, but if you want to send an email without user interaction I'm pretty sure you can't (for obvious reasons).

- Colin.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi. Thanks for your observation, that, at this point, may be the truth .. For my actual needs, having not the time to look in Google parameters, I will do the following:
1) Send data to my server with OkHttp.lib that works.
2) Call my web service from B4A to send the mail.
Or, use the FileProvider example found here, which calls Gmail app, that, of course works... It is strange that it works, thinking well, if the account that I am using is not authorized. But, of course, I am missing something. It is not excluded that, due to the plenty of hackers, they decided to further restrict the use of smtp, in the way I want. Anyway, also my webservice, with secure parameters, at the moment, is not working with another provider. So, probably, the end, things will be as you say.
 
Upvote 0

Filippo

Expert
Licensed User
Longtime User
You need to have Google generate an app password.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
If you're planning on releasing the app on Google Play, I'll be very surprised if it gets through review. There are very good reasons for not allowing apps to send emails without user intervention & I'm pretty sure app review will be looking for apps trying to circumvent that inherent security. Apart from that, I'd guess users will be very reluctant to use an app that is capable of sending emails without their knowledge.

- Colin.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
They have good reasons, of course. I am not planning to release anything. Just working for the needs of my company.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
You need to have Google generate an app password.
View attachment 139204
I think that no programmer in the world doesn't know english. Nevertheless many don't know german, like me. You did your best, of course, and probably this is the solution. I have found in StackOverflow something like what you say. But the links I found there sent to parts of google configuration that were not compliant with the discussion. Probably obsolete. So, I am not optimist to find what Erel defined "something like that" , and probably was meaning what you say... Thanks a lot.
 
Upvote 0

Filippo

Expert
Licensed User
Longtime User
I think that no programmer in the world doesn't know english.
It's not quite true, I don't know English, for example.

So, I am not optimist to find what Erel defined "something like that" , and probably was meaning what you say... Thanks a lot.
To generate an app password you need to follow these steps:

I have been using such an app password for several years in one of my Windows applications, which I use to send e-mails to my customers.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
I already gave up (sorry if you don't uderstand english). I follow your link and relative instructions, but options available are not those described in the instructions. Probably the company acount that I am using has some other limitation, etc. Thank a lot, anyway. I hope that this discussion helps somebody else , to compensate your kindness.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
And, just to close my question, I must point out that it is not a problem of gmail smtp, but also of another provider on which the not secure method works. GMail rules may be responsible of the failure, but it remains the problem that on the other provider i use, whose rules are much more accessible and clear, the code analogous to the working c# web service one, that I posted here, doesn't work too. This provider rules clearly indicate a port number 25 for not secure connection, and the web service works (didn't try the secure connection). It could be an issue related to my tablet Android authorizations. As a matter of fact Filippo talks about a Windows program sending mails, not about a B4A with net.lib App. By the way another user, rleiman, about two years ago, had similar problem (and also similar issues on gmail authorizations). That thread had no successfull conclusion too in what I see.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…