예제 #1
0
        public void SendMail()
        {
            var recipients = new List <MapiRecipDesc>();

            To.ForEach(email => recipients.Add(new MapiRecipDesc {
                recipClass = RecipClass.To, name = email
            }));
            CC.ForEach(email => recipients.Add(new MapiRecipDesc {
                recipClass = RecipClass.CC, name = email
            }));
            BCC.ForEach(email => recipients.Add(new MapiRecipDesc {
                recipClass = RecipClass.BCC, name = email
            }));

            var msg = new MapiMessage {
                subject = Subject, noteText = Body,
                recips  = GetRecipientsData(recipients), recipCount = recipients.Count,
                files   = GetAttachmentsData(Attachments), fileCount = Attachments.Count,
            };

            int result = MAPISendMail(IntPtr.Zero, IntPtr.Zero, msg, MAPI_LOGON_UI | MAPI_DIALOG, 0);

            if (result > 1)
            {
                throw new Exception("Failed to send mail: {0}.".Fmt(GetErrorMessage(result)));
            }
            Cleanup(msg);
        }
예제 #2
0
        /// <summary>
        /// SMTP邮件Sender的类的封装
        /// </summary>
        /// <param name="smtpServer">smtp服务地址</param>
        /// <param name="port">端口号</param>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="enableSSL">是否采用ssl的方式</param>
        public override bool Send(string smtpServer, int port, string userName, string password, bool enableSSL)
        {
            bool isSended = false;

            try
            {
                MailMessage message = new MailMessage();
                To.ForEach(email =>
                {
                    if (email.IsValidEmail())
                    {
                        message.To.Add(email);
                    }
                });
                message.Sender = new MailAddress(userName, userName);
                if (CC != null && CC.Any())
                {
                    CC.ForEach(email =>
                    {
                        if (email.IsValidEmail())
                        {
                            message.CC.Add(email);
                        }
                    });
                }
                message.Subject = Encoding.Default.GetString(Encoding.Default.GetBytes(Subject));

                message.From = DisplayName.IsBlank() ? new MailAddress(From) : new MailAddress(From, Encoding.Default.GetString(Encoding.Default.GetBytes(DisplayName)));

                message.Body                        = Encoding.Default.GetString(Encoding.Default.GetBytes(Body));
                message.BodyEncoding                = Encoding.GetEncoding("GBK");
                message.HeadersEncoding             = Encoding.UTF8;
                message.SubjectEncoding             = Encoding.UTF8;
                message.IsBodyHtml                  = true;
                message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                message.ReplyToList.Add(new MailAddress(From));

                SmtpClient smtp = new SmtpClient(smtpServer, port)
                {
                    DeliveryMethod        = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = false,
                    Credentials           = new NetworkCredential(userName, password),
                    Timeout   = 30000,
                    EnableSsl = enableSSL
                };

                smtp.SendCompleted += smtp_SendCompleted;
                smtp.Send(message);
                isSended = true;
            }
            catch (Exception ex)
            {
                Logger.Log("邮件发送失败:", ex);
            }
            return(isSended);
        }
예제 #3
0
        /// <summary>
        /// Converts <see cref="MailMessage"/> to <see cref="System.Net.Mail.MailMessage"/>.
        /// </summary>
        /// <returns>A <see cref="System.Net.Mail.MailMessage"/>.</returns>
        internal System.Net.Mail.MailMessage ToMailMessage()
        {
            var mail = new System.Net.Mail.MailMessage();

            if (From != null)
            {
                mail.From = new MailAddress(From);
            }
            To.ForEach(a => mail.To.Add(new MailAddress(a)));
            ReplyToList.ForEach(a => mail.ReplyToList.Add(new MailAddress(a)));
            Bcc.ForEach(a => mail.Bcc.Add(new MailAddress(a)));
            CC.ForEach(a => mail.CC.Add(new MailAddress(a)));

            foreach (var header in Headers)
            {
                mail.Headers[header.Key] = header.Value;
            }

            mail.Body = Body;
            if (BodyEncoding != null)
            {
                mail.BodyEncoding = BodyEncoding;
            }
            mail.DeliveryNotificationOptions = DeliveryNotificationOptions;
            mail.IsBodyHtml = IsBodyHtml;
            if (Sender != null)
            {
                mail.Sender = new MailAddress(Sender);
            }
            mail.Subject = Subject;
            if (SubjectEncoding != null)
            {
                mail.SubjectEncoding = SubjectEncoding;
            }

            mail.Priority = Priority;

            return(mail);
        }