首页 > 编程知识 正文

收不到unity的邮件,unity邮箱验证

时间:2023-05-03 20:36:38 阅读:246878 作者:3463

目录

背景

版本

步骤

注意事项


背景

最近做一个小应用,需要通过Unity程序统计用户行为,然后将数据返回来进行研究。一开始打算是搭建服务器来传数据,但是一是时间比较紧,二是我的数据只需要一次传回,没有实时性要求,所以搭个服务器性价比不高。

考虑代替方案的时候先是想生成一个数据文件或者是在UI里面做一个可以复制的文本框来装数据,之后用户帮我发回(熟人QQ、微信,陌生人就邮箱)。忽然查到原来unity就有发邮件的功能,于是赶紧mark下来,分享一下。

  版本 Unity 2018.4.16c1 (64-bit) 这个项目是2D项目JDK 1.8.0_152SDK(Android Studio中自带的,2020年2月20日左右的版本)  步骤

1、首先是要using这些Mail、Net和Security模块

// add these usings to use Unity mailusing System.Net;using System.Net.Mail;using System.Net.Security;using System.Security.Cryptography.X509Certificates;

2、在需要发送邮件的地方加入下面的语句使用邮箱

// create a new MailMessageMailMessage mail = new MailMessage();string account;string code_autorized;string title;string body;string reciever;string path_file;// set mailmail.From = new MailAddress(account); // account of sendermail.To.Add(reciever); // account of recievermail.Subject = title; // mail subject(title)mail.Body = body; // mail bodymail.Attachments.Add(new Attachment(path_file)); // add attachments// smtp(Simple Mail Transfer Protocol) sends mail for youstring smtp_tmp = account.Split('@')[1]; // eg:"xxxxx@qq.com"->"qq.com"SmtpClient smtpServer = new SmtpClient("smtp." + smtp_tmp); // eg:"smtp.qq.com"// identify your account and authorized codesmtpServer.Credentials = new System.Net.NetworkCredential(account, code_autorized) as ICredentialsByHost;smtpServer.EnableSsl = true;// callbacksServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors){ return true; };// sendsmtpServer.Send(mail);

详细看每一部分:

1、新建MailMessage实例,设置发件人、收件人、邮件主题、附件、内容等。

// create a new MailMessageMailMessage mail = new MailMessage();string account;string code_autorized;string title;string body;string reciever;string path_file;// set mailmail.From = new MailAddress(account); // account of sendermail.To.Add(reciever); // account of recievermail.Subject = title; // mail subject(title)mail.Body = body; // mail bodymail.Attachments.Add(new Attachment(path_file)); // add attachments

2、新建SmtpClient实例,通过中继转发邮件,我写了一个字符分割的语句,以smtp.xx.com来作为不同邮箱中继转发,目前试过163和qq邮箱是可行的

// smtp(Simple Mail Transfer Protocol) sends mail for youstring smtp_tmp = account.Split('@')[1]; // eg:"xxxxx@qq.com"->"qq.com"SmtpClient smtpServer = new SmtpClient("smtp." + smtp_tmp); // eg:"smtp.qq.com"

3、验证账户和授权码(注意不是密码),之后进行发送

// identify your account and authorized codesmtpServer.Credentials = new System.Net.NetworkCredential(account, code_autorized) as ICredentialsByHost;smtpServer.EnableSsl = true;// callbacksServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors){ return true; };// sendsmtpServer.Send(mail);

另外还可以加入try...catch语句来检测是否发送成功

using systemtry{ // send code}cathch(Exception ex){ print("error code:" + ex.StackTrace)}

经测试,发送邮件在unity的编辑器以及PC、Android导出版本中都是可用的。

 

注意事项

授权码注意不是密码,而是邮箱提供的用于通过第三方验证邮箱用的特殊码,163邮箱和qq邮箱的授权码获取方式如下:

163邮箱设置,可以通过“重置授权码”来自定义授权码

QQ邮箱在设置->账户下开启POP3/SMTP后,将进行身份验证,验证成功则会自动产生一个授权码

 

欢迎交流和指正。

win7系统出现电脑蓝屏如何解决win7电脑文件夹如何加密HTML div三边怎么加边框Session超时问题怎么解决

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。