using System;
using System.Collections;
using System.Net;
using System.Net.Mail;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System.IO;
using System.Linq;
using DG.Tweening;
public class EmailUtility : MonoBehaviour
{
public string sender = "no-reply@briteminds.ae";
public string smtpPassword = "9O-(~![d-fjM";
public string smtpHost = "mail.briteminds.ae";
public TMP_InputField receiverInputField;
public string attachmentFolderPath;
public GameObject emailPanel;
public GameObject emailSentPanel;
private void Start()
{
if (receiverInputField == null)
{
Debug.LogError("Receiver input field is not assigned in the inspector!");
return;
}
}
public void SendEmail()
{
string receiver = receiverInputField.text;
var mail = CreateMailMessage(receiver);
StartCoroutine(SendEmailAsync(mail));
}
private MailMessage CreateMailMessage(string receiver)
{
var mail = new MailMessage
{
From = new MailAddress(sender),
Subject = "Your Picture!",
Body = "Please find your picture attached by BriteMinds."
};
mail.To.Add(receiver);
string[] pngFiles = Directory.GetFiles(attachmentFolderPath, "*.png");
string latestFile = pngFiles.OrderByDescending(f => File.GetLastWriteTime(f)).FirstOrDefault();
if (!string.IsNullOrEmpty(latestFile))
{
mail.Attachments.Add(new Attachment(latestFile));
}
return mail;
}
private IEnumerator SendEmailAsync(MailMessage mail)
{
var smtpServer = new SmtpClient(smtpHost)
{
Port = 587,
Credentials = new NetworkCredential(sender, smtpPassword)
};
ServicePointManager.ServerCertificateValidationCallback = (s, certificate, chain, sslPolicyErrors) => true;
yield return null;
try
{
smtpServer.Send(mail);
}
catch (Exception e)
{
Debug.LogError("Error sending email: " + e.Message);
}
finally
{
// Clean up and update UI here if needed
}
emailPanel.GetComponent<CanvasGroup>().DOFade(0, 1);
yield return new WaitForSeconds(1);
emailPanel.SetActive(false);
emailSentPanel.SetActive(true);
emailSentPanel.GetComponent<CanvasGroup>().alpha = 0;
emailSentPanel.GetComponent<CanvasGroup>().DOFade(1, 1);
}
}