1

**它只是说“发送邮件失败”。不确定代码或 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();
        }
    }
}
4

3 回答 3

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

于 2013-04-03T19:29:34.853 回答
0

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

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

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

于 2013-04-03T19:24:00.553 回答
-1

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

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

检查内部异常的最佳方法?

这里有一些伪代码

   while (ex != null)
    {
        //LogThisException(ex);
        ex = ex.InnerException;
    }
于 2013-04-03T19:40:15.820 回答