asp.net发送邮件问题

时间:2010-07-06 06:33:40

标签: c# .net asp.net

我尝试从我的应用程序发送电子邮件。一切似乎都没问题,邮件消息,凭证等。

当我调试代码时,它不会陷入异常并且不会发送电子邮件。此外,服务器上没有邮件队列。没有错误消息。

msgObj.Subject = this.Subject;        
    msgObj.From = new MailAddress(this.From , this.Display_Name);//
    msgObj.Body = this.Message;
    msgObj.IsBodyHtml = true;

    SmtpClient client = new SmtpClient(this.SMTP_Server,25);
                client.Credentials = new System.Net.NetworkCredential(SMTP_User + "@doping.com.tr", SMTP_Password);


                try
                {
                    client.Send(msgObj);
                    return true;
                }
                catch (Exception ex)
                {
                    ex.ToString();
                    return false;
                } 

可能是什么问题?

感谢。

2 个答案:

答案 0 :(得分:0)

检查端口。端口25是否正在使用支持。还要检查SSL。如果还是不行。请尝试以下代码。

 /// <summary>
/// Transmit an email message
/// </summary>
/// <param name="from">Senders Name </param>
/// <param name="fromPerson">Sender Email Address</param>
/// <param name="body">The Email Message Body</param>
/// <returns>Status Message as String</returns>
public static void SendMail(string fromEmail, string fromName, string body)
{

    try
    {

        using (MailMessage mail = new MailMessage())
        {
            mail.To.Add("abc@gmail.com");
            mail.From = new MailAddress(fromEmail, fromName);
            mail.Subject = "Report ";
            mail.SubjectEncoding = System.Text.Encoding.UTF8;
            mail.Body = body;
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            mail.Priority = MailPriority.High;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = global::ProjectName.Properties.Settings.Default.Host;
            smtp.Port = global::ProjectName.Properties.Settings.Default.Port;

            if (smtp.Port == 587)
            {
                smtp.EnableSsl = true;
            }
            string userName = global::ProjectName.Properties.Settings.Default.UserName;
            string password = global::ProjectName.Properties.Settings.Default.Password;
            smtp.Credentials = new NetworkCredential(userName, password);
            smtp.Send(mail);

        }
    }

    catch (SmtpException smEx)
    {
        LogError(smEx.Message, smEx.StackTrace);
    }
}

这里我使用项目的设置文件并从那里检索值。

答案 1 :(得分:0)

可以使用SmtpClient配置命名空间配置System.Net的默认设置,例如:

<configuration>
  <system.net>
    <mailSettings>
      <smtp deliveryMethod="network" from="ben@contoso.com">
        <network host="localhost" port="25" defaultCredentials="true" />
      </smtp>
    </mailSettings>
  </system.net>
</configuration>

有关详细信息,请参阅<smtp> Element (Network Settings) topic