Asp.net SMTP Gmail失败

时间:2018-08-14 06:12:41

标签: c# asp.net email smtp gmail

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

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

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

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

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);
} 

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

  

发送邮件失败。

请帮我找出问题所在。

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

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

以下是“发送”按钮的代码:

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");
                }
            }
        }

    }

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

尝试一下。我已经更新了我的软件,看起来工作正常。 另外,请确保您的“字符串”与send中声明的顺序相同。

public string send(string gmailid, string password, string toemail, string subject, string body)
    {
        string msg = null;
        MailMessage mail = new MailMessage();
        mail.To.Add(toemail);
        mail.From = new MailAddress(gmailid);
        mail.Subject = subject;
        mail.Body = body;
        mail.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.Credentials = new System.Net.NetworkCredential
                    (gmailid, password);
        smtp.EnableSsl = true;
        try
        {
            smtp.Send(mail);
            msg = "Your Message has been sent!";
        }
        catch (Exception)
        {
            msg = "Your Message could NOT be sent.";
        }
        return msg;
    }

答案 2 :(得分:0)

我想在评论中加上它,但要长一点。我使用smpt.gmail.com多年了,没有出现这样的问题。唯一的区别是,我习惯将邮件设置放置到web.configapp.config

  <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network">
        <network defaultCredentials="false" host="smtp.gmail.com" password="MYPASSWORD" port="587" userName="MYACCOUNTEMAIL"/>
      </smtp>
    </mailSettings>
</system.net>

并像这样发送电子邮件

var cli = new System.Net.Mail.SmtpClient();
cli.EnableSsl = true; //for some reason .config settings don't work here
//no more cli settings
var msg = new System.Net.Mail.MailMessage();
// do all other things
//finally
cli.Send(msg);

对我有用。

相关问题