简体   繁体   English

通过Gmail发送电子邮件会引发错误

[英]Sending email through Gmail throws an error

Im working on an web application i got stuck between this email sending code.I am working on contact page where anyone can send you email as you guys know that.This code in working fine but i can't pass through this error 我正在Web应用程序上工作,我陷入了此电子邮件发送代码之间。我正在联系页面上,任何人都可以向您发送电子邮件,就像你们知道的那样。此代码工作正常,但我无法通过此错误

在此处输入图片说明

as i said im working with Asp.Net Mvc so here is my POST controller im using gmail account so that it won't conflict between any mail services. 正如我所说的即时消息与Asp.Net Mvc一起使用,因此这是我使用gmail帐户的POST控制器即时消息,因此它不会在任何邮件服务之间发生冲突。

  public ActionResult sendemail()
    {
        return View();
    }

    [HttpPost]
    public ActionResult sendemail(string to, string from, string subject, string body, string pwd)
    {
        SmtpClient client = new SmtpClient();
        client.Host = "smtp.gmail.com";
        client.Port = 587;
        client.EnableSsl = true;

        client.UseDefaultCredentials = true;
        client.Credentials = new NetworkCredential("faseehyasin12@gmail.com", pwd);
        client.DeliveryMethod = SmtpDeliveryMethod.Network;

        MailMessage mail = new MailMessage();
        mail.To.Add(to);
        mail.From = new MailAddress(from);
        mail.Subject = subject;
        mail.Body = body;
        try
        {
            client.Send(mail);
            Response.Write("ok");
            return View();
        }
        catch(Exception e)
        {
            throw e;
        }

    }

and here is my view and i want to ask do i really need a password to send email to someone in this code ? 这是我的观点,我想问一下我是否真的需要密码才能使用此代码向某人发送电子邮件? 在此处输入图片说明

and my GET controller is empty with only written code is return view() so i won't bother to take ss for that. 而且我的GET控制器是空的,只有编写的代码是return view(),所以我不会为此而烦恼。 i have also allowed "less secure app" but it still gives me this error. 我也允许“安全性较低的应用程序”,但它仍然给我这个错误。 Need Help 需要帮忙

First go to https://myaccount.google.com/lesssecureapps and change status as open. 首先访问https://myaccount.google.com/lesssecureapps并将状态更改为“打开”。 And go to https://accounts.google.com/b/0/displayunlockcaptcha and click Continue button. 然后转到https://accounts.google.com/b/0/displayunlockcaptcha ,然后单击继续按钮。

Please try with following code. 请尝试以下代码。

string host = "smtp.gmail.com";
int port = 587;
bool ssl = true;
string fromAddress = "faseehyasin12@gmail.com";
string fromPassword = "your password here";
using (var mail = new MailMessage())
{
    string subject = "Test";
    string body = "Mail test";
    mail.From = new MailAddress(fromAddress);
    mail.Subject = subject;
    mail.IsBodyHtml = true;
    mail.Body = body;
    mail.To.Add("mail@domain.com");
    using (var smtpServer = new SmtpClient(host,port))
    {
        smtpServer.UseDefaultCredentials = false;
        smtpServer.Credentials = new System.Net.NetworkCredential(fromAddress, fromPassword);
        smtpServer.EnableSsl = ssl;
        smtpServer.Send(mail);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM