Unlock File after sending as attachement with CDO
-
17-04-2021 - |
Frage
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
Lösung
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
Andere Tipps
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.
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.