我有这个MVC C#App和一个试图发送电子邮件的控制器,但我总是遇到这个错误
这是我的action'controller中的代码,它没有收到任何模型,所以我添加了特定的值
[HttpPost]
public ActionResult Index2()
{
try
{
MailMessage mail = new MailMessage();
mail.To.Add("valid_email@hotmail.com");
mail.From = new MailAddress("valid_email@gmail.com");
mail.Subject = "PruebaMVC";
string Body = "PruebaMVC";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential
("valid_email@gmail.com", "correctPWD");
smtp.EnableSsl = true;
smtp.Send(mail);
}
catch (Exception ex)
{
throw new ArgumentNullException("Exception in sendEmail:" + ex.Message);
}
return View();
}
你可以告诉我有什么问题吗?
答案 0 :(得分:0)
因为我使用了下面的代码,它对我来说很好,我在web.config中配置了smtp
ContentType HTMLType = new ContentType(" text / html"); NetworkCredential cred = new NetworkCredential(StringConstant.NetworkUserName,StringConstant.NetworkPwd);
MailMessage msg = new MailMessage();
msg.BodyEncoding = System.Text.Encoding.Default;
msg.To.Add(ToEmail);
msg.Priority = System.Net.Mail.MailPriority.High;
msg.Subject = Subj;
msg.Body = strBody;
msg.IsBodyHtml = true;
if (attachement != null)
{
msg.Attachments.Add(attachement);
}
System.Net.Mail.AlternateView HTMLView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(strBody, HTMLType);
msg.From = new MailAddress(StringConstant.MailFrom); // Your Email Id
SmtpClient client = new SmtpClient(StringConstant.SmtpFrom, StringConstant.SmtpPort);
client.Credentials = cred;
client.EnableSsl = true;
client.Send(msg);