MailKit显示已发送但未收到的邮件 - .NET Core 1.1

时间:2017-07-18 16:08:53

标签: c# .net-core mailkit

VS2017中的C#代码显示邮件已成功发送。我可以从我的outlook帐户的已发信息框中验证电子邮件确实已成功发送。但是在我的Yahoo帐户上,即使超过一个小时,电子邮件也不存在。 问题:我的代码中缺少某些内容 - 我们如何才能使其有效? 注意:我已在Outlook的已发送框中验证了FromTo邮件地址均正确无误。我正在关注Technet article

static void Main(string[] args)
{
    try
    {
        //From Address
        string FromAddress = "myfromEmail@hotmail.com";
        string FromAdressTitle = "Email from ASP.NET Core 1.1";
        //To Address
        string ToAddress = "myToEmail@yahoo.com";
        string ToAdressTitle = "Microsoft ASP.NET Core";
        string Subject = "Hello World - Sending email using ASP.NET Core 1.1";
        string BodyContent = "ASP.NET Core was previously called ASP.NET 5. It was renamed in January 2016. It supports cross-platform frameworks ( Windows, Linux, Mac ) for building modern cloud-based internet-connected applications like IOT, web apps, and mobile back-end.";

        //Smtp Server
        string SmtpServer = "smtp.live.com";
        //Smtp Port Number
        int SmtpPortNumber = 587;

        var mimeMessage = new MimeMessage();
        mimeMessage.From.Add(new MailboxAddress(FromAdressTitle, FromAddress));
        mimeMessage.To.Add(new MailboxAddress(ToAdressTitle, ToAddress));
        mimeMessage.Subject = Subject;
        mimeMessage.Body = new TextPart("plain")
        {
            Text = BodyContent

        };

        using (var client = new SmtpClient())
        {

            client.Connect(SmtpServer, SmtpPortNumber, false);
            // Note: only needed if the SMTP server requires authentication
            // Error 5.5.1 Authentication 
            client.Authenticate(FromAddress, "myFromEmailpassword");
            client.Send(mimeMessage);
            Console.WriteLine("The mail has been sent successfully !!");
            Console.ReadLine();
            client.Disconnect(true);

        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

1 个答案:

答案 0 :(得分:1)

最后,一小时后,我收到了我的雅虎帐户上的电子邮件。不知道为什么雅虎需要花费一个多小时,但前景立即发送。感谢那些可能试图帮助但可能没有在代码中发现错误的人。

观察:在收到的电子邮件中,我可以在To字段中看到ToAdressTitleMicrosoft ASP.NET Core(正如上面代码所预期的那样)。但是在From字段中,我只看到FromAddress而不是FromAdressTitle作为Email from ASP.NET Core 1.1(如上面的代码所示)。我想知道为什么?