【问题标题】:mail was not sending邮件未发送
【发布时间】:2017-02-02 03:37:18
【问题描述】:
void sendMail(string invoiceNumber)
{
    try
    {
        MailMessage mail = new MailMessage();
        SmtpClient SmtpServer = new SmtpClient("Smtp.gmail.com");
        mail.From = new MailAddress("*******@gmail.com");
        mail.To.Add("******@gmail.com");
        mail.Subject = "Test Mail - 1";
        mail.Body = invoiceNumber;
        mail.Subject = "PDFs Attached";
        DirectoryInfo di = new DirectoryInfo(@"C:\Users\User\Desktop\test\");
        foreach (var file in di.GetFileSystemInfos("*.pdf*"))
            mail.Attachments.Add(new Attachment(file.FullName));
        SmtpServer.Port = 587;
        SmtpServer.Credentials = new System.Net.NetworkCredential("*******@gmail.com", "*********");
        SmtpServer.EnableSsl = true;
        SmtpServer.Send(mail);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

【问题讨论】:

  • 欢迎来到 SO。您应该阅读有关我们如何编写格式良好的问题的信息。 rene 已经提到的链接是一个非常好的开始。这次,我为您编写了代码。然而,这仍然不是一个问题。这只是带有 mail was not sending 的代码。你必须描述出了什么问题?到目前为止,您的发现是什么以及您遇到的问题。

标签: c# c#-4.0


【解决方案1】:

https://www.google.com/settings/security/lesssecureapps 使用此链接允许您的 gmail id 上安全性较低的应用程序...

void sendMail(string invoiceNumber)
{
    try
    {
        MailMessage mail = new MailMessage();
            mail.To.Add("recevermailid");
            mail.From = new MailAddress("sender id");
            mail.Subject = "Confirmation";
            string Body = "your message body";
            mail.Body = Body; 
            mail.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new System.Net.NetworkCredential
            ("sender email", "seneder email password");// Enter seders User name and password
            smtp.EnableSsl = true;
            smtp.Send(mail);

    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-06
    • 2015-02-02
    • 2016-03-21
    • 2019-10-24
    • 1970-01-01
    相关资源
    最近更新 更多