C# 发送邮件应用程序中的身份验证错误

发布于 2024-08-31 04:14:12 字数 840 浏览 4 评论 0原文

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

MailMessage mail = new MailMessage("[email protected]", "[email protected]");
        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有问题吗?

感谢您的帮助和评论!

I'm developing a simple send mail app in C#, using my CMail Server:

MailMessage mail = new MailMessage("[email protected]", "[email protected]");
        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);

Obviously i ommited my account information, so, this code throws me an Authentication Exception for some reason. I first thought that the code was wrong, so i change the info to my gmail account and everything goes fine, with the only SMTP server that i having trouble is with the CMail. Is there a problem with .NET and CMail's SMTP ?

Thanks for the help and comments!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

壹場煙雨 2024-09-07 04:14:12

尝试添加:

smtp.EnableSsl = true;

Try adding:

smtp.EnableSsl = true;
疯到世界奔溃 2024-09-07 04:14:12

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

If you are using 2-step verification then you will need to add application specific password.

晌融 2024-09-07 04:14:12

完整的工作样本

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("[email protected]");
        Message.To.Add(new MailAddress("[email protected]"));
        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"); }
    }

Full work sample

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("[email protected]");
        Message.To.Add(new MailAddress("[email protected]"));
        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"); }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文