接收电子邮件时出现问题

时间:2013-11-13 07:18:43

标签: c# email asp.net-mvc-4

我正在使用c#在mvc4中完成我的项目。我的网站上有联系页面。我需要的是我必须在点击发送按钮时从其他ID的接收我的电子邮件ID。我使用以下代码

public void ReceiveMail(string name,string email,string message)
{
    MailMessage msg = new MailMessage();
    HttpContext ctx = HttpContext.Current;
    msg.To.Add(new MailAddress("MyEmailId"));
    msg.From = new MailAddress(email);
    msg.Subject =name + "send a message";
    msg.Priority = MailPriority.High;
    msg.Body = message;
    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");// i am confused what to write here
    SmtpServer.Send(msg);
}

显示错误

 The SMTP server requires a secure connection or the client was not authenticated.
 The server response was: 5.7.0 Must issue a STARTTLS command first. 
 at4sm42219747pbc.30 - gsmtp

我不知道从哪台服务器收到邮件。那我怎么解决这个问题呢。请帮帮我

4 个答案:

答案 0 :(得分:1)

使用Gmail发送电子邮件需要一些其他设置。首先,端口号应为587(而不是默认值25)。第二,Gmail需要安全连接。当然,您应该提供有效的凭证。

总而言之,SmtpClient的初始化应该如下所示:

SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com", 587);
SmtpServer.EnableSsl = true;
SmtpServer.Credentials = new NetworkCredential("username@gmail.com", "password");

答案 1 :(得分:1)

如错误所示,应首先使用STARTTLS命令。 Thas意味着gmail只接受通过安全连接的邮件。在this answer中,enableSsl设置为true。正如documentation from microsoft所说,SmtpClient类也有这样的属性。此外,您应将凭据保留在smptClient中。我认为gmail只接受来自身份验证用户的邮件。我认为整个问题已经解决here

答案 2 :(得分:0)

您需要使用NetworkCredential登录Gmail SMTP服务器。错误很明显。

SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("your-email", "your-password");

答案 3 :(得分:0)

你试过了吗?

smtpServer.Host = "smtp.gmail.com";
smtpServer.Port = 587;
smtpServer.Credentials = 
    new NetworkCredential("SenderGmailUserName", "SenderPassword");
相关问题