0

在这里,我处于一个奇怪的境地。当我从本地主机发送电子邮件时,它工作正常,但是当我将页面上传到服务器并尝试发送电子邮件时,我收到以下错误

SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应是:5.5.1 Authentication Required

我在标签中显示错误消息以用于测试目的,并且我知道尝试也放错了位置,我稍后会设置它。

我正在使用的代码是

 if (Page.IsValid)
    {
try
            {
            StringBuilder message = new StringBuilder();
            message.Append("Hello My Name is ");
            message.Append(txtName.Text);
            message.AppendLine();
            message.AppendLine("My Contact Number " + txtContactNumber.Text.ToString());
            message.AppendLine();
            message.AppendLine();
            message.AppendLine("My Email Id Is " + txtFromEmailAddress.Text.ToString());
            message.AppendLine();
            message.Append(txtEmailBody.Text);


        MailMessage mailMessage = new MailMessage("xxx@gmail.com", "yyy@gmail.com");
        mailMessage.Subject = "New Client Query";
        mailMessage.Body = message.ToString();

        SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 25);
        //smtpClient.UseDefaultCredentials = true;
        smtpClient.Credentials = new System.Net.NetworkCredential()
        {
            UserName = "xxx@gmail.com",
            Password = "password"
        };
        smtpClient.EnableSsl = true;

            smtpClient.Send(mailMessage);


        txtContactNumber.Text = "";
        txtFromEmailAddress.Text = "";
        txtName.Text = "";
        txtEmailBody.Text = "";
        lblEmailStatus.Text = "Email Sent Successfully.";
        lblEmailStatus.ForeColor = System.Drawing.Color.Yellow; 
        }
        catch(Exception ex)
        {
            lblEmailStatus.Text = ex.Message + " <br> " + ex.Source;
        }
    }
    else
    {

        lblEmailStatus.Text = "Error!   Email Not Sent ";
        lblEmailStatus.ForeColor = System.Drawing.Color.Yellow;
    }

我已经用谷歌搜索了几个小时并检查了该站点上的链接,但我仍然无法弄清楚。

现在,如果有人有任何解决方案/提示,我在这里请求你们。

4

3 回答 3

1

我终于找到了原因。

几天前我发送电子邮件的电子邮件有点被黑了,出于安全原因,谷歌团队将我的电子邮件标记为不安全。我更改了电子邮件地址,它工作正常谢谢大家。

于 2014-01-30T20:42:07.263 回答
1

尝试这个

    public string SendEmailTest(String EmailMessage, String FromMail, String MailPassword, String MailServer, String To, String CC, String BCC, String DisplayName, String Subject, String Attachment)
    {
        try
        {
            SmtpClient smtpClient = new SmtpClient();

            MailMessage message = new MailMessage();

            MailAddress fromAddress;

            fromAddress = new MailAddress(FromMail);

            smtpClient.Host = MailServer;
            smtpClient.Port = 25;

            System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(FromMail, MailPassword);
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials = SMTPUserInfo;

            message.From = fromAddress;

            message.To.Add(new MailAddress(To, DisplayName));
            if (CC != "")
                message.CC.Add(new MailAddress(CC, DisplayName));
            if (BCC != "")
                message.Bcc.Add(new MailAddress(BCC, DisplayName));

            message.Subject = Subject;

            message.IsBodyHtml = true;
            message.Body = EmailMessage;

            if (Attachment != "")
                message.Attachments.Add(new Attachment(Attachment));

            message.Priority = MailPriority.High;

            smtpClient.Send(message);
            return "SendEmail";
        }
        catch (Exception ex)
        {
            return "Email :" + ex;
        }

    }
于 2013-10-03T11:04:11.357 回答
0

根据Google Gmail 文档,看来端口应该是 587 而不是 25。我发现其他一些问题似乎与此处此处相关。

SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
client.UseDefaultCredentials = false;
于 2013-10-03T10:56:41.520 回答