如何从c#发送邮件

Red*_*wan 5 c# email send

我有代码,

 System.Web.Mail.MailMessage oMailMessage = new MailMessage();
            oMailMessage.From = strFromEmaild;
            oMailMessage.To = strToEmailId;
            oMailMessage.Subject = strSubject;
            oMailMessage.Body = strBody;
            SmtpMail.SmtpServer = "localhost";
            SmtpMail.Send(oMailMessage);
Run Code Online (Sandbox Code Playgroud)

(所有变量都有值)

我已经安装了SMTP虚拟服务.为什么它无法发送电子邮件.为什么不工作?

编辑

public bool SendMail(string strToEmailId, string strFromEmaild, string strSubject, string strBody)
{
    try
    {
        System.Web.Mail.MailMessage oMailMessage = new MailMessage();
        oMailMessage.From = strFromEmaild;
        oMailMessage.To = strToEmailId;
        oMailMessage.Subject = strSubject;
        oMailMessage.Body = strBody;
        SmtpMail.SmtpServer = "SERVERNAME";
        SmtpMail.Send(oMailMessage);

        return true;
     }
     catch (Exception ex)
     {
         return false;
     }
 }
Run Code Online (Sandbox Code Playgroud)

我有这个代码.它正在执行正常并返回true,但我没有在收件箱中收到任何电子邮件.

还有什么可能是错的?

在C:\ Inetpub\mailroot\Badmail的BadMail Dir中获取一些邮件也在队列目录中获取一些邮件......这意味着什么.. ??

我发现邮件只能发送到Gmail帐户......为什么会这样?

p.c*_*ell 1

确定错误是什么:

try
{
 SmtpMail.Send(oMailMessage);
}
catch (Exception ex)
{
//breakpoint here to determine what the error is:
Console.WriteLine(ex.Message);
}
Run Code Online (Sandbox Code Playgroud)

从这里,请使用异常详细信息编辑您的问题。