send email in c#

前端 未结 6 1588
猫巷女王i
猫巷女王i 2021-01-28 04:56

i\'ve tried to send email using this code..but an error occurred in smtp.Send(mail); messaging \"Failure sending mail\"

  MailMessage mail = ne         


        
6条回答
  •  粉色の甜心
    2021-01-28 05:33

    You should be using the using statement when creating a new MailMessage, plus a few things you missed out like port number and enableSSL

    using (MailMessage mail = new MailMessage())
    {
        mail.From = new MailAddress("from@gmail.com");
        mail.To.Add(new MailAddress("to@yahoo.com"));
        mail.Subject = "test sample";
        mail.Body = @"thank you";
    
        SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
        smtpServer.Port = 587;
        smtpServer.Credentials = new NetworkCredential("from@gmail.com", "password"); 
        smtpServer.EnableSsl = true;
        smtpServer.Send(mail);
    }
    

提交回复
热议问题