-2

我在尝试发送邮件时遇到问题。VS 给我一个这个错误:发送邮件失败。我做错了什么!!

 string from = "from@htomail.com";
            string to = "to@gmail.com";
            MailMessage mail = new MailMessage();
            mail.To.Add(to);
            mail.From = new MailAddress(from, "test", System.Text.Encoding.UTF8);
            mail.Subject = "testing";
            mail.SubjectEncoding = System.Text.Encoding.UTF8;
            mail.Body = "Loren ipsum";
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            mail.IsBodyHtml = true;
            mail.Priority = MailPriority.High;
            SmtpClient smtp = new SmtpClient();
            smtp.Credentials = new System.Net.NetworkCredential(from, "password");
            smtp.Port = 587;
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            smtp.Send(mail);
4

1 回答 1

2

尝试在您的 helper.cs 中执行此操作

public static void SendEmail(string email, string subject, string message)
    {
        MailMessage emailMessage = new MailMessage();
        emailMessage.From = new MailAddress("administrator@gmail.com", "Administrator");
        emailMessage.To.Add(new MailAddress(email));
        emailMessage.Subject = subject;
        emailMessage.Body = message;
        emailMessage.IsBodyHtml = true;
        emailMessage.Priority = MailPriority.Normal;
        SmtpClient MailClient = new SmtpClient("smtp.gmail.com", 587);
        MailClient.EnableSsl = true;
        MailClient.Credentials = new System.Net.NetworkCredential("administrator@gmail.com", "p@ssw0rd123");
        MailClient.Send(emailMessage);
    }

希望它有所帮助

于 2013-10-09T18:05:50.127 回答