cancel
Showing results for 
Search instead for 
Did you mean: 

Email sender problem unity quest 2

AR-Initiative
Honored Guest

Hello everyone, I'm having problems when sending a report by email from a medical app I'm developing for oculus quest 2, I have made the following code, it works perfectly sending the email from the editor, but when compiling the apk and want to send the email nothing happens, it does not send anything by mail.

As additional information I can add that the gmail account has two-factor verification enabled, has the option of less secure applications enabled, and a special password for third-party applications created in the security sector for the app in question.
I repeat the app sends perfectly while running with questlink from the editor but does not send it when the apk is compiled and installed in the viewer.

Does anyone have any idea what is happening or how to solve it?

Thank you very much

My Code

 

Spoiler

using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

public class EmailSender : MonoBehaviour
{
public string senderEmail = "miemail@gmail.com";
public string senderPassword = "mipass mipass mipass mipass";
public string recipient1 = "correo1@example.com";
public string recipient2 = "correo2@example.com";
public string subject = "Informe adjunto";
public string body = "Adjunto encontrará el informe.";
public TextMeshProUGUI debugText;

public void SendEmailWithAttachment()
{
string pdfPath = Path.Combine(Application.persistentDataPath, "informe");
DirectoryInfo directoryInfo = new DirectoryInfo(pdfPath);
FileInfo[] files = directoryInfo.GetFiles("*.pdf");

if (files.Length > 0)
{
string filePath = files[0].FullName;
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(senderEmail);
mail.To.Add(recipient1);
mail.To.Add(recipient2);
mail.Subject = subject;
mail.Body = body;

Attachment attachment = new Attachment(filePath);
mail.Attachments.Add(attachment);

SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
smtpServer.Port = 587;
smtpServer.EnableSsl = true;
smtpServer.Credentials = new NetworkCredential(senderEmail, senderPassword) as ICredentialsByHost;

ServicePointManager.ServerCertificateValidationCallback =
delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{ return true; };

try
{
smtpServer.Send(mail);
Debug.Log("Email enviado con éxito.");
SetDebugText("Email enviado con éxito.");
}
catch (Exception ex)
{
Debug.LogError("Error al enviar el email: " + ex.Message);
SetDebugText("Error al enviar el email: " + ex.Message);
}
}
}
else
{
Debug.LogError("No se encontraron archivos PDF en la carpeta 'informe'.");
SetDebugText("No se encontraron archivos PDF en la carpeta 'informe'.");
}
}

private void SetDebugText(string message)
{
if (debugText != null)
{
debugText.text = message;
}
}
}

 

0 REPLIES 0