发送邮件失败c#

时间:2013-10-08 16:49:16

标签: c# asp.net visual-studio

我在尝试发送邮件时遇到问题。 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);

1 个答案:

答案 0 :(得分: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);
    }

希望有所帮助