c# - 在unity3D中使用C#发送邮件?

标签 c# email unity3d

我需要在 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();
    }
   }  
  }
 }

最佳答案

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

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();
        }
    }  
}

使用它应该可以解决您的问题并帮助您完成类(class)。

关于c# - 在unity3D中使用C#发送邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18202573/

相关文章:

c# - 如何在超时事件时注销用户

c# - htmlagilitypack xpath 不工作

java - 如何将 javax.mail.BodyPart 内容转换为 UTF-8 干净文本?

email - 如何解码电子邮件的内容?

android - 如何将 iOS Unity 游戏发送给赞助商/发行商

unity3d - 使用 ShaderLab 和 CG 统一进行着色器编程

c# - MongoDb c#官方驱动批量更新

c# - 在 .NET 中将 int 转换为 byte[4]

zend-framework - Zend 框架 : How to set default mail transport in configuration file?

c# - Android 上的 Unity .SVG 到 .PNG 转换