2

我需要在 Unity3D 中使用 C# 发送电子邮件。我已经这样做了,但是遇到了运行时警告。请帮我解决以下警告。

警告:在名为“Program”的脚本文件中定义的类不是从 MonoBehaviour 或 ScriptableObject 派生的

我的代码是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;

namespace EMail
 {
  class Program 
   {
     static void Main(string[] args)
      {
       try
       {
        MailMessage mail = new MailMessage();
        SmtpClient smtpC = new SmtpClient("smtp.gmail.com");
        //From address to send email
        mail.From = new MailAddress("From@gmail.com");
        //To address to send email
        mail.To.Add("To@gmail.com");
        mail.Body = "This is a test mail from C# program";
        mail.Subject = "TEST";
        smtpC.Port = 587;
       //Credentials for From address
        smtpC.Credentials =(System.Net.ICredentialsByHost) new System.Net.NetworkCredential("EmailID", "password");
         smtpC.EnableSsl = true;
         smtpC.Send(mail);
        Console.WriteLine("Message sent successfully");
       Console.ReadLine();
      }
   catch (Exception e)
    {
     Console.WriteLine(e.GetBaseException());
      Console.ReadLine();
    }
   }  
  }
 }
4

2 回答 2

1

您需要实际阅读您的错误消息,它会告诉您答案:

warning:The class defined in the script file named 'Program' is not derived from MonoBehaviour or ScriptableObject

要在 Unity 中创建自定义对象,您必须使它们从 ScriptableObject 或 MonoBehaviour 对象扩展,以便在您的游戏引擎中使用它们。ScriptableObject 用于不需要附加到游戏内对象的代码,而 MonoBehaviour 则需要。

在这种情况下,作为电子邮件处理程序,您需要使用可编写脚本的对象,因为它不会附加到场景对象。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using UnityEngine;
//^Make sure to include UnityEngine if you will connect to other game objects

//Extend ScriptableObject to use custom code 
public class EmailHandler extends ScriptableObject
{
    public void SendEmail()
    {
        try
        {
            MailMessage mail = new MailMessage();
            SmtpClient smtpC = new SmtpClient("smtp.gmail.com");
            //From address to send email
            mail.From = new MailAddress("From@gmail.com");
            //To address to send email
            mail.To.Add("To@gmail.com");
            mail.Body = "This is a test mail from C# program";
            mail.Subject = "TEST";
            smtpC.Port = 587;
            //Credentials for From address
            smtpC.Credentials =(System.Net.ICredentialsByHost) new System.Net.NetworkCredential("EmailID", "password");
            smtpC.EnableSsl = true;
            smtpC.Send(mail);

            //Change Console.Writeline to Debug.Log 
            Debug.Log ("Message sent successfully");
        }
        catch (Exception e)
        {
            Debug.Log(e.GetBaseException());
            //You don't need or use Console.ReadLine();
        }
    }  
}

使用它应该可以解决您的问题并帮助您完成课程。

于 2013-08-13T08:40:45.537 回答
0

好吧,从大约 5 分钟的 Google 搜索结果来看,由于证书验证错误导致的身份验证可能会得到修复。起初,我只是认为 Google 拒绝了您的凭据,因为您多次尝试连接和登录。

如果您在 Windows 上尝试运行此帖子正确答案提供的两个命令: Smtp 问题

于 2013-08-14T08:00:45.957 回答