예제 #1
0
 private static void AddHeader(GXMailMessage msg, string key, string value)
 {
     if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
     {
         msg.Headers[key] = value;
     }
 }
예제 #2
0
        private void ProcessMailAttachments(GXMailMessage gxmessage, List <MessagePart> attachs)
        {
            if (attachs == null || attachs.Count == 0)
            {
                return;
            }

            if (DownloadAttachments)
            {
                if (!Directory.Exists(AttachDir))
                {
                    Directory.CreateDirectory(AttachDir);
                }

                foreach (var attach in attachs)
                {
                    string attachName = FixFileName(AttachDir, attach.FileName);
                    if (!string.IsNullOrEmpty(attach.ContentId) && attach.ContentDisposition != null && attach.ContentDisposition.Inline)
                    {
                        string cid = "cid:" + attach.ContentId;
                        attachName         = String.Format("{0}_{1}", attach.ContentId, attachName);
                        gxmessage.HTMLText = gxmessage.HTMLText.Replace(cid, attachName);
                    }
                    try {
                        attach.Save(new FileInfo(Path.Combine(AttachDir, attachName)));
                        gxmessage.Attachments.Add(attachName);
                    }
                    catch (Exception e)
                    {
                        LogError("Could not add Attachment", "Failed to save attachment", MailConstants.MAIL_InvalidAttachment, e);
                    }
                }
            }
        }
예제 #3
0
 public short Send(GXMailMessage msg)
 {
     ResetError();
     session.Send(this, msg);
     return(errorCode);
 }
예제 #4
0
 public short Receive(GXMailMessage msg)
 {
     ResetError();
     session.Receive(this, msg);
     return(errorCode);
 }
예제 #5
0
        public void Send(GXSMTPSession session, GXMailMessage msg)
        {
            if (client != null)
            {
                using (MailMessage mail = new MailMessage())
                {
                    string senderAddress = (!String.IsNullOrEmpty(msg.From.Address) ? msg.From.Address : session.Sender.Address);
                    string senderName    = (!String.IsNullOrEmpty(msg.From.Name) ? msg.From.Name : session.Sender.Name);
                    if (String.IsNullOrEmpty(senderAddress))
                    {
                        session.HandleMailException(new GXMailException("SmtpSession Sender Address must be specified", GXInternetConstants.MAIL_InvalidSenderAddress));
                        return;
                    }
                    GXLogging.Debug(log, "Sending Message");
                    mail.From            = new MailAddress(senderAddress, senderName);
                    mail.SubjectEncoding = GetEncoding();
                    mail.Subject         = msg.Subject;

                    if (!String.IsNullOrEmpty(msg.HTMLText))
                    {
                        mail.Body       = msg.HTMLText;
                        mail.IsBodyHtml = true;
                    }
                    else
                    {
                        mail.Body = msg.Text;
                    }

                    foreach (string key in msg.Headers.Keys)
                    {
                        mail.Headers.Add(key, (string)msg.Headers[key]);
                    }

                    try
                    {
                        SendAllRecipients(mail.To, msg.To);
                        SendAllRecipients(mail.CC, msg.CC);
                        SendAllRecipients(mail.Bcc, msg.BCC);
                        SendAllRecipients(mail.ReplyToList, msg.ReplyTo);
                    }
                    catch (Exception re)
                    {
                        session.HandleMailException(new GXMailException(re.Message, GXInternetConstants.MAIL_InvalidRecipient));
                    }

                    foreach (var item in msg.Attachments)
                    {
                        string fileName = item;
                        try
                        {
                            fileName = System.IO.Path.Combine(attachDir, item);
                            mail.Attachments.Add(new Attachment(fileName));
                        }
                        catch (FileNotFoundException)
                        {
                            session.HandleMailException(new GXMailException("Can't find " + fileName, GXInternetConstants.MAIL_InvalidAttachment));
                        }
                        catch (Exception e)
                        {
                            session.HandleMailException(new GXMailException(e.Message, GXInternetConstants.MAIL_InvalidAttachment));
                        }
                    }

                    try
                    {
                        client.Send(mail);
                        GXLogging.Debug(log, "Email successfully sent");
                    }
                    catch (SmtpFailedRecipientsException e)
                    {
                        session.HandleMailException(new GXMailException(e.Message, GXInternetConstants.MAIL_InvalidRecipient));
                    }
                    catch (SmtpException e)
                    {
                        HandleError(session, e);
                    }
                }
            }
            else
            {
                session.HandleMailException(new GXMailException("Must login before sending message", GXInternetConstants.MAIL_CantLogin));
            }
        }
예제 #6
0
 public short Delete(GXMailMessage msg)
 {
     ResetError();
     session.Delete(this, msg);
     return(errorCode);
 }
예제 #7
0
 public short GetMailMessage(string msgId, bool fetchEntireMsg, GXMailMessage msg)
 {
     ResetError();
     session.GetMailMessage(this, msgId, fetchEntireMsg, msg);
     return(errorCode);
 }
예제 #8
0
 public short MarkAs(GXMailMessage msg, bool isRead)
 {
     ResetError();
     session.MarkAs(this, msg, isRead);
     return(errorCode);
 }
예제 #9
0
        public void Receive(GXPOP3Session sessionInfo, GXMailMessage gxmessage)
        {
            if (client == null)
            {
                LogError("Login Error", "Must login", MailConstants.MAIL_CantLogin);
                return;
            }

            if (lastReadMessage == count)
            {
                LogDebug("No messages to receive", "No messages to receive", MailConstants.MAIL_NoMessages);
                return;
            }
            try {
                if (count > lastReadMessage)
                {
                    Message m = null;
                    try
                    {
                        m = client.GetMessage(++lastReadMessage);
                    }
                    catch (Exception e)
                    {
                        LogError("Receive message error", e.Message, MailConstants.MAIL_ServerRepliedErr, e);
                    }

                    if (m != null)
                    {
                        MailMessage msg;
                        try
                        {
                            msg = m.ToMailMessage();
                        }
                        catch (ArgumentException ae)
                        {
                            GXLogging.Error(log, "Receive message error " + ae.Message + " subject:" + m.Headers.Subject, ae);
                            PropertyInfo subjectProp = m.Headers.GetType().GetProperty("Subject");
                            string       subject     = m.Headers.Subject;
                            if (HasCROrLF(subject))
                            {
                                subjectProp.SetValue(m.Headers, subject.Replace('\r', ' ').Replace('\n', ' '));
                                GXLogging.Warn(log, "Replaced CR and LF in subject " + m.Headers.Subject);
                            }
                            msg = m.ToMailMessage();
                        }
                        using (msg)
                        {
                            gxmessage.From = new GXMailRecipient(msg.From.DisplayName, msg.From.Address);
                            SetRecipient(gxmessage.To, msg.To);
                            SetRecipient(gxmessage.CC, msg.CC);
                            gxmessage.Subject = msg.Subject;
                            if (msg.IsBodyHtml)
                            {
                                gxmessage.HTMLText = msg.Body;
                                MessagePart plainText = m.FindFirstPlainTextVersion();
                                if (plainText != null)
                                {
                                    gxmessage.Text += plainText.GetBodyAsText();
                                }
                            }
                            else
                            {
                                gxmessage.Text = msg.Body;
                            }
                            if (msg.ReplyToList != null && msg.ReplyToList.Count > 0)
                            {
                                SetRecipient(gxmessage.ReplyTo, msg.ReplyToList);
                            }

                            gxmessage.DateSent = m.Headers.DateSent;
                            if (gxmessage.DateSent.Kind == DateTimeKind.Utc && GeneXus.Application.GxContext.Current != null)
                            {
                                gxmessage.DateSent = DateTimeUtil.FromTimeZone(m.Headers.DateSent, "Etc/UTC", GeneXus.Application.GxContext.Current);
                            }
                            gxmessage.DateReceived = GeneXus.Mail.Internals.Pop3.MailMessage.GetMessageDate(m.Headers.Date);
                            AddHeader(gxmessage, "DispositionNotificationTo", m.Headers.DispositionNotificationTo.ToString());
                            ProcessMailAttachments(gxmessage, m.FindAllAttachments());
                        }
                    }
                }
            }catch (Exception e)
            {
                LogError("Receive message error", e.Message, MailConstants.MAIL_ServerRepliedErr, e);
            }
        }