【问题标题】:Sending Email using SMTP in C#在 C# 中使用 SMTP 发送电子邮件
【发布时间】:2013-04-03 19:17:54
【问题描述】:

**它只是说“发送邮件失败。” 不确定代码或 SMTP 服务器的问题?请帮助这是我的代码,变量是通过表单发送的,我已经确认所有变量都正常

SMTP 设置在 Web.config** 中

<?xml version="1.0"?>
<configuration>
  <system.net>
    <mailSettings>
      <smtp from="newsletter@abc.com">
        <network host="mail.abc.com" port="25" userName="newsletter@abc.com" password="abc#!@"/>
      </smtp>
    </mailSettings>
  </system.net>
    <system.web>

    </system.web>
</configuration>

C# 中的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

public partial class SendMail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void cmdSend_Click(object sender, EventArgs e)
    {
        MailMessage mMailMessage = new MailMessage();

        // address of sender
        mMailMessage.From = new MailAddress(txtFrom.Text);
        // recipient address
        mMailMessage.To.Add(new MailAddress(txtTo.Text));
        // Check if the bcc value is empty
        if (txtBcc.Text != string.Empty)
        {
            // Set the Bcc address of the mail message
            mMailMessage.Bcc.Add(new MailAddress(txtBcc.Text));
        }

        // Check if the cc value is empty
        if (txtCc.Text != string.Empty)
        {
            // Set the CC address of the mail message
            mMailMessage.CC.Add(new MailAddress(txtCc.Text));
        }     // Set the subject of the mail message
        mMailMessage.Subject = txtSubject.Text;
        // Set the body of the mail message
        mMailMessage.Body = txtBody.Text;
        // Set the format of the mail message body as HTML
        mMailMessage.IsBodyHtml = true;
        // Set the priority of the mail message to normal
        mMailMessage.Priority = MailPriority.Normal;

        // Instantiate a new instance of SmtpClient
        SmtpClient mSmtpClient = new SmtpClient();


        // Send the mail message
        try
        {
            mSmtpClient.Send(mMailMessage);
        }
        catch (Exception ex)
        {
            ;//log error
            lblMessage.Text = ex.Message;
        }
        finally
        {
            mMailMessage.Dispose();
        }
    }
}

【问题讨论】:

  • 您的 SMTP 设置在哪里?
  • @Grant,只要设置存在于应用程序或网络配置中,它们就会被拾取。
  • @我已经添加了web.config
  • @MichaelPerrenoud 我收到“发送邮件失败”。留言
  • 查看事件查看器,看看您是否收到任何与之相关的系统或应用程序错误。

标签: c# smtp


【解决方案1】:

尝试使用 telnet 命令检查是否可以发送邮件。

开始-> 输入“telnet”:

open smtp.server.com 25
Helo smtp.server.com 
Mail from:yourAdress@server.com
Rcpt to:yourAdress@server.com

Data
Subject:The life
OMG
.
Quit

如果没有 telnet,请通过添加/删除 windows 功能添加它。见:http://technet.microsoft.com/en-us/library/cc771275(v=ws.10).aspx

【讨论】:

  • 我试过 Telnet 它说 C:\Windows\system32>telnet mail.abc.com 25 'telnet' 不是内部或外部命令、可运行程序或批处理文件。
  • 对不起...不是命令提示符。开始-> 输入 telnet
  • 安装 Telnet,我应该在服务器或任何客户端机器上检查 Telnet 命令吗?
  • 问题出在 SMTP 服务器中,现在已修复,感谢您的帮助 :)
【解决方案2】:

首先,我们没有指定 SMTP 服务器名称:

SmtpClient smtp = new SmtpClient(@"abc.company.com");

其次,在像上面的 sn-p 那样指定 SMTP 服务器名称后,确保防火墙或 Symantec(或类似的程序)没有阻止出站请求。

【讨论】:

  • 首先,这些设置是在网络配置中定义的——所以它们不需要传递到ctor
  • 正如 OP 所指出的,他的 SMTP 信息在配置中。见msdn.microsoft.com/en-us/library/w355a94k.aspx
  • 我阅读问题时不在那里。不过,防火墙或赛门铁克或类似的东西需要验证。
【解决方案3】:

在您的 try catch 块中,您需要处理所有异常。继续循环直到exception.innerexception 为空。更详细的错误信息将在邮件信息发送失败的内部异常中找到。外部异常和相应的消息将是通用的,没有那么有用。

一些示例可以在这里找到:

Best way to check for inner exception?

这里有一些伪代码

   while (ex != null)
    {
        //LogThisException(ex);
        ex = ex.InnerException;
    }

【讨论】:

  • 嗯,通常内部异常中有更好的消息。在这一行设置一个断点: lblMessage.Text = ex.Message;并继续扩展 ex.InnerException。
猜你喜欢
  • 2016-04-17
  • 2019-01-09
  • 1970-01-01
  • 1970-01-01
  • 2016-09-02
  • 2011-07-30
  • 1970-01-01
  • 2013-08-28
相关资源
最近更新 更多