在C#中使用SMTP发送电子邮件

时间:2013-04-03 19:17:55

标签: c# smtp

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

3 个答案:

答案 0 :(得分:1)

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

开始 - &GT;输入“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

答案 1 :(得分:0)

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

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

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

答案 2 :(得分:-1)

在try catch块中,您需要完成所有异常。保持循环,直到exception.innerexception为空。将在邮件消息无法发送的内部异常中找到更详细的错误消息。外部异常和相应的消息将是通用的,并没有那么有用。

有些样本可以在这里找到:

Best way to check for inner exception?

这里有一些Psuedo代码

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