我目前正在开发Unity(适用于Android平板电脑)并遇到了一个无法解决问题的问题。当我的应用程序关闭时,我想通过电子邮件将某个日志文件发送到预定义的电子邮件地址。现在,一切都很好。这是我到目前为止的工作代码。
private void sendEmail()
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress("from@gmail.com");
mail.To.Add("to@gmail.com");
mail.Subject = "Subject";
mail.Body = "O tempora, o mores!";
System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(Application.persistentDataPath + "test.csv");
mail.Attachments.Add(attachment);
SmtpClient smtpServer = new SmtpClient();
smtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpServer.Host = "smtp.gmail.com";
smtpServer.Port = 587;
smtpServer.Credentials = new System.Net.NetworkCredential("test@gmail.com", "password") as ICredentialsByHost;
smtpServer.EnableSsl = true;
ServicePointManager.ServerCertificateValidationCallback = delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
};
// Send the e-mail asynchronously so the main thread wont hang when there's no internet available
// The userState can be any object that allows your callback
// method to identify this send operation.
String userState = mail.Subject;
smtpServer.SendAsync(mail, userState);
}
现在是困难的部分。因为这在关闭时执行,所以应用程序依赖于始终存在与Internet的连接这一事实。如果由于某个原因在执行时没有互联网连接,则不会发送带有日志的电子邮件。所以现在我的问题是:是否有某种方式让我在没有互联网可用的情况下排队我的电子邮件,并且一旦建立了互联网连接就会发送它们?但请记住,此时应用程序已经关闭。我想我需要一些像解决方案这样的服务(背景),但我似乎无法找到任何关于此的内容,而我只是学习在Unity和C#中编写代码。任何帮助表示赞赏!