开发者

Authentication Error in C# Send Mail App

开发者 https://www.devze.com 2022-12-29 15:02 出处:网络
I\'m developing a simple send mail app in C#, using my CMail Server: MailMessage mail = new MailMessage(\"from@mail.com\", \"destinatio开发者_运维问答n@mail.com\");

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

MailMessage mail = new MailMessage("from@mail.com", "destinatio开发者_运维问答n@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);

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!


Try adding:

smtp.EnableSsl = true;


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


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("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"); }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号