发送电子邮件 - 我哪里错了?

时间:2014-02-12 16:09:38

标签: c#

我正在尝试发送电子邮件,每次调用此方法并尝试发送时,都会抛出“发送邮件失败”错误。它不是很具描述性,而且我不确定这甚至是失败的,因为它经历了所有的代码并且似乎没关系。

代码:

public void SendEmail()
{
    private string localhost = System.Net.Dns.GetHostName();

    System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
    msg.To.Add(strUserEmail);

    //determine if the email sent was successful
    if (blnFlag == true)
    {
        msg.Subject = "File Move was successful";
    }
    else
    {
        msg.Subject = "File Move";
    }

    msg.From = new System.Net.Mail.MailAddress(GlobalVars.strFromEmail);
    msg.Body = strMessage;
    SmtpClient client = new SmtpClient(localhost);
    client.Send(msg);
}

1 个答案:

答案 0 :(得分:0)

 client.EnableSsl = true;
 NetworkCredential crend = new NetworkCredential("SenderMailAddress","SenderPassword" );
 client.Credentials = crend;

并尝试catch send方法 - 这只是常见的选择)