asp电子邮件System.Net.Mail

时间:2011-05-26 19:51:36

标签: c# asp.net email

使用此代码(System.Net.Mail):

SmtpClient client = new SmtpClient();
client.Send(MyMessage);

我收到此错误:

  

System.Net.Sockets.SocketException:没有   可以联系,因为   目标机器积极拒绝它   127.0.0.1:25

Web.config条目。

工作代码。

        {
    string body = "";
    body = "<table border='0' align='center' cellpadding='2' style='border-collapse: collapse' bordercolor=''#111111' width='100%' id='AutoNumber1'>";
    body = body + "<tr><td width='100%' align='center' colspan='6'><b>Feed Back Form</b></td></tr>";
    body = body + "<tr><td width='100%' colspan='6'>&nbsp;</td></tr>";
    body = body + "<tr><td width='50%' colspan='2'>Name</td><td width='50%' colspan='4'><b>" + name.Text + "</b></td></tr>";
    body = body + "<tr><td width='50%' colspan='2'>Address</td><td width='50%' colspan='4'><b>" + Address.Text + "</b></td></tr>";
    body = body + "<tr><td width='50%' colspan='2'>City</td><td width='50%' colspan='4'><b>" + City.Text + "</b></td></tr>";
    body = body + "<tr><td width='50%' colspan='2'>State</td><td width='50%' colspan='4'><b>" + State.Text + "</b></td></tr>";
    body = body + "<tr><td width='50%' colspan='2'>Country</td><td width='50%' colspan='4'><b>" + Country.Text + "</b></td></tr>";
    body = body + "<tr><td width='50%' colspan='2'>Zip/Pin Code</td><td width='50%' colspan='4'><b>" + ZipCode.Text + "</b></td></tr>";
    body = body + "<tr><td width='50%' colspan='2'>Phone</td><td width='50%' colspan='4'><b>" + Phone.Text + "</b></td></tr>";
    body = body + "<tr><td width='50%' colspan='2'>E-Mail</td><td width='50%' colspan='4'><b>" + email.Text + "</b></td></tr>";
    body = body + "<tr><td width='50%' colspan='2'>Website URL (If Any)</td><td width='50%' colspan='4'><b>" + weburl.Text + "</b></td></tr>";
    body = body + "<tr><td width='50%' colspan='2'>How did you know about Country Oven?</td>";
    body = body + "<td width='50%'><b>" + radiobutn.SelectedItem.Text + "</b></td></tr>";
    body = body + "<tr><td width='50%' colspan='2'>Your feedback/suggestions for the site</td>";
    body = body + "<td width='50%' colspan='4'><b>" + txtsugg.Text + "</b></td></tr>";
    body = body + "<tr><td width='50%' colspan='2'>Query (If you have any)</td>";
    body = body + "<td width='50%' colspan='4'><b>" + query.Text + "</b></td></tr></table>";
    MailMessage message = new MailMessage();
    message.To = "contact@xxxx.com";
    message.From = email.Text;
    message.Subject = "ContactUs Form";
    message.BodyFormat = MailFormat.Html;
    message.Body = body;
    SmtpMail.SmtpServer.Insert(0, "");
    SmtpMail.Send(message);
    // lblmsg.Text = "Message sent successfully";
    RegisterStartupScript("startupScript", "<script language=JavaScript>alert('Message sent successfully.');</script>");
    clear();
}


 <system.net>
    <mailSettings>
        <smtp deliveryMethod="Network">
        <network defaultCredentials="True" host="LocalHost" port="25" />
        </smtp>
    </mailSettings>
</system.net>

有什么问题?

4 个答案:

答案 0 :(得分:2)

确保您的防火墙允许端口25.此外,还在本地计算机上配置并运行SMTP服务。

答案 1 :(得分:1)

也许您刚刚没有在本地主机上运行SMTP服务器。

答案 2 :(得分:1)

您可能没有运行smtp服务器。如果将文件转储到磁盘上,请尝试:

<mailSettings>
  <smtp deliveryMethod="SpecifiedPickupDirectory">
      <specifiedPickupDirectory pickupDirectoryLocation="c:\Temp\mail\"/>
   </smtp>
</mailSettings>

如果此方法有效,请尝试设置本地邮件服务器并将设置更改回当前值。

这些答案可能对您有所帮助:

答案 3 :(得分:0)

如果在退出弃用方法之前它在同一台机器上运行正常,那么我会假设.NET发送邮件的默认配置与旧版本的默认配置不同。

也许尝试删除webconfig中的传递方法条目并使用默认方法。

我以前用这种方式进行交换,除了来自地址,主机,用户名和密码之外从未指定任何内容。

相关问题