本文介绍了Asp.net SMTP Gmail失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在写我重复问题的评论之前,我想说我已经阅读并尝试了所有可能的方法.

before writing a comment that I am duplicating questions, I want to say that i've read and tried everything that could be possible.

我正在尝试通过gmail smtp发送电子邮件.我使用ASP.NET WebForm,也尝试在.net 4.0的服务器上部署网站.

I am trying to send an email by gmail smtp. I use ASP.NET WebForm, also tried to deploy website on the server with .net 4.0.

我的Gmail帐户已通过启用以下各项进行配置:POP,IMAP协议.另外,安全性较低的应用已打开,并且禁用了两个因素身份验证.

My Gmail Account has been configured by enabling: POP, IMAP protocols. Also less secure app was Turned ON and two factor authentication is disabled.

这是我发送电子邮件的代码:

Here is my code for sending emails:

try
{
    SmtpClient smtp = new SmtpClient("smtp.gmail.com", 465);
    smtp.EnableSsl = true;
    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtp.UseDefaultCredentials = false;
    smtp.Credentials = new NetworkCredential("email", "password");
    MailMessage masage = new MailMessage();
    masage.To.Add("email");
    masage.From = new MailAddress("email");
    masage.Subject = "subject";
    masage.Body = "Hello";
    smtp.Send(masage);
}
catch (Exception eX)
{
    throw new Exception("cDocument: Error occurred trying to Create an Email" + Environment.NewLine + eX.Message);
}

问题是,当我尝试发送电子邮件时,出现错误

So problem is, when I am trying to send an email, I have an error

请帮我找出问题所在.

更新:我尝试使用所有端口465和587.仅在按下发送按钮后才会出现按摩.我使用System.Net.Mail;

Update:I tried to use all ports 465 and 587.Massage appears only after pressing button for sending.I use System.Net.Mail;

为什么这个问题是唯一的:因为我尝试使用所有端口,并且拥有所有发送权限,但是仍然有错误.

Why this question is unique:Because I tried to use all ports and I have all permissions for sending, but still have an error.

以下是发送"按钮的代码:

Here is a code for Sending button:

public void btn_submit_Click(object sender, EventArgs e)
    {
        //comment
        string comment = id_comment.Text.Replace("'", "\"");

        //comment end
        bool check = id_check_terms.Checked;
        string Username = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
        string Username_new = Username.Replace("APAC\\", "");
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        con.Open();
        //username
        var Fullname = "Select FirstName From UserData where (Employee='"+Username_new+"')";
        SqlCommand Fullname_command = new SqlCommand(Fullname, con);
        object Fullname_object = Fullname_command.ExecuteScalar();
        string Fullname_converted = Convert.ToString(Fullname_object);
        //usernitemame
        var items = "Select item From Basket where (Username='" + Username_new + "')";
        SqlCommand items_command = new SqlCommand(items, con);
        object items_object = items_command.ExecuteScalar();
        string items_converted = Convert.ToString(items_object);
        List<String> columnData = new List<String>();
        List<String> emaildata = new List<String>();
        List<String> emaildatacc = new List<String>();

        using (con)
        {
            //email primary
            string email = "Select Email From MBAccessoriesEmail where TOorCC='1'";
            using (SqlCommand command = new SqlCommand(email, con))
            {
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        emaildata.Add(reader.GetString(0));
                    }
                }
            }
            string email_primary = string.Join(", ", emaildata);
            //endemail
            //email cc
            string emailcc = "Select Email From MBAccessoriesEmail where TOorCC='2'";
            using (SqlCommand command = new SqlCommand(emailcc, con))
            {
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        emaildatacc.Add(reader.GetString(0));
                    }
                }
            }
            string email_cc = string.Join("; ", emaildatacc);
            //endemail


            this.GridView1.Columns[6].Visible = false;
            if (check == true)
            {
                if (GridView1.Rows.Count == 0)
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "<script>alert('You dont have anything in your basket.');</script>", false);
                }else
                {

                    try {
                        MailMessage mail = new MailMessage();
                        mail.To.Add("email");
                        mail.From = new MailAddress("email");
                        mail.Subject = "subject";
                        mail.Body = "Hello";
                        mail.IsBodyHtml = true;
                        SmtpClient smtp = new SmtpClient();
                        smtp.Host = "smtp.gmail.com";
                        smtp.Port = 587;
                        smtp.Credentials = new System.Net.NetworkCredential("email", "password");
                        smtp.EnableSsl = true;
                        smtp.Send(mail);
                        }
                    catch (Exception eX)
                    {
                        throw new Exception("cDocument: Error occurred trying to Create an Email" + Environment.NewLine + eX.Message);
                    }

                    cmd.CommandText = "Delete Basket where Username='" + Username_new + "'";
                    cmd.ExecuteNonQuery();

                    con.Close();

                    Response.Redirect("ShoppingCart.aspx");
                }
            }
        }

    }

推荐答案

Google提供了高度安全的验证,可避免从其他设备发送电子邮件,而您需要向该Google帐户授予权限.

Google provides high security validations, to avoid send email from other device your need to give permission to that google account.

从以前用于访问的其他设备上访问 https://g.co/allowaccess 您的Google帐户,然后按照说明进行操作.

Go to https://g.co/allowaccess from a different device you have previously used to access your Google account and follow the instructions.

这篇关于Asp.net SMTP Gmail失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 23:39