Пример #1
0
 private void HandleError(Exception e, short errCode)
 {
     switch (errCode)
     {
         case 1:
             _session.HandleMailException(new GXMailException(e.Message, GXInternetConstants.MAIL_ErrorReceivingMessage));
             break;
         case 2:
             _session.HandleMailException(new GXMailException("Must login before sending message", GXInternetConstants.MAIL_CantLogin));
             break;
         case 3:
             _session.HandleMailException(new GXMailException(e.Message, GXInternetConstants.MAIL_AuthenticationError));
             break;
         case 5:
             _session.HandleMailException(new GXMailException("No new messages", GXInternetConstants.MAIL_NoMessages));
             break;
         case 6:
             _session.HandleMailException(new GXMailException("Folder not found", GXInternetConstants.MAIL_CantOpenFolder));
             break;
         case 7:
             _session.HandleMailException(new GXMailException("Mail Message with Id not found", GXInternetConstants.MAIL_EmailId_NotFound));
             break;
             
         default:
             _session.HandleMailException(new GXMailException(e.Message, GXInternetConstants.MAIL_MessageNotSent));
             break;
     }
 }
Пример #2
0
        public void Send(GXMailServiceSession sessionInfo, GXMailMessage gxmessage)
        {
            if (_service == null)
            {
                HandleError(2);
                return;
            }
            bool anyError = false;

            string fromAddress = (!string.IsNullOrEmpty(gxmessage.From.Address)) ? gxmessage.From.Address : _userName;

            EmailMessage email = new EmailMessage(_service);

            email.From = new EmailAddress(gxmessage.From.Name, fromAddress);

            SetRecipient(email.ToRecipients, gxmessage.To);
            SetRecipient(email.CcRecipients, gxmessage.CC);
            SetRecipient(email.BccRecipients, gxmessage.BCC);
            SetRecipient(email.ReplyTo, gxmessage.ReplyTo);

            email.Subject = gxmessage.Subject;
            if (string.IsNullOrEmpty(gxmessage.HTMLText))
            {
                email.Body = new MessageBody(BodyType.Text, gxmessage.Text);
            }
            else
            {
                email.Body = new MessageBody(BodyType.HTML, gxmessage.HTMLText);
            }

            foreach (var attach in gxmessage.Attachments)
            {
                string attachFilePath = attach.Trim();
                if (Path.IsPathRooted(attachFilePath))
                {
                    attachFilePath = Path.Combine(_attachDir, attach);
                }
                try
                {
                    email.Attachments.AddFileAttachment(attachFilePath);
                }
                catch (FileNotFoundException)
                {
                    anyError = true;
                    sessionInfo.HandleMailException(new GXMailException("Can't find " + attachFilePath, GXInternetConstants.MAIL_InvalidAttachment));
                }
                catch (Exception e)
                {
                    anyError = true;
                    sessionInfo.HandleMailException(new GXMailException(e.Message, GXInternetConstants.MAIL_InvalidAttachment));
                }
            }

            if (!anyError)
            {
                try
                {
                    email.SendAndSaveCopy(WellKnownFolderName.SentItems);
				}
                catch (Exception e)
                {
                    sessionInfo.HandleMailException(new GXMailException(e.Message, MailConstants.MAIL_MessageNotSent));
                }
            }
        }