C#smtp不想发送电子邮件

时间:2019-10-18 17:06:22

标签: c# smtp

发送电子邮件时,出现异常: 发送邮件失败。 我已将主机设置为“ smtp.gmail.com”,并将端口设置为587,但它不起作用。 代码:

public void SendActivationLink(string email)
    {
        using (MailMessage mail = new MailMessage())
        {

            SmtpClient smtpClient = new SmtpClient();
            smtpClient.UseDefaultCredentials = false;
            smtpClient.EnableSsl = true;
            smtpClient.Port = 587;
            MailMessage message = new MailMessage();
            MailAddress from = new MailAddress("myemail@gmail.com", "Voc");

            message.From = from;
            message.To.Add(email);
            message.Subject = "Title";
            message.Body = "Jello"; 
            smtpClient.Host = "smtp.gmail.com"; 
            smtpClient.UseDefaultCredentials = false;
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.Credentials = new System.Net.NetworkCredential("myemail@gmail.com", "mypassword");
            try
            {
                smtpClient.SendAsync(message, email);
            }
            catch (SmtpException ex)
            {

                throw new ApplicationException("exeption" + ex.Message);

            }


        }

0 个答案:

没有答案