使用 CDO 作为附件发送后解锁文件

发布于 2024-11-17 17:41:55 字数 174 浏览 4 评论 0原文

您好,我遇到以下问题:

我正在使用 CDO 发送带有附件的电子邮件(我需要这样做,因为 system.Net.Mail 无法在 465 端口上使用隐式 SSL)。 问题是发送后附件仍处于锁定状态。 我怎样才能解锁它?

我正在使用 c# 进行编程。

谢谢你的回答

皮尔卡洛

Hi I have the following Problem:

I'm sending e-mail with attachement with CDO (I need to do this because system.Net.Mail dont work with implicit SSL on 465 port).
The problem is that attached file after sending remain locked.
How can I Unlock its ?

I'm programming using c#.

Thank you for your answer

Piercarlo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初见你 2024-11-24 17:41:55

我自己解决了

在 CDO.Message.Send() 之后需要这个;

GC.Collect();
GC.WaitForPendingFinalizers();

希望这对以下完整代码中的其他一些有用

[SqlFunction()]
public static SqlString SendFromMittente(SqlString Messaggio, SqlString eMailDestinatario, SqlString From, SqlString SmtpHost, SqlString Utente, SqlString Password, SqlString Oggetto, SqlString Allegati, SqlBoolean SSL, SqlInt32 SmtpPort)
{
    try
    {
        CDO.Message oMsg = new CDO.Message();
        CDO.IConfiguration iConfg;
        iConfg = oMsg.Configuration;
        ADODB.Fields oFields;
        oFields = iConfg.Fields;
        ADODB.Field oField;
        oField = oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"];
        oField.Value = CDO.CdoSendUsing.cdoSendUsingPort;
        oField = oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"];
        oField.Value = SmtpHost.Value;
        oField = oFields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"];
        oField.Value = SSL.Value.ToString();
        oField = oFields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"];
        oField.Value = SmtpPort.Value.ToString();
        oField = oFields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"];//Use 0 for anonymous 1 for authenticate
        oField.Value = CDO.CdoProtocolsAuthentication.cdoBasic;
        oField = oFields["http://schemas.microsoft.com/cdo/configuration/sendusername"];
        oField.Value = Utente.Value;
        oField = oFields["http://schemas.microsoft.com/cdo/configuration/sendpassword"];
        oField.Value = Password.Value;
        oFields.Update();
        oMsg.Subject = Oggetto.Value;
        oMsg.From = From.Value;
        oMsg.To = eMailDestinatario.Value;
        oMsg.TextBody = Messaggio.Value;
        if (!string.IsNullOrEmpty(Allegati.Value))
        {
            char[] sep = { ',' };
            string[] aryAllegati = Allegati.Value.Split(sep);
            foreach (string file in aryAllegati)
            {
                oMsg.AddAttachment(file);

            }
        }
        oMsg.Send();
        GC.Collect();
        GC.WaitForPendingFinalizers();
        Marshal.FinalReleaseComObject(oMsg);
        return new SqlString("true");
    }
    catch (Exception ex)
    {
        return new SqlString(ex.ToString());
    }
}

请注意,代码是为 SQL 程序集

Piercarlo编写的

I resolved from myself

After CDO.Message.Send() need this;

GC.Collect();
GC.WaitForPendingFinalizers();

Hope that will be usefull for some other else following completee code

[SqlFunction()]
public static SqlString SendFromMittente(SqlString Messaggio, SqlString eMailDestinatario, SqlString From, SqlString SmtpHost, SqlString Utente, SqlString Password, SqlString Oggetto, SqlString Allegati, SqlBoolean SSL, SqlInt32 SmtpPort)
{
    try
    {
        CDO.Message oMsg = new CDO.Message();
        CDO.IConfiguration iConfg;
        iConfg = oMsg.Configuration;
        ADODB.Fields oFields;
        oFields = iConfg.Fields;
        ADODB.Field oField;
        oField = oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"];
        oField.Value = CDO.CdoSendUsing.cdoSendUsingPort;
        oField = oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"];
        oField.Value = SmtpHost.Value;
        oField = oFields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"];
        oField.Value = SSL.Value.ToString();
        oField = oFields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"];
        oField.Value = SmtpPort.Value.ToString();
        oField = oFields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"];//Use 0 for anonymous 1 for authenticate
        oField.Value = CDO.CdoProtocolsAuthentication.cdoBasic;
        oField = oFields["http://schemas.microsoft.com/cdo/configuration/sendusername"];
        oField.Value = Utente.Value;
        oField = oFields["http://schemas.microsoft.com/cdo/configuration/sendpassword"];
        oField.Value = Password.Value;
        oFields.Update();
        oMsg.Subject = Oggetto.Value;
        oMsg.From = From.Value;
        oMsg.To = eMailDestinatario.Value;
        oMsg.TextBody = Messaggio.Value;
        if (!string.IsNullOrEmpty(Allegati.Value))
        {
            char[] sep = { ',' };
            string[] aryAllegati = Allegati.Value.Split(sep);
            foreach (string file in aryAllegati)
            {
                oMsg.AddAttachment(file);

            }
        }
        oMsg.Send();
        GC.Collect();
        GC.WaitForPendingFinalizers();
        Marshal.FinalReleaseComObject(oMsg);
        return new SqlString("true");
    }
    catch (Exception ex)
    {
        return new SqlString(ex.ToString());
    }
}

Note that code is write for SQL assembly

Piercarlo

遮了一弯 2024-11-24 17:41:55

由于某种原因,强制垃圾收集对我不起作用。我通过手动将附件作为字节数组添加到 CDO.Message 对象解决了这个问题,如下所述 此处

For some reason forcing the garbage collection didn't work for me. I solved this problem by manually adding the attachment to the CDO.Message object as a byte array as explained here.

胡大本事 2024-11-24 17:41:55

我刚刚遇到了同样的问题,但我不想依赖垃圾收集。这是一个有效的 C++ 解决方案。在其后添加您的发送:

CDO::IBodyParts *bodyparts;
imsg->get_Attachments(&bodyparts);
bodyparts->DeleteAll();
imsg->Release();

之后,您的文件删除就可以正常工作了。

I just had the same problem but I don't want to rely on garbage collection. Here is a working C++ solution. Add your send after it:

CDO::IBodyParts *bodyparts;
imsg->get_Attachments(&bodyparts);
bodyparts->DeleteAll();
imsg->Release();

After that, your file deletion will work fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文