0

I am sending a mail From my App. When I tried the code below, it sends the mail from unity Editor. But when I build and tested my app on an ipad, the mail is not sent from my App.

public void Start() {

    using (var mail = new MailMessage {
        From = new MailAddress(sender),
        Subject = "test subject",
        Body = "Hello there!"}) 
    {

        mail.To.Add(receiver);

        var smtpServer = new SmtpClient(smtpHost) {
            Port = 25,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            EnableSsl = true,
            UseDefaultCredentials = false,

            Credentials = (ICredentialsByHost) new NetworkCredential(sender, smtpPassword)
        };
        ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
        smtpServer.Send(mail);
    }
}

It works only when using the Editor. Not in my App (iPad).

What's wrong in my code? I have also enabled the "Allow secure less apps" in my Gmail account.

Draken
  • 3,134
  • 13
  • 34
  • 54
karthik keyan
  • 428
  • 4
  • 14
  • Which line is causing the problem? Put some Debug.Log() statements in your code and share the log here. Also, just in case you didn't know, putting your username and password in mobile application is almost guaranteed to yield your mail server to spammers. – Nika Kasradze Nov 09 '18 at 09:06
  • Actually mail is sending from Unity Editor. When i build and test in ipad only. The mail is not sending to receipent. – karthik keyan Nov 09 '18 at 09:35

2 Answers2

0

Change the smtpServer.Send() method to try/catch block so that you can see want goes wrong:

public void Start() {

    using (var mail = new MailMessage {
        From = new MailAddress(sender),
        Subject = "test subject",
        Body = "Hello there!"}) 
    {

        mail.To.Add(receiver);

        var smtpServer = new SmtpClient(smtpHost) {
            Port = 25,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            EnableSsl = true,
            UseDefaultCredentials = false,

            Credentials = (ICredentialsByHost) new NetworkCredential(sender, smtpPassword)
        };
        ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
        try {
            smtpServer.Send(mail);
        }  
        catch (Exception ex) {
            Console.WriteLine("Exception while smtpServer.Send(mail): {0}", 
                      ex.ToString() );            
        }    
    }
}

Build. Start viewing logs in xCode in real-time to see what's been logged (see this answer). Run your app and see the logs in xCode. Most probably you will understand the problem yourself. If not, post the logs for us.

Nika Kasradze
  • 2,834
  • 3
  • 25
  • 48
0

I have found a solutions for the above code.. We have to change the Build Settings

Go to File --> Build Settings... --> Select Android. Now, click on Player Settings.

1.On the Internet Access, change it from Auto to Require.

2.Make sure that API Compatible Level is set to .NET 2.0 not .NET 2.0 Subset.

3.Make sure that Stripping Level is set to Disabled.

4.Go to Player settings --> Android and change Internet Access from Auto to Require

These are the things to try if SmtpClient is not working on ios

create a link.xml file and add the following code....

and execute the code... It will send the mail from unity App....

karthik keyan
  • 428
  • 4
  • 14
  • Your answer is basically copy and paste of one of my posts.... – Programmer Nov 09 '18 at 14:51
  • @programmer yes it is your code only. Actually i have added for ios . Some one deleted it. Actually if some one look for ios and android... The post may be helpful.I have searched for many hours for android and ios separately.. The above answer credit(android) goes to the you only.... – karthik keyan Nov 10 '18 at 05:11