0

您好我有以下问题:

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

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

谢谢您的回答

皮尔卡洛

4

3 回答 3

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 程序集编写的

皮尔卡洛

于 2011-06-29T07:09:48.123 回答
0

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

于 2012-01-19T15:18:28.837 回答
-1

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

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

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

于 2018-01-17T13:46:10.113 回答