Ejemplo n.º 1
0
        private System.String SendMailDotNetOpenMail(anmar.SharpWebMail.EmailServerInfo server)
        {
            System.String               message     = null;
            System.Text.Encoding        encoding    = (System.Text.Encoding)Application["sharpwebmail/send/message/charset"];
            DotNetOpenMail.EmailMessage mailMessage = new DotNetOpenMail.EmailMessage();
            mailMessage.HeaderCharSet = encoding;
            mailMessage.FromAddress   = new DotNetOpenMail.EmailAddress(this.GetFromAddress(), this.fromname.Value);
            System.String[] tokens = anmar.SharpMimeTools.ABNF.address_regex.Split(this.toemail.Value);
            foreach (System.String token in tokens)
            {
                if (anmar.SharpMimeTools.ABNF.address_regex.IsMatch(token))
                {
                    mailMessage.AddToAddress(new DotNetOpenMail.EmailAddress(token.Trim()));
                }
            }
            mailMessage.Subject = this.subject.Value.Trim();
            System.String format = Request.Form["format"];
            if (format != null && format.Equals("html"))
            {
                mailMessage.HtmlPart         = new DotNetOpenMail.HtmlAttachment(bodyStart + FCKEditor.Value + bodyEnd);
                mailMessage.HtmlPart.CharSet = encoding;
            }
            else
            {
                mailMessage.TextPart         = new DotNetOpenMail.TextAttachment(FCKEditor.Value);
                mailMessage.TextPart.CharSet = encoding;
            }

            if (this._headers != null)
            {
                // RFC 2822 3.6.4. Identification fields
                if (this._headers["Message-ID"] != null)
                {
                    mailMessage.AddCustomHeader("In-Reply-To", this._headers["Message-ID"]);
                    mailMessage.AddCustomHeader("References", this._headers["Message-ID"]);
                }
                if (this._headers["References"] != null)
                {
                    mailMessage.AddCustomHeader("References", System.String.Concat(this._headers["References"], " ", this._headers["Message-ID"]).Trim());
                }
                else if (this._headers["In-Reply-To"] != null && this._headers["In-Reply-To"].IndexOf('>') == this._headers["In-Reply-To"].LastIndexOf('>'))
                {
                    mailMessage.AddCustomHeader("References", System.String.Concat(this._headers["In-Reply-To"], " ", this._headers["Message-ID"]).Trim());
                }
            }
            mailMessage.XMailer = System.String.Concat(Application["product"], " ", Application["version"]);
            this.ProcessMessageAttachments(mailMessage);
            try {
                if (log.IsDebugEnabled)
                {
                    log.Debug(System.String.Concat("Sending message. engine: DotNetOpenMail , protocol: ", server.Protocol));
                }
                DotNetOpenMail.SmtpServer SmtpMail = new DotNetOpenMail.SmtpServer(server.Host, server.Port);
                if (server.Protocol.Equals(anmar.SharpWebMail.ServerProtocol.SmtpAuth))
                {
                    anmar.SharpWebMail.IEmailClient client = (anmar.SharpWebMail.IEmailClient)Session["client"];
                    SmtpMail.SmtpAuthToken = new DotNetOpenMail.SmtpAuth.SmtpAuthToken(client.UserName, client.Password);
                }
                mailMessage.Send(SmtpMail);
                SmtpMail = null;
                if (log.IsDebugEnabled)
                {
                    log.Debug("Message sent");
                }
            } catch (System.Exception e) {
                message = e.Message;
                if (log.IsErrorEnabled)
                {
                    log.Error(System.String.Concat("Error sending message. engine: DotNetOpenMail , protocol: ", server.Protocol), e);
                }
            }
            mailMessage = null;
            return(message);
        }
Ejemplo n.º 2
0
        public void Write(IMessage message, Stream stream)
        {
            var outMessage = new DotNetOpenMail.EmailMessage();

            // assuming this is right for unicode support?
            outMessage.HeaderEncoding = DotNetOpenMail.Encoding.EncodingType.QuotedPrintable;
            outMessage.HeaderCharSet  = Encoding.UTF8;

            if (message.HasSubject)
            {
                outMessage.Subject = message.Subject;
            }

            if (message.RecipientCount > 0)
            {
                foreach (var recipient in message.Recipients)
                {
                    var mailAddress =
                        new DotNetOpenMail.EmailAddress(
                            recipient.HasEmailAddress ? recipient.EmailAddress : " ", recipient.Name,
                            DotNetOpenMail.Encoding.EncodingType.QuotedPrintable, Encoding.UTF8
                            );

                    switch (recipient.RecipientType)
                    {
                    case RecipientType.mapi_to:
                        outMessage.AddToAddress(mailAddress);
                        //outMessage.To.Add(mailAddress);
                        break;

                    case RecipientType.mapi_cc:
                        outMessage.AddCcAddress(mailAddress);
                        //outMessage.CC.Add(mailAddress);
                        break;

                    case RecipientType.mapi_bcc:
                        outMessage.AddBccAddress(mailAddress);
                        //outMessage.Bcc.Add(mailAddress);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
            }
            if (message.AttachmentCount > 0)
            {
                foreach (var attachment in message.Attachments)
                {
                    var filename = attachment.Filename;

                    if (attachment.IsMessage)
                    {
                        var embeddedMessage = attachment.OpenAsMessage();
                        var messageWriter   = new MimeMessageWriter();
                        var msgStream       = new MemoryStream();
                        messageWriter.Write(embeddedMessage, msgStream);

                        //msgStream = new MemoryStream(msgStream.ToArray());
                        //.Flush();
                        //msgStream.Seek(0, SeekOrigin.Begin);

                        var messageAttachment =
                            new DotNetOpenMail.FileAttachment(msgStream.ToArray());
                        messageAttachment.ContentType = "message/rfc822";
                        messageAttachment.FileName    = filename + ".eml";
                        //new Attachment(msgStream, filename + ".eml", "message/rfc822");

                        outMessage.AddMixedAttachment(messageAttachment);
                        //outMessage.Attachments.Add(messageAttachment);
                    }
                    else
                    {
                        var fileAttachment =
                            new DotNetOpenMail.FileAttachment(attachment.Bytes);
                        fileAttachment.FileName    = filename;
                        fileAttachment.ContentType = "application/octet-stream";

                        //    new Attachment(new MemoryStream(attachment.Bytes), filename);
                        //outMessage.Attachments.Add(fileAttachment);
                        outMessage.AddMixedAttachment(fileAttachment);
                    }
                }
            }
            if (message.HasBody && message.HasHtmlBody)
            {
                //first we create the Plain Text part
                //var plainTextBody = AlternateView.CreateAlternateViewFromString(message.Body, null, "text/plain");
                //then we create the Html part
                //var htmlBody = AlternateView.CreateAlternateViewFromString(message.HtmlBody, null, "text/html");

                //outMessage.AlternateViews.Add(plainTextBody);
                //outMessage.AlternateViews.Add(htmlBody);
                outMessage.TextPart          = new DotNetOpenMail.TextAttachment(message.Body);
                outMessage.TextPart.CharSet  = Encoding.UTF8;
                outMessage.TextPart.Encoding = DotNetOpenMail.Encoding.EncodingType.QuotedPrintable;

                outMessage.HtmlPart          = new DotNetOpenMail.HtmlAttachment(Encoding.UTF8.GetString(message.HtmlBody));
                outMessage.HtmlPart.CharSet  = Encoding.UTF8;
                outMessage.HtmlPart.Encoding = DotNetOpenMail.Encoding.EncodingType.QuotedPrintable;
            }
            else if (message.HasBody)
            {
                //outMessage.Body = message.Body;
                //outMessage.BodyText = message.Body;
                outMessage.TextPart          = new DotNetOpenMail.TextAttachment(message.Body);
                outMessage.TextPart.CharSet  = Encoding.UTF8;
                outMessage.TextPart.Encoding = DotNetOpenMail.Encoding.EncodingType.QuotedPrintable;
            }
            else if (message.HasHtmlBody)
            {
#warning Encoding is not detected
                //outMessage.TextPart = new DotNetOpenMail.TextAttachment("");
                outMessage.HtmlPart          = new DotNetOpenMail.HtmlAttachment(Encoding.UTF8.GetString(message.HtmlBody));
                outMessage.HtmlPart.CharSet  = Encoding.UTF8;
                outMessage.HtmlPart.Encoding = DotNetOpenMail.Encoding.EncodingType.QuotedPrintable;


                //outMessage.Body = message.HtmlBody;
                //outMessage.IsBodyHtml = true;
            }
            // attempt to get sender details

            PropId senderDisplayNameProp      = 0x0C1A; // sender display name
            PropId senderEmailAddressProp     = 0x0C1F; // sender email address
            PropId senderDisplayNameProp_Alt  = 0x0042; // sender "representing" display name
            PropId senderEmailAddressProp_Alt = 0x0065; // sender "representing" email address

            string senderDisplayname = string.Empty;

            if (message.PropertyExists(senderDisplayNameProp))
            {
                senderDisplayname =
                    message.GetPropertyType(senderDisplayNameProp).Value == (ushort)PropertyType.KnownValue.prop_type_string
                    ? PropertyHelper.GetEncodedStringProperty(message.ReadProperty(senderDisplayNameProp), Encoding.Default)
                    : PropertyHelper.GetUnicodeStringProperty(message.ReadProperty(senderDisplayNameProp));
            }
            else if (message.PropertyExists(senderDisplayNameProp_Alt))
            {
                senderDisplayname =
                    message.GetPropertyType(senderDisplayNameProp_Alt).Value == (ushort)PropertyType.KnownValue.prop_type_string
                    ? PropertyHelper.GetEncodedStringProperty(message.ReadProperty(senderDisplayNameProp_Alt), Encoding.Default)
                    : PropertyHelper.GetUnicodeStringProperty(message.ReadProperty(senderDisplayNameProp_Alt));
            }

            string senderEmailAddress = string.Empty;
            if (message.PropertyExists(senderEmailAddressProp))
            {
                senderEmailAddress =
                    message.GetPropertyType(senderEmailAddressProp).Value == (ushort)PropertyType.KnownValue.prop_type_string
                    ? PropertyHelper.GetEncodedStringProperty(message.ReadProperty(senderEmailAddressProp), Encoding.Default)
                    : PropertyHelper.GetUnicodeStringProperty(message.ReadProperty(senderEmailAddressProp));
            }
            else if (message.PropertyExists(senderEmailAddressProp_Alt))
            {
                senderEmailAddress =
                    message.GetPropertyType(senderEmailAddressProp_Alt).Value == (ushort)PropertyType.KnownValue.prop_type_string
                    ? PropertyHelper.GetEncodedStringProperty(message.ReadProperty(senderEmailAddressProp_Alt), Encoding.Default)
                    : PropertyHelper.GetUnicodeStringProperty(message.ReadProperty(senderEmailAddressProp_Alt));
            }

            var senderAddress =
                new DotNetOpenMail.EmailAddress(string.IsNullOrEmpty(senderEmailAddress) ? " " : senderEmailAddress, senderDisplayname, DotNetOpenMail.Encoding.EncodingType.QuotedPrintable, Encoding.UTF8);
            //new MailAddress(string.IsNullOrEmpty(senderEmailAddress) ? "*****@*****.**" : senderEmailAddress, senderDisplayname);
            //if (senderAddress.Address == "*****@*****.**")
            //    SetEmptyAddress(senderAddress);
            outMessage.FromAddress = senderAddress;

            //outMessage.Sender = senderAddress;
            //outMessage.From = senderAddress;

            // TODO: pull this prop from IMessage instance... -th
            //outMessage.ReplyTo = senderAddress;

            // for now, this is pointless, since the implementation of MailMessage.Send
            // overwrites this with current time... <sigh> -th
            PropId sentTimeProp = 0x0039; // "client submit" time

            if (message.PropertyExists(sentTimeProp))
            {
                var sentTime = (DateTime)PropertyHelper.GetTimeProperty(message.ReadProperty(sentTimeProp));

                outMessage.AddCustomHeader("Date", sentTime.ToString("ddd, dd MMM yyyy HH:mm:ss zzz", System.Globalization.CultureInfo.InvariantCulture).Remove(29, 1));
                //MimeDateTime(sentTime));
                //outMessage.Headers.Add("Date", MimeDateTime(sentTime));
            }
            using (var sw = new StreamWriter(stream))
                sw.Write(outMessage.ToDataString());

            //outMessage.Save(stream);
        }
Ejemplo n.º 3
0
        private System.String SendMailDotNetOpenMail( anmar.SharpWebMail.EmailServerInfo server )
        {
            System.String message = null;
            DotNetOpenMail.EmailMessage mailMessage = new DotNetOpenMail.EmailMessage();
            mailMessage.FromAddress = new DotNetOpenMail.EmailAddress(this.GetFromAddress(), this.fromname.Value);
            System.String[] tokens = anmar.SharpMimeTools.ABNF.address_regex.Split(this.toemail.Value);
            foreach ( System.String token in tokens ) {
                if ( anmar.SharpMimeTools.ABNF.address_regex.IsMatch(token ) )
                    mailMessage.AddToAddress (new DotNetOpenMail.EmailAddress(token.Trim()));
            }
            mailMessage.Subject = this.subject.Value.Trim();
            System.String format = Request.Form["format"];
            if ( format!=null && format.Equals("html") ) {
                mailMessage.HtmlPart = new DotNetOpenMail.HtmlAttachment(bodyStart + FCKEditor.Value + bodyEnd);
            } else {
                mailMessage.TextPart = new DotNetOpenMail.TextAttachment(FCKEditor.Value);
            }

            if ( this._headers != null ) {
                // RFC 2822 3.6.4. Identification fields
                if ( this._headers["Message-ID"]!=null ) {
                    mailMessage.AddCustomHeader("In-Reply-To", this._headers["Message-ID"]);
                    mailMessage.AddCustomHeader("References", this._headers["Message-ID"]);
                }
                if ( this._headers["References"]!=null ) {
                    mailMessage.AddCustomHeader("References", System.String.Concat (this._headers["References"], " ", this._headers["Message-ID"]).Trim());
                } else if ( this._headers["In-Reply-To"]!=null && this._headers["In-Reply-To"].IndexOf('>')==this._headers["In-Reply-To"].LastIndexOf('>') ) {
                    mailMessage.AddCustomHeader("References", System.String.Concat (this._headers["In-Reply-To"], " ", this._headers["Message-ID"]).Trim());
                }
            }
            mailMessage.XMailer = System.String.Concat (Application["product"], " ", Application["version"]);
            this.ProcessMessageAttachments(mailMessage);
            try {
                if ( log.IsDebugEnabled) log.Error ( "Sending message" );
                DotNetOpenMail.SmtpServer SmtpMail = new DotNetOpenMail.SmtpServer(server.Host, server.Port);
                if ( server.Protocol.Equals(anmar.SharpWebMail.ServerProtocol.SmtpAuth) ) {
                    anmar.SharpWebMail.IEmailClient client = (anmar.SharpWebMail.IEmailClient)Session["client"];
                    SmtpMail.SmtpAuthToken = new DotNetOpenMail.SmtpAuth.SmtpAuthToken(client.UserName, client.Password);
                }
                mailMessage.Send(SmtpMail);
                SmtpMail=null;
                if ( log.IsDebugEnabled) log.Error ( "Message sent" );
            } catch (System.Exception e) {
                message = e.Message;
                if ( log.IsErrorEnabled ) log.Error ( "Error sending message", e );
            }
            mailMessage = null;
            return message;
        }