简体   繁体   English

无法使用Gmail发送电子邮件

[英]Unable to send email using Gmail

I am trying to send email using gmail from my mvc application. 我正在尝试使用我的mvc应用程序中的gmail发送电子邮件。 I am getting the following exception 我收到以下异常

Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. 无法从传输连接读取数据:远程主机强制关闭现有连接。

here is the code for sending email 这是发送电子邮件的代码

string tn = "EmailVerification.htm";
string fileName = Path.GetFileName(tn);
string templatePath = Path.Combine(Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/EmailTemplates")), fileName);
  System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();

            mailMessage.From = new MailAddress(EmailFrom, "Email");
            mailMessage.To.Add(new MailAddress(To));
            mailMessage.Subject = "subject";
            mailMessage.IsBodyHtml = true;
            MailDefinition mailDef = new MailDefinition();
            mailDef.BodyFileName = templatePath;
            mailDef.IsBodyHtml = true;
            mailDef.Subject = "mailDefsubject";
            mailDef.From = mailMessage.From.ToString();

            ListDictionary replacements = new ListDictionary();

            replacements.Add("<%FIRSTNAME%>", FirstName);
            replacements.Add("<%USERNAME%>", "asd");
            replacements.Add("<%VERIFICATIONLINK%>", VerificationLink);     
            replacements.Add("<%WEBSITE%>", "http://www.mysite.com");

            MailMessage msgHtml = mailDef.CreateMailMessage(To, replacements, new System.Web.UI.Control());
            AlternateView htmlView = AlternateView.CreateAlternateViewFromString(msgHtml.Body, null, "text/html");

            AddLinkedResources(templatePath, ref htmlView);

            mailMessage.AlternateViews.Add(htmlView);

            SmtpClient smtpClient = new SmtpClient();
            Object userState = mailMessage;
            smtpClient.Credentials = new System.Net.NetworkCredential("myaddress@gmail.com", "1234567");
            smtpClient.EnableSsl = true;
            smtpClient.Host = "smtp.gmail.com";
            smtpClient.Port = 587;

            //Attach event handler for async callback
            smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
            try
            {               

                smtpClient.Send(mailMessage);
                EmailStatus = true;
            }
            catch (SmtpException smtpEx)
            {
                EmailStatus = false;

            }
            catch (Exception ex)
            {
                //HttpContext.Current.Response.Write(ex.InnerException);
                EmailStatus = false;

            }

in the web.config i have 在web.config我有

<system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="noreply@asd.com">
        <network host="smtp.gmail.com" port="587"
                                       userName="asd@gmail.com"
                                       password="asd" />
      </smtp>
    </mailSettings>
  </system.net>

Try setting: 尝试设置:

smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential("myaddress@gmail.com", "1234567");

Also you seem to be subscribing to the SendCompleted event but this event is intended to be used when sending an email asynchronously ( SendAsync method), so you could get rid of it. 此外,您似乎订阅了SendCompleted事件,但此事件旨在用于异步发送电子邮件( SendAsync方法),因此您可以摆脱它。 The Send method that you are using is blocking. 您正在使用的Send方法是阻止。

You may also checkout the following answer . 您也可以查看以下答案

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

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