与CDO一起作为附件发送后解锁文件

时间:2011-06-28 08:35:46

标签: c# cdo.message

您好我有以下问题:

我正在发送带有CDO附件的电子邮件(我需要这样做,因为system.Net.Mail不能在465端口上使用隐式SSL)。 问题是发送后附加的文件保持锁定状态。 我该如何解锁?

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

感谢您的回答

Piercarlo

3 个答案:

答案 0 :(得分:0)

我自己解决了

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

答案 1 :(得分:0)

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

答案 2 :(得分:-1)

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

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

之后,您的文件删除工作正常。