![]() |
#7 |
Участник
|
Добавлю свои 5 копеек, все то же самое но с использование .Net, глюков пока не замечено.
X++: public static boolean sendMailNet( str mailTo, str mailToCC = '', str mailSubject, str mailBody, str mailServer, str mailFrom, str userName, str userPass, boolean isBodyHTML = false, int port = 25, boolean highMailImportance = false, str attachmentFile = '', str mailToBCC = '', container conAttachFileName = conNull() //отправка нескольких файлов ) { System.Net.Mail.SmtpClient smtpClient; System.Net.Mail.MailMessage mailMessage; System.Net.Mail.MailAddress mailAddressFrom; System.Net.NetworkCredential networkCredential; System.Net.Mail.AttachmentCollection attachmentCollection; System.Net.Mail.Attachment attachment; int countMails; boolean fileExists; int countAttachFiles; int n; str tmpFileName; str strInfo; boolean res = false; ; new InteropPermission(InteropKind::CLRInterop).assert(); if (!mailServer) { error('Не указан почтовый сервер!'); return false; } if (!mailFrom) { error('Не указан обратный адрес отправителя!'); return false; } try { mailAddressFrom = new System.Net.Mail.MailAddress(mailFrom); } catch { error("Адрес отправителя не задан или некорректен"); return false; } mailMessage = new System.Net.Mail.MailMessage(); mailMessage.set_From(mailAddressFrom); if(mailTo != "") { countMails += CDOMailNet::createMailAddressCollection(mailTo, mailMessage.get_To()); //создание коллекции // адресатов (ниже по тексту код) } if(mailToCC != "") { countMails += CDOMailNet::createMailAddressCollection(mailToCC, mailMessage.get_CC()); //создание коллекции // адресатов (ниже по тексту код) } if(mailToBCC != "") { countMails += CDOMailNet::createMailAddressCollection(mailToBCC, mailMessage.get_Bcc()); //создание коллекции // адресатов (ниже по тексту код) } if(countMails == 0) { error("Не заданы адреса получателей"); return false; } mailMessage.set_Subject(mailSubject); if(highMailImportance) { mailMessage.set_Priority(System.Net.Mail.MailPriority::High); } if(attachmentFile != "") { conAttachFileName += attachmentFile; } countAttachFiles = conLen(conAttachFileName); if(countAttachFiles > 0) { attachmentCollection = mailMessage.get_Attachments(); for(n=1 ; n <= countAttachFiles; n++) { tmpFileName = conpeek(conAttachFileName, n); if(tmpFileName != "") { fileExists = System.IO.File::Exists(tmpFileName); if(fileExists == true) { try { attachment = new System.Net.Mail.Attachment(tmpFileName); attachmentCollection.Add(attachment); } catch( Exception::CLRError) { error(strFmt("Ошибка отправки почты: %1", AifUtil::getClrErrorMessage())); return false; } } else { strInfo = strFmt(@"Не найден файл для отправки: %1", tmpFileName); if(isBodyHTML) mailBody = strInfo + @"<br /><br />" + mailBody; else mailBody = strInfo + "\n\n" + mailBody; error(strInfo); } } } } mailMessage.set_IsBodyHtml(isBodyHTML); mailMessage.set_Body(mailBody); try { smtpClient = new System.Net.Mail.SmtpClient(mailServer, port); if(userName != "") { smtpClient.set_UseDefaultCredentials(false); networkCredential = new System.Net.NetworkCredential(userName, userPass); smtpClient.set_Credentials(networkCredential); } smtpClient.Send(mailMessage); res = true; } catch( Exception::CLRError) { error(strFmt("Ошибка отправки почты: %1", AifUtil::getClrErrorMessage())); } if(attachmentCollection != null) attachmentCollection.Dispose(); if(mailMessage != null) mailMessage.Dispose(); return res; } //создание коллекции адресатов protected static int createMailAddressCollection(str _strEmails, System.Net.Mail.MailAddressCollection _mailAddressCollection) { List listEmails; ListEnumerator le; eMail eMail; int countMails = 0; ; if(_strEmails && _mailAddressCollection != null) { listEmails = strSplit(_strEmails, ";, "); le = listEmails.getEnumerator(); while (le.moveNext()) { eMail = le.current(); if(eMail != "") { try { _mailAddressCollection.Add(eMail); countMails ++; } catch( Exception::CLRError) { warning(strFmt("Адрес: %1 будет пропущен, причина: %2", eMail, AifUtil::getClrErrorMessage())); } } } } return countMails; } |
|
|
За это сообщение автора поблагодарили: gl00mie (5), AR® (1). |