邮件发送失败

本文关键字:失败 | 更新日期: 2023-09-27 18:06:17

我正在尝试制作一个电子邮件页面部分,我在这个论坛上找到了一篇文章和一个答案,我在下面使用了我的代码,但它仍然说发送邮件失败,我几乎不知道为什么?我需要帮助找到一个补救措施,为什么我的代码下面不能发送电子邮件。以下是我的代码供您参考。请建议…谢谢。

protected void Button1_Click(object sender, EventArgs e)
{
SmtpClient client = new SmtpClient();
        client.Host = "smtp.live.com"; //Im not sure about this,I just copy it from the article
        client.Port = 4548; //This is my ASP.Net Development Server Port,Im not sure also if this is correct.
        client.Credentials = new System.Net.NetworkCredential(
            @"email_account",
            @"email_password"); //Im not sure about this code if this correct, I just copy it

        client.EnableSsl = true;
        // create message
        MailMessage message = new MailMessage();
        message.From = new MailAddress(TextBox4.Text, "Mackmellow"); //Textbox4 is my email address
        message.To.Add(new MailAddress(TextBox1.Text, "Mackmellow")); // Textbox1 is the email add I want to send
        message.Subject = TextBox2.Text; //Textbox2 is my subject
        message.Body = TextBox3.Text; // Textbox3 is my message
        message.BodyEncoding = System.Text.Encoding.UTF8;
        message.IsBodyHtml = true;
        message.SubjectEncoding = System.Text.Encoding.UTF8;

        // send message
        try
        {
            client.Send(message);

        }
        catch (SmtpException ex)
        {
            Response.Write(ex.Message);
        }
        finally
        {
            // Clean up.
            message.Dispose();
        }

请纠正我的代码,告诉我我错过了什么?

邮件发送失败

不能使用开发服务器端口发送邮件。

您必须使用邮件服务器的smtp服务器端口。

对于smtp.live.com,您应该使用端口25或587

对于下面的代码,指定有效的登录详细信息(电子邮件/密码)

  client.Credentials = new System.Net.NetworkCredential(
            @"email_account",
            @"email_password");

只提供G-mail帐户Id。然后检查一下。我认为SmtpClient不接受任何其他域名邮件地址。因此,您可以只输入gmail地址,然后检查它。

编辑

看这个例子和其他答案。

从Asp发送邮件错误。Net/C #

如何在asp.net中发送电子邮件到任何目的地例如yahoo,gmail,hotmail等c#代码

试试这个

SmtpClient SmtpServer = new SmtpClient("smtp.live.com");
    var mail = new MailMessage();
    mail.From = new MailAddress("email@hotmail.com");
    mail.To.Add("ToGmail.com");
    mail.Subject = "Your Sub";
    mail.IsBodyHtml = true;
    string htmlBody;
    htmlBody = "HTML code";
    mail.Body = htmlBody;
    SmtpServer.Port = 587;
    SmtpServer.UseDefaultCredentials = false;
    SmtpServer.Credentials = new System.Net.NetworkCredential("email@hotmail.com", "YourPassword");
    SmtpServer.EnableSsl = true;
    SmtpServer.Send(mail);