在unity3D中使用C#发送电子邮件?
我需要在Unity3D中使用C#发送电子邮件。我已经这样做了,但是遇到了运行时警告。请帮我解决下面的警告 警告:名为“Program”的脚本文件中定义的类不是从MonoBehavior或ScriptableObject派生的 我的代码是:在unity3D中使用C#发送电子邮件?,c#,email,unity3d,C#,Email,Unity3d,我需要在Unity3D中使用C#发送电子邮件。我已经这样做了,但是遇到了运行时警告。请帮我解决下面的警告 警告:名为“Program”的脚本文件中定义的类不是从MonoBehavior或ScriptableObject派生的 我的代码是: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Mail; namespace EMail {
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或MonoBehavior对象扩展,以便在游戏引擎中使用它们。ScriptableObject用于在MonoBehavior需要时不需要附加到游戏中对象的代码
在本例中,作为电子邮件处理程序,您需要使用可编写脚本的对象,因为它不会附加到场景对象
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();
}
}
}
使用它可以解决您的问题,并帮助您完成课程。通过5分钟的谷歌搜索,由于证书验证错误而导致的身份验证可能会得到修复。 起初我只是认为谷歌拒绝了你的认证,因为你已经多次尝试连接和登录 如果您在windows上尝试运行此命令提供的两个命令,请给出正确答案:
我不太确定我接触unity已经很久了,但是你不应该错过一个使用语句,例如“mono”或“unity”吗?我正在使用下面的代码,但出现了身份验证错误,如system.InvalidOperationException:SSL身份验证错误:RemoteCertificateNotAvailable,RemoteCertificateChaineErrors.那么如何解决这些plz帮助我urjentit Cames身份验证问题以便执行catch block is System.InvalidOperationException:SSL身份验证错误:RemoteCertificateNotAvailable,RemoteCertificateChaineErrors.,plz帮助我。我在谷歌花了十秒钟,你的错误是身份验证问题。阅读你的错误。这是Gmail的身份验证问题-您没有在
smtpC.Credentials=(System.Net.ICredentialsByHost)new System.Net.NetworkCredential(“EmailID”,“password”)上更改Gmail的用户和密码
我在电子邮件中得到的id来自邮件id,密码来自邮件id Passwprd。它是否正确。恕我直言,我已经告诉过你该怎么做了-将用户名和密码放入smtpC.Credentials=(System.Net.ICredentialsByHost)new System.Net.NetworkCredential(“将用户名放在这里”,“将密码放在这里”)
我不能再简单了。我不会继续帮助@user2632643-这是一个问答网站,我回答了你的问题。我们不是技术支持网站,不会为你编写代码。在谷歌上调查一下,看看你的错误,然后尝试其他解决方案。