【问题标题】:Email is successfully sending when ever i close my application每当我关闭我的应用程序时,电子邮件都会成功发送
【发布时间】:2014-11-28 16:23:53
【问题描述】:

我正在发送一封包含一些附件和一些基本信息的电子邮件。所以每件事都在发送 完美。但是当我检查我的收件箱邮件时,还没有, 所以我试着关闭我的申请表。所以那个时候邮件来了。

累了很多时间,我发现每当我发送邮件并关闭我的整个表单时,收件人邮箱都会收到电子邮件。任何想法!!!

代码sn-p:

 SmtpClient smtp = new SmtpClient(smtpserver, 25);

            MailMessage msg = new MailMessage();

            msg.From = new MailAddress(email_From);
            msg.To.Add(email_Recipient);

            msg.IsBodyHtml = true;
            msg.Subject = email_Subject;

            ///Attachment's and Body


            try
            {
                    _f3.ShowDialog();
                    smtp.Send(msg);
                    MessageBox.Show("Email Successfully Sent!!!", "Mail!!!.");
                    Environment.Exit(0);// -->> if i keep this mail is going.. else i have to close
                    // my application to receive mail
            }

            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }   

【问题讨论】:

  • 我之前遇到过这个问题。如果我没记错的话,我必须在完成后处理 SmtpClient。
  • 您的目标是什么版本的 .NET? (VS2010中的C#编译器支持多目标)见stackoverflow.com/a/2782559/103167
  • @BenVoigt 我试过这也行不通...using (smtp as IDisposable) { smtp.Send(msg); }
  • 理论...由于您的评论看起来像是在附加文件,请确保您处理掉所有可能的东西。很可能是某些东西使文件处于锁定状态,并且消息在被释放之前无法发送——这就是它在应用程序关闭后发送的原因。处置! :D

标签: c# visual-studio-2010 visual-studio email sendmail


【解决方案1】:

正如@TyCobb 指出的那样,.NET 4+ 中的SmtpClient 实现了IDisposable。你不打电话给smtp.Dispose(),就是违反了它的合同。 using 块通常是实现此目的最方便的方法。

【讨论】:

  • Error System.Net.Mail.SmtpClient' does not contain a definition for 'Dispose' and no extension method 'Dispose' accepting a first argument of type 'System.Net.Mail.SmtpClient' could be found (are you missing a using directive or an assembly reference?)
  • @StacyKebler:MSDN says it is there。并且还解释了为什么调用 Dispose 很重要。如果它不像 MSDN 所说的那样公开,那么试试((IDisposable)smtp).Dispose()。或者就像我说的那样选择using。
  • unable to cast object of type' system.net.mail.smptclient' to type ' system.idisposable'
  • @StacyKebler 正如 Ben 在问题 cmets 中所问的那样,您正在运行什么版本的 .NET? SmtpClient 直到 4.0 才实现 IDisposable。 MailMessage 在 2.0 中实现了 IDisposable,但我怀疑调用它是否会真正解决您的问题。不过,这只是尝试。
【解决方案2】:

所以,有很多方法可以发送电子邮件。 Using 语句是一种非常流行的语句。

这是另一个发送邮件方法的链接: Send e-mail via SMTP using C#

我想你的问题是,你想做什么?

这是大型电子邮件发件人吗? 您可能想要使用邮件发送服务,例如发送网格。

如果这是一个你自己的测试项目,它会发送低优先级的管理员电子邮件或其他东西,你可能会接受上述内容。

无论如何,我都会有一个类设置,您可以在其中传递以下内容:To、From、Subject、HTMLMessageContent、PlainTextMessageContent,至少。

然后,此方法将处理从您的应用程序发送的所有电子邮件。下面是一个很好的起点。

public bool SendMail(string from, string to, string subject, string htmlContent, string plainContent)
{
    //Email sending code - could be replaced by 3rd party mail sending API, etc.
    MailMessage mail = new MailMessage(from, to);
    SmtpClient client = new SmtpClient();
    client.Port = 25;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.UseDefaultCredentials = false;
    client.Host = "smtp.google.com";
    mail.Subject = subject;
    mail.Body = htmlContent;
    client.Send(mail);
}

然后您将通过

调用上述内容
//... code
SendMail(email_From, email_Recipient, email_Subject, email_body_HTML, email_body, plaintext);
//... code

显然,您可以执行诸如返回表示成功的布尔值、错误处理/捕获/记录、附件等操作。

试试这个,看看它是否是一个好的起点。

或者,以下链接应该有一些用处: http://csharp.net-informations.com/communications/csharp-smtp-mail.htm Sending email in .NET through Gmail

【讨论】:

    【解决方案3】:

    尝试重新排列您的代码:

     smtp.Send(msg);
    _f3.ShowDialog();
    

    将发送电子邮件的行放在显示模式对话框的行上方。在此表单关闭之前,您的代码将不会继续运行。

    【讨论】:

    • 我尝试了同样的效果.. 直到我关闭应用程序才会收到邮件
    【解决方案4】:

    尝试使用“使用块”,也可以使用 Show() 插入 ShowDialog()

    using(SmtpClient smtp = new SmtpClient(smtpserver, 25))
    {
    
            MailMessage msg = new MailMessage();
    
            msg.From = new MailAddress(email_From);
            msg.To.Add(email_Recipient);
    
            msg.IsBodyHtml = true;
            msg.Subject = email_Subject;
    
            ///Attachment's and Body
    
    
            try
            {
                    _f3.ShowDialog(); //until you dont close the dialog, it will not send the msg. Maybe _f3.Show() solve your problem
                    smtp.Send(msg);
                    MessageBox.Show("Email Successfully Sent!!!", "Mail!!!.");
                    //Environment.Exit(0);// -->> if i keep this mail is going.. else i have to close
                    // my application to receive mail
            }
    
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }   
    }
    

    【讨论】:

    • Error 'System.Net.Mail.SmtpClient': type used in a using statement must be implicitly convertible to 'System.IDisposable'
    • 当我使用using(SmtpClient smtp = new SmtpClient(smtpserver, 25)) 时,我发现了上述错误
    • 如果您使用的是 FrameWork
    • 圣母啊!!我去了项目属性-> 应用程序选项卡。并将框架从 2.0 更改为版本 4.0.. 不,我的 smtp.Dispose(); 工作正常.. 我的意思是当我更改框架时.. 这会影响我的应用程序.. 因为我需要这个应用程序在 Windows XP 上工作专业操作系统
    • 另外,我想指出这个应用程序在服务器的两台不同的计算机上运行。所以有什么问题吗。我可以看到另一个名为 .Net framework 4 Client profile.. 的框架工作。我需要选择它吗.. 如果我在网络域上运行此应用程序..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-10
    • 1970-01-01
    • 1970-01-01
    • 2010-12-20
    相关资源
    最近更新 更多