无法连接到SMTP服务器

时间:2011-05-21 09:05:50

标签: c# asp.net smtp mail-server

//aspx.cs file    
protected void SendButton_Click(object sender, EventArgs e)
{
    MailHelper.SendMailMessage("naresh7590@gmail.com", "cheekucheekuarena@gmail.com", "", "", "Sample Subject", "Sample body of text for mail message");
}


//MailHelper.cs
 using System.Net.Mail;

public class MailHelper
{
    public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
    {
        MailMessage mMailMessage = new MailMessage();
        mMailMessage.From = new MailAddress(from);
        mMailMessage.To.Add(new MailAddress(to));


        if ((bcc != null) && (bcc != string.Empty))
        {
            mMailMessage.Bcc.Add(new MailAddress(bcc));
        }

        if ((cc != null) && (cc != string.Empty))
        {
            mMailMessage.CC.Add(new MailAddress(cc));
        }      

        mMailMessage.Subject = subject;
        mMailMessage.Body = body;

        mMailMessage.IsBodyHtml = true;
        mMailMessage.Priority = MailPriority.Normal;

        SmtpClient mSmtpClient = new SmtpClient("127.0.0.1");
        mSmtpClient.Send(mMailMessage);//Error here
    }
}

我在mSmtpClient.Send(mMailMessage)收到错误。

你可以帮帮我吗?

2 个答案:

答案 0 :(得分:0)

鉴于来自地址是gmail帐户,我假设您正在尝试通过GMAIL发送电子邮件,如果不是,则本地计算机中的SMTP服务器出现问题。 我希望这会有所帮助:

    public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
    {
        //Creating Mail Message
        var mMailMessage = new MailMessage(from, to, subject, body);
        if (!String.IsNullOrEmpty(bcc))
            mMailMessage.Bcc.Add(bcc);
        if (!String.IsNullOrEmpty(cc))
            mMailMessage.CC.Add(cc);
        mMailMessage.IsBodyHtml = true;
        mMailMessage.Priority = MailPriority.Normal;

        //Sending Mail Message through GMAIL
        var mSmtpClient = new SmtpClient("smtp.gmail.com", 587);
        mSmtpClient.EnableSsl = true;
        mSmtpClient.Credentials = new System.Net.NetworkCredential(from, "GMAIL Password");
        mSmtpClient.Send(mMailMessage);
    }

答案 1 :(得分:-1)

你的头衔说明了一切。服务器仅允许TLS连接。 所有解释都在这里:http://tools.ietf.org/html/rfc2487