E-mail. C#. Exception: Message could not be sent.

Программирование на Юнити.

E-mail. C#. Exception: Message could not be sent.

Сообщение Susanin 06 ноя 2017, 21:22

Народ.
Пытаюсь отправить через шарп письмо, посредством ф-и:
Синтаксис:
Используется csharp
public static void SendMail(string smtpServer, string from, string password,string mailto, string caption, string message, string attachFile = null)
{
    try
    {
        MailMessage mail = new MailMessage();
            mail.From = new MailAddress(from);
            mail.To.Add(new MailAddress(mailto));
            mail.Subject = caption;
            mail.Body = message;

            if (!string.IsNullOrEmpty(attachFile))
            mail.Attachments.Add(new Attachment(attachFile));

        SmtpClient client = new SmtpClient();
            client.Host = smtpServer;
            client.Port =  587;
            client.UseDefaultCredentials = false;
            client.EnableSsl = true;
            client.Credentials = new NetworkCredential(from.Split('@')[0], password) as ICredentialsByHost;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.Send(mail);
        mail.Dispose();
    }
    catch (Exception e)
    {
        throw new Exception("Mail.Send: " + e.Message);
    }
}
 


Однако упорно выкидывает исключение Exception: Mail.Send: Message could not be sent.
Запрос корректен: SendMail("smtp.gmail.com", "почта@gmail.com", "password", "crazysovietingeneere@gmail.com", "Тема письма", "Тело письма");

Попробовал
1) На Gmailе активировать POP для всех писем.
2) В playerSettingn/ Configuration / API compability выставил .NET 2.0
3) Порты и адреса менял (mail.ru и gmail)
Ниче не помогает.
Может кто сталкивался?
Susanin
UNIт
 
Сообщения: 120
Зарегистрирован: 11 дек 2015, 10:52

Re: E-mail. C#. Exception: Message could not be sent.

Сообщение Bill Gates 08 ноя 2017, 08:01

Попробуй. Через прокси работать не будет.

Синтаксис:
Используется csharp
using System.Collections;

using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;

public static class Utils
{
    public static bool SendMail (string smtpServer,
    int port,
    string from,
    string password,
    string[] mailTo,
    string caption,
    string message,
    bool htmlMessageBody = false,
    string[] attachFiles = null)
    {
        using (MailMessage mail = new MailMessage ())
        {
            try
            {
                mail.From = new MailAddress (from);

                for (int i = 0; i < mailTo.Length; ++i)
                    mail.To.Add (new MailAddress (mailTo[i]));

                mail.Subject = caption;
                mail.Body = message;
                mail.IsBodyHtml = htmlMessageBody;

                if (attachFiles != null)
                {
                    for (int i = 0; i < attachFiles.Length; ++i)
                    {
                        if (!string.IsNullOrEmpty (attachFiles[i]))
                        {
                            mail.Attachments.Add (new Attachment (attachFiles[i]));
                        }
                    }
                }

                SmtpClient client = new SmtpClient ();

                client.Host = smtpServer;
                client.Port = port;
                client.Timeout = 3;
                client.EnableSsl = true;
                client.UseDefaultCredentials = false;

                var credentials = new NetworkCredential (from, password);

                client.Credentials = (ICredentialsByHost)credentials;
                client.DeliveryMethod = SmtpDeliveryMethod.Network;

                ServicePointManager.ServerCertificateValidationCallback =
                    delegate (
                    object s,
                    X509Certificate certificate,
                    X509Chain chain,
                    SslPolicyErrors sslPolicyErrors)
                    {
                        return true;
                    };

                client.Send (mail);

                return true;
            }
            catch (System.Exception e)
            {
                Debug.LogException (e);

                return false;
            }
        }
    }
}
 
Bill Gates
UNIт
 
Сообщения: 127
Зарегистрирован: 16 июл 2015, 11:27

Re: E-mail. C#. Exception: Message could not be sent.

Сообщение Susanin 12 ноя 2017, 02:16

Я так понимаю он ругался на метод шифрования:
ServicePointManager.ServerCertificateValidationCallback =
delegate (
object s,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
return true;
};
Susanin
UNIт
 
Сообщения: 120
Зарегистрирован: 11 дек 2015, 10:52


Вернуться в Скрипты

Кто сейчас на конференции

Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 8