Automatic email sending function in Unity

take a note

Due to project requirements, a function of sending emails with attachments is required. Without further ado, first upload the core code, and finally talk about the pitfalls encountered

private void Send()
    {
      //判断输入的字符串是否是邮箱
        if (IsEmail(EmailText.text)&& !IsSending)
        {
            IsSending = true;
            MailMessage mail = new MailMessage();
          //设置发件人邮箱
            mail.From = new MailAddress("[email protected]");
                //设置收件人邮箱地址
            mail.To.Add(EmailText.text);
            if (cCEmailManager != null)
            {
                foreach (var email in cCEmailManager.CCEmailList)
                {
                  //设置抄送人邮箱地址
                    mail.CC.Add(email.CCEmailAccount);
                }
            }
          //设置邮件内容
            string content = string.Empty;
            if (TitleText != null && TitleText.text != string.Empty)
            {
                content = TitleText.text;
            }
            else
            {
                content = ExportDatas.Instance.GetFileName();
            }
          //设置主体的字符编码
            mail.BodyEncoding = System.Text.Encoding.UTF8;
          //设置邮件标题
            mail.Subject = content;
          //设置邮件内容
            mail.Body = content;
          //下面是创建设置发件人邮箱的类型端口密码等
            SmtpClient smtpServer = new SmtpClient(/*"smtp.qq.com"*/"smtp.exmail.qq.com");
            //smtpServer.Port = 465/*587*/;
            smtpServer.Credentials = new System.Net.NetworkCredential("[email protected]", "xxxxxx") as ICredentialsByHost;
            smtpServer.EnableSsl = true;
            ServicePointManager.ServerCertificateValidationCallback =
                delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
                { return true; };
          //下面是添加邮件附件
            mail.Attachments.Add(new Attachment(ExportDatas.Instance.GetFilePath()));
          //下面添加发送完成的回调,方法自己写
            smtpServer.SendCompleted += new SendCompletedEventHandler(SendEmailCompleted);
            if (SendTips != null) SendTips.gameObject.SetActive(true);
            if (SendingTips != null) SendingTips.gameObject.SetActive(true);
            if (SendCompletedTips != null) SendCompletedTips.gameObject.SetActive(false);
            Debug.Log("发送中");
            smtpServer.SendMailAsync(mail);
        }
        else
        {
            //IsSending = false;
            UIToastTool.Instance.Show("请填写正确的邮箱地址");
        }
    }


Basically, the core code for sending emails is the one above. Let’s talk about the pitfalls encountered below.

1. When setting the sender’s mailbox type port, etc., if it is a personal QQ mailbox, then setting the port is no problem. If it is a Tencent corporate mailbox, please remove the port setting. Although the official document says that the port needs to be set, once it is set It will fail to send.

2. The sender's password is not the real login password filled in when registering the mailbox, but the code produced when setting the permission to automatically send emails. As for how to set the permission, please go to the specific official document for different mailbox types.

3. The callback of successful email sending will conflict with some codes, for example, it will conflict with the following method of selecting the folder location provided by windows, resulting in failure to receive the callback information

//System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog(); 

Guess you like

Origin blog.csdn.net/m0_69824302/article/details/130773288