如何在C#中发送Asp.net中的邮件

时间:2012-05-15 09:37:11

标签: asp.net email

  

可能重复:
  No connection could be made because the target machine actively refused it?

我在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“

请告诉我解决方案..

5 个答案:

答案 0 :(得分:0)

您需要设置 false UseDefaultCredentials属性。

smtp.UseDefaultCredentials = false;

答案 1 :(得分:0)

该错误消息通常表示防火墙阻止了您的连接。请尝试使用“"173.194.79.109:587"”,而不是使用您的计算机名称localhost

答案 2 :(得分: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);

答案 3 :(得分: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);
        }

    }

答案 4 :(得分: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;

}`           

谢谢。