0

可能重复:
由于目标机器主动拒绝,无法建立连接?

我在 Asp.net 中发送邮件时遇到严重问题,如果有人知道,请告诉我解决方案。这是我发送邮件的代码

MailMessage mm = new MailMessage();

        SmtpClient smtp = new SmtpClient();

        mm.From = new MailAddress(txtfrom.Text);

        mm.To.Add(new MailAddress(txtto.Text));



        mm.Subject = txtsubject.Text;

        mm.Body = txtmessage.Text;

        mm.IsBodyHtml = true;

        smtp.Host = "smtp.gmail.com"; //You can add this in the webconfig

        smtp.EnableSsl = true;

        System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();

        NetworkCred.UserName = "mymail@gmail.com";

        NetworkCred.Password = "my password";
        smtp.UseDefaultCredentials = true;

        smtp.Credentials = NetworkCred;

        smtp.Port = 587; //this is Gmail port for e-mail

        smtp.Send(mm);//send an e-mail

问题是当我点击发送按钮时它显示以下错误

“无法建立连接,因为目标机器主动拒绝它 173.194.79.109:587”

请告诉我解决方案..

4

5 回答 5

0

您需要将属性设置为falseUseDefaultCredentials

smtp.UseDefaultCredentials = false;
于 2012-05-15T09:43:47.800 回答
0

该错误消息通常意味着防火墙阻止了您的连接。不要使用您的机器名称"173.194.79.109:587",而是尝试使用“ localhost”。

于 2012-05-15T09:44:01.660 回答
0

这是我检查发送邮件的功能......它工作正常。

`

    private static bool testsendemail(MailMessage messageBody)
    {

        try

        {

        MailMessage message1 = new MailMessage();

        SmtpClient smtpClient = new SmtpClient();

        string msg = string.Empty;

        MailAddress fromAddress = new MailAddress("FromMail@Test.com");
        message1.From = fromAddress;
        message1.To.Add("ToMail@Test1.com");
        message1.Subject = "This is Test mail";
        message1.IsBodyHtml = true;
        message1.Body ="You can write your body here"+messageBody;
        smtpClient.Host = "smtp.mail.yahoo.com"; // We use yahoo as our smtp client
        smtpClient.Port = 587;
        smtpClient.EnableSsl = false;
        smtpClient.UseDefaultCredentials = true;
        smtpClient.Credentials = new  System.Net.NetworkCredential("SenderMail@yahoo.com", "YourPassword");

        smtpClient.Send(message1);
    }
    catch
    {
        return false;
    }
    return true;

}`           

谢谢你。

于 2012-05-15T10:41:28.943 回答
0

这就是我使用它的方式。

SmtpClient gglClient = new SmtpClient("173.194.67.108", 587);
gglClient.UseDefaultCredentials = false;
gglClient.DeliveryMethod = SmtpDeliveryMethod.Network;
gglClient.Credentials = new NetworkCredential("username@gmail.com", "password");
gglClient.EnableSsl = true;

MailMessage msg = new MailMessage("", email);
msg.Subject = "";
msg.Body = @" ";

msg.IsBodyHtml = false;
msg.BodyEncoding = UTF8Encoding.UTF8;

gglClient.Timeout = 8000;
ServicePointManager.ServerCertificateValidationCallback =
    delegate(object s, X509Certificate certificate, X509Chain chain,
        SslPolicyErrors sslPolicyErrors) { return true; };

gglClient.Send(msg);
于 2012-05-15T09:55:32.223 回答
0
    protected void SendMail_Click(object sender, EventArgs e)
    {
        var fromAddress = new MailAddress(fromid.Text, fromname.Text);
        var toAddress = new MailAddress(toid.Text, toname.Text);
        string fromPassword = pswd.Text;
        string subject = subjectbox.Text;
         string body = bodybox.Text;

        var smtp = new SmtpClient
        {
            Host = "smtp.gmail.com",
            Port = 587,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
            Timeout = 20000
        };
        using (var message = new MailMessage(fromAddress, toAddress)
        {
            Subject = subject,
            Body = body,
            IsBodyHtml = false

        })
        {
            smtp.Send(message);
        }

    }
于 2012-05-15T10:18:08.680 回答