在MVC中发送电子邮件时出错

时间:2018-01-17 15:48:47

标签: c# model-view-controller

当用户登录到面临此错误的网站时

SMTP服务器需要安全连接,或者客户端未经过身份验证。服务器响应为:5.7.1需要身份验证

帮帮我 我没有在Web.Config中为SMTP写任何东西

// Plug in your email service here to send an email.
        MailMessage mail = new MailMessage();
        SmtpClient SmtpServer = new SmtpClient("smtp.mail.yahoo.com");
        mail.From = new MailAddress("elahi2mahdi@yahoo.com", "Hello");
        mail.To.Add(message.Destination);
        mail.Subject = message.Subject;
        mail.Body = message.Body;
        mail.IsBodyHtml = true;

        //System.Net.Mail.Attachment attachment;
        // attachment = new System.Net.Mail.Attachment("c:/textfile.txt");
        // mail.Attachments.Add(attachment);

        SmtpServer.Port =587;
        SmtpServer.Credentials = new System.Net.NetworkCredential("elahi2mahdi@yahoo.com", "password");
        SmtpServer.EnableSsl = true;
       SmtpServer.UseDefaultCredentials = true;

        // SmtpServer.Send(mail);
        return SmtpServer.SendMailAsync(mail);

2 个答案:

答案 0 :(得分:0)

我觉得你对于向雅虎提出简单的smtp请求的解决方案有不必要的复杂性。根本不需要SMTP服务器。请查看以下文章:Send Email from Yahoo

基本上(从文章中提取)这是你应该做的(信用转到原始海报):

using System.Net;
using System.Net.Mail;

string smtpAddress = "smtp.mail.yahoo.com";
int portNumber = 587;
bool enableSSL = true;

string emailFrom = "email@yahoo.com";
string password = "abcdefg";
string emailTo = "someone@domain.com";
string subject = "Hello";
string body = "Hello, I'm just writing this to say Hi!";

using (MailMessage mail = new MailMessage())
{
    mail.From = new MailAddress(emailFrom);
    mail.To.Add(emailTo);
    mail.Subject = subject;
    mail.Body = body;
    mail.IsBodyHtml = true;
    // Can set to false, if you are sending pure text.

    mail.Attachments.Add(new Attachment("C:\\SomeFile.txt"));
    mail.Attachments.Add(new Attachment("C:\\SomeZip.zip"));

    using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
    {
        smtp.Credentials = new NetworkCredential(emailFrom, password);
        smtp.EnableSsl = enableSSL;
        smtp.Send(mail);
    }
}

答案 1 :(得分:0)

显示错误此代码的任务已取消

                    await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>");

代码

string smtpAddress = "smtp.mail.yahoo.com";
        int portNumber = 587;
        bool enableSSL = true;

        string emailFrom = "elahi2mahdi@yahoo.com";
        string password = "password";
        string emailTo =message.Destination;
        string subject = message.Subject;
        string body = message.Body;

        using (MailMessage mail = new MailMessage())
        {
            mail.From = new MailAddress(emailFrom);
            mail.To.Add(emailTo);
            mail.Subject = subject;
            mail.Body = body;
            mail.IsBodyHtml = true;
            // Can set to false, if you are sending pure text.

            //mail.Attachments.Add(new Attachment("C:\\SomeFile.txt"));
            //mail.Attachments.Add(new Attachment("C:\\SomeZip.zip"));

            using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
            {
                smtp.Credentials = new NetworkCredential(emailFrom, password);
                smtp.EnableSsl = enableSSL;
               return smtp.SendMailAsync(mail);
            }
        }