0

我正在使用我的 CMail 服务器在 C# 中开发一个简单的发送邮件应用程序:

MailMessage mail = new MailMessage("from@mail.com", "destination@mail.com");
        mail.Subject = "Sub";
        mail.Body = "Hi!";
        SmtpClient smtp = new SmtpClient("MyServer");
        System.Net.NetworkCredential cred = new System.Net.NetworkCredential("user", "pass");
        smtp.UseDefaultCredentials = false;
        smtp.Credentials = cred;
        smtp.Send(mail);

显然我省略了我的帐户信息,因此,此代码出于某种原因向我抛出了身份验证异常。我首先认为代码是错误的,所以我将信息更改为我的 gmail 帐户,一切正常,我唯一遇到问题的 SMTP 服务器是 CMail。.NET 和 CMail 的 SMTP 有问题吗?

感谢您的帮助和评论!

4

3 回答 3

0

如果您使用两步验证,则需要添加应用程序专用密码。

于 2013-04-26T01:20:32.213 回答
0

尝试添加:

smtp.EnableSsl = true;
于 2010-05-03T20:48:20.863 回答
0

完整的工作样本

public static void sendEmail()
    {
        //for use GMAIL require enable - 
        //https://myaccount.google.com/lesssecureapps?rfn=27&rfnc=1&eid=39511352899709300&et=0&asae=2&pli=1
        Console.WriteLine("START MAIL SENDER");

        //Авторизация на SMTP сервере
        SmtpClient Smtp = new SmtpClient("smtp.gmail.com", 587);
        Smtp.EnableSsl = true;
        Smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
        Smtp.UseDefaultCredentials = false;
         //username   password
        Smtp.Credentials = new NetworkCredential("rere", "rere");

        //Формирование письма
        MailMessage Message = new MailMessage();
        Message.From = new MailAddress("rere@gmail.com");
        Message.To.Add(new MailAddress("rere@gmail.com"));
        Message.Subject = "test mesage";
        Message.Body = "tttt body";

        string file = "D:\\0.txt";
        if (file != "")
        {
            Attachment attach = new Attachment(file, MediaTypeNames.Application.Octet);
            ContentDisposition disposition = attach.ContentDisposition;
            disposition.CreationDate = System.IO.File.GetCreationTime(file);
            disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
            disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
            Message.Attachments.Add(attach);
            Console.WriteLine("ADD FILE [" + file + "]");
        }
        try
        {
            Smtp.Send(Message);
            MessageBox.Show("SUCCESS");
        }
        catch { MessageBox.Show("WRONG"); }
    }
于 2017-05-12T16:25:29.370 回答