电子邮件不统一发送? [英] Email not sending in unity android?

查看:43
本文介绍了电子邮件不统一发送?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了许多来源和问题,但我仍然不清楚通过 Unity 发送电子邮件的代码如何在 android 版本中不起作用.它在 Windows 构建中运行良好,但在 android 中不起作用.有人能帮我吗.这是代码 -

I have looked many source and question but I am still not clear to how this code of sending email through unity is not working in android build. It works fine in windows build but doesn't work in android. Can someone help me. Here is the code -

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using UnityEngine;
public class EmailUnity
{

public static string SenderEmail;
public static string SenderPassword;
public static string SmtpClient;
public static int SmtpPort;

public static void SendEmail(string to, string subject, string body, bool isHtml, string[] attachmentPaths,
    Action<object, AsyncCompletedEventArgs> callback = null)
{
    try
    {
        SmtpClient emailServer = new SmtpClient(SmtpClient, SmtpPort);
        emailServer.EnableSsl = true;
        emailServer.Credentials = (ICredentialsByHost) new NetworkCredential(SenderEmail, SenderPassword);

        ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

        MailMessage message = new MailMessage(SenderEmail, to);
        message.Subject = subject;
        message.Body = body;
        message.IsBodyHtml = isHtml;

        foreach (string path in attachmentPaths)
        {
            if (!string.IsNullOrEmpty(path) && File.Exists(path))
            {
                message.Attachments.Add(new Attachment(path));
            }
        }
        if (callback == null)
        {
            callback = SampleCallback;
        }
        emailServer.SendCompleted += new SendCompletedEventHandler(callback);
        emailServer.SendAsync(message, "");

        Debug.Log("Email sending");
    }
    catch (Exception ex)
    {
        Debug.Log("Error: " + ex.Message);
        callback("", new AsyncCompletedEventArgs(ex, true, "Exception occured"));
    }
}

private static void SampleCallback(object sender, AsyncCompletedEventArgs e)
{
    if (e.Cancelled || e.Error != null)
    {
        Debug.Log("Error: " + e.Error.Message);
    }
    else
    {
        Debug.Log("Email sent");
    }
}
}

推荐答案

如果 SmtpClient 不能在 Android 上运行,可以尝试这些方法.

These are the things to try if SmtpClient is not working on Android.

转到文件 --> 构建设置... --> 选择 Android.现在,点击播放器设置.

Go to File --> Build Settings... --> Select Android. Now, click on Player Settings.

1.在互联网访问上,将其从自动更改为需要.

1.On the Internet Access, change it from Auto to Require.

2.确保 API Compatible Level 设置为 .NET 2.0 而不是 .NET 2.0 Subset.

2.Make sure that API Compatible Level is set to .NET 2.0 not .NET 2.0 Subset.

3.确保剥离级别设置为禁用.

4.转到播放器设置 --> Android 并将互联网访问自动更改要求

4.Go to Player settings --> Android and change Internet Access from Auto to Require

这篇关于电子邮件不统一发送?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆