My Gmail blocks my Oculus device from accessing it to send out emails.

MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
            SmtpServer.Timeout = 10000;
            SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
            SmtpServer.UseDefaultCredentials = false;
            SmtpServer.Port = 587;

            mail.From = new MailAddress("SenderEmail@gmail.com");
            mail.To.Add(new MailAddress("RecieverEmail@gmail.com"));

            mail.Subject = "Report";
            mail.Body = "This is a report.";

            System.Net.Mail.Attachment att;
            System.Net.Mail.Attachment att2; 
            System.Net.Mail.Attachment att3;
           

            att = new Attachment(GameManager.CSVFile1.GetFilePath());
            att2 = new Attachment(GameManager.CSVFile2.GetFilePath()); 
            att3 = new Attachment(GameManager.CSVFile3.GetFilePath()); 
            mail.Attachments.Add(att);
            mail.Attachments.Add(att2);
            mail.Attachments.Add(att3);


            SmtpServer.Credentials = new System.Net.NetworkCredential("SenderEmail@gmail.com", "EmailPassword") as ICredentialsByHost;
            SmtpServer.EnableSsl = true;
            ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
            {
                return true;
            };

            mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
            SmtpServer.Send(mail);

Hello,

I have been developing an application for the Oculus Quest 2 for a while now and I was using the code shown here to to send out emails with CSV files as attachments.

Previously during testing I had no issues with my VR app login into my gmail to send out these emails. However, this was all done when my Oculus device was connected to the same network as my work computer (which I use for development).

Recently I tried to use this function remotely, as in away from my work computer with my oculus being connected to another network. This time I ran into an issue were my game is stuck on the scene were the email would be sent, and I get a notification on my gmail that a suspicious login attempt has been blocked (the code in my game does not proceed because it cannot access my email).

The “Less secure app access” option has been enabled on my gmail during this test and the “clear cache” suggestion is not an option given that the user needs to send emails freely.

If anyone has any suggestions on how I can get around this it would be much appreciated. Also, I am currently looking into using a server to save CSVs and send out emails that way but I am not sure if this is the way to go.

Thank you so much for your time.

You’ll want to reach out to Oculus Developer Support about this one: https://developer.oculus.com/support/

Did you ever solve this?

Hey,

Yes I did, I had to use a third party software called “SendGrid”, which is essentially a server that handles the distribution of emails. They have a free subscription deal and they have a guide on how to integrate their service into any Unity project (https://sendgrid.com/blog/send-email-unity-game/).

Hopefully this can help anyone else with my previous issue :).

1 Like

Hi, how did you manage to get it to work? I’m using the sample CS script in the blog post but I keep getting this error message in Unity:

SmtpException: 535 Authentication failed: Bad username / password

I don’t know what I’m doing wrong, I already verified my sender email, created an API key and set up 2FA, here’s the full log if that helps:

SmtpException: 535 Authentication failed: Bad username / password

System.Net.Mail.SmtpClient.CheckStatus (System.Net.Mail.SmtpClient+SmtpResponse status, System.Int32 i) (at :0)
System.Net.Mail.SmtpClient.Authenticate (System.String user, System.String password) (at :0)
System.Net.Mail.SmtpClient.Authenticate () (at :0)
System.Net.Mail.SmtpClient.SendCore (System.Net.Mail.MailMessage message) (at :0)
System.Net.Mail.SmtpClient.SendInternal (System.Net.Mail.MailMessage message) (at :0)
System.Net.Mail.SmtpClient.Send (System.Net.Mail.MailMessage message) (at :0)
SendGridMailManager.SendEmail () (at Assets/Scripts/SendGridMailManager.cs:32)
UnityEngine.Events.InvokableCall.Invoke () (at <9baebf9af86541678fd15bfdbf5f26eb>:0)
UnityEngine.Events.UnityEvent.Invoke () (at <9baebf9af86541678fd15bfdbf5f26eb>:0)
UnityEngine.UI.Button.Press () (at D:/Program Files/Unity/2020.3.5f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:68)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at D:/Program Files/Unity/2020.3.5f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:110)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at D:/Program Files/Unity/2020.3.5f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at D:/Program Files/Unity/2020.3.5f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:262)
UnityEngine.EventSystems.EventSystem:Update() (at D:/Program Files/Unity/2020.3.5f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:385)

Edit:

I already figured it out, I’ll post the solution to my problem in case someone else needs it:

smtpServer.Credentials = new System.Net.NetworkCredential(api_user, api_key) as ICredentialsByHost;

In this line I changed the value of api_user to the string ‘apikey’ and the value of api_key to the API key you get when you create a new one and SendGrid tells you to store it somewhere safe (you won’t be able to see it again after you create it). In the end it looked like this:

smtpServer.Credentials = new System.Net.NetworkCredential("apikey", "ThisIsWhereYouPasteYourAPIKey") as ICredentialsByHost;