예제 #1
0
 public SmtpBody(MailAddress from, MailAddress to, string subject, string body, SmtpCharsetList charsetList)
 {
     this.From               = from;
     this.To                 = to;
     this.Subject            = subject;
     this.Body               = body;
     this.CharsetList        = charsetList;
     this.AttatchedFileList  = new List <Attachment>();
     this.LinkedResourceList = new List <LinkedResource>();
 }
예제 #2
0
        public void Send(SmtpConfig smtp)
        {
            SmtpClient c = new SmtpClient(smtp.SmtpServer, smtp.SmtpPort);

            c.DeliveryMethod = SmtpDeliveryMethod.Network;
            c.EnableSsl      = smtp.UseSSL;

            MailMessage mail = new MailMessage(CharsetList.NormalizeMailAddress(this.From),
                                               CharsetList.NormalizeMailAddress(this.To));

            Encoding         bodyEnc  = CharsetList.GetAppropriateCharset(this.Body);
            TransferEncoding bodyTran = SmtpCharsetList.GetTransferEncoding(bodyEnc);

            byte[]       bodyData = bodyEnc.GetBytes(this.Body);
            MemoryStream ms       = new MemoryStream(bodyData);

            AlternateView alt = new AlternateView(ms,
                                                  new ContentType("text/plain; charset=" + bodyEnc.WebName));

            alt.TransferEncoding = bodyTran;

            if (Str.IsEmptyStr(this.Body) == false)
            {
                mail.AlternateViews.Add(alt);
            }
            mail.Body         = "";
            mail.BodyEncoding = bodyEnc;

            // HTML メールの場合
            if (Str.IsEmptyStr(this.BodyHtml) == false)
            {
                Encoding         htmlEnc  = CharsetList.GetAppropriateCharset(this.BodyHtml);
                TransferEncoding htmlTran = SmtpCharsetList.GetTransferEncoding(htmlEnc);

                byte[] htmlData = htmlEnc.GetBytes(this.BodyHtml);
                ms = new MemoryStream(htmlData);

                AlternateView alt2 = new AlternateView(ms,
                                                       new ContentType("text/html; charset=" + htmlEnc.WebName));

                // リソースファイル
                foreach (LinkedResource a in LinkedResourceList)
                {
                    alt2.LinkedResources.Add(a);
                }

                mail.AlternateViews.Add(alt2);
            }

            // 添付ファイル
            foreach (Attachment a in AttatchedFileList)
            {
                mail.Attachments.Add(a);
            }

            Encoding subjectEnc = CharsetList.GetAppropriateCharset(this.Subject);

            byte[] subjectData = subjectEnc.GetBytes(this.Subject);
            string subjectText = string.Format("=?{0}?B?{1}?=", subjectEnc.WebName.ToUpper(),
                                               Convert.ToBase64String(subjectData, Base64FormattingOptions.None));

            if (Str.IsAscii(this.Subject))
            {
                subjectText = this.Subject;
            }

            mail.Subject = subjectText;

            if (this.ReplyTo != null)
            {
//                mail.ReplyTo = this.ReplyTo;
                mail.ReplyToList.Add(this.ReplyTo);
            }

            foreach (MailAddress cc in CcList)
            {
                mail.CC.Add(cc);
            }

            foreach (MailAddress bcc in BccList)
            {
                mail.Bcc.Add(bcc);
            }

            mail.Headers.Add("X-Mailer", XMailer);
            mail.Headers.Add("X-MSMail-Priority", MSMailPriority);
            mail.Headers.Add("X-Priority", MailPriority);
            mail.Headers.Add("X-MimeOLE", MimeOLE);

            c.Send(mail);
        }