当前位置:   article > 正文

Unity邮件功能Mail的使用_unity 网易邮箱授权码

unity 网易邮箱授权码

目录

背景

版本

步骤

注意事项


背景

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

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

 

版本

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

 

步骤

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

  1. // add these usings to use Unity mail
  2. using System.Net;
  3. using System.Net.Mail;
  4. using System.Net.Security;
  5. using System.Security.Cryptography.X509Certificates;

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

  1. // create a new MailMessage
  2. MailMessage mail = new MailMessage();
  3. string account;
  4. string code_autorized;
  5. string title;
  6. string body;
  7. string reciever;
  8. string path_file;
  9. // set mail
  10. mail.From = new MailAddress(account); // account of sender
  11. mail.To.Add(reciever); // account of reciever
  12. mail.Subject = title; // mail subject(title)
  13. mail.Body = body; // mail body
  14. mail.Attachments.Add(new Attachment(path_file)); // add attachments
  15. // smtp(Simple Mail Transfer Protocol) sends mail for you
  16. string smtp_tmp = account.Split('@')[1]; // eg:"xxxxx@qq.com"->"qq.com"
  17. SmtpClient smtpServer = new SmtpClient("smtp." + smtp_tmp); // eg:"smtp.qq.com"
  18. // identify your account and authorized code
  19. smtpServer.Credentials = new System.Net.NetworkCredential(account, code_autorized) as ICredentialsByHost;
  20. smtpServer.EnableSsl = true;
  21. // callbacks
  22. ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors){ return true; };
  23. // send
  24. smtpServer.Send(mail);

详细看每一部分:

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

  1. // create a new MailMessage
  2. MailMessage mail = new MailMessage();
  3. string account;
  4. string code_autorized;
  5. string title;
  6. string body;
  7. string reciever;
  8. string path_file;
  9. // set mail
  10. mail.From = new MailAddress(account); // account of sender
  11. mail.To.Add(reciever); // account of reciever
  12. mail.Subject = title; // mail subject(title)
  13. mail.Body = body; // mail body
  14. mail.Attachments.Add(new Attachment(path_file)); // add attachments

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

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

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

  1. // identify your account and authorized code
  2. smtpServer.Credentials = new System.Net.NetworkCredential(account, code_autorized) as ICredentialsByHost;
  3. smtpServer.EnableSsl = true;
  4. // callbacks
  5. ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors){ return true; };
  6. // send
  7. smtpServer.Send(mail);

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

  1. using system
  2. try
  3. {
  4. // send code
  5. }
  6. cathch(Exception ex)
  7. {
  8. print("error code:" + ex.StackTrace)
  9. }

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

 

注意事项

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

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

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

 

欢迎交流和指正。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/233624
推荐阅读
相关标签
  

闽ICP备14008679号