Unity邮件功能Mail的使用

目录

背景

版本

步骤

注意事项


背景

最近做一个小应用,需要通过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模块

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

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

// create a new MailMessage
MailMessage mail = new MailMessage();

string account;
string code_autorized;
string title;
string body;
string reciever;
string path_file;

// set mail
mail.From = new MailAddress(account); // account of sender
mail.To.Add(reciever); // account of reciever
mail.Subject = title; // mail subject(title)
mail.Body = body; // mail body
mail.Attachments.Add(new Attachment(path_file)); // add attachments

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

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

// send
smtpServer.Send(mail);

详细看每一部分:

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

// create a new MailMessage
MailMessage mail = new MailMessage();

string account;
string code_autorized;
string title;
string body;
string reciever;
string path_file;

// set mail
mail.From = new MailAddress(account); // account of sender
mail.To.Add(reciever); // account of reciever
mail.Subject = title; // mail subject(title)
mail.Body = body; // mail body
mail.Attachments.Add(new Attachment(path_file)); // add attachments

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

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

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

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

// send
smtpServer.Send(mail);

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

using system

try
{
    // send code
}

cathch(Exception ex)
{
    print("error code:" + ex.StackTrace)
}

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

注意事项

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

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

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

 

欢迎交流和指正。

发布了8 篇原创文章 · 获赞 6 · 访问量 5554

猜你喜欢

转载自blog.csdn.net/longroad1216/article/details/104582437
今日推荐