.Net魔法堂:發個待附件的郵件

來源:互聯網
上載者:User

標籤:style   blog   http   color   strong   os   

一、前言                          

  由於工作需要最近把郵件發送封裝成WebService,現在把代碼記錄在此,以便日後查閱。

 

二、二話不說寫代碼                    

 private void _SendMail(string form, string pw, string subject, string[] to, string[] cc, string body, string attachmentName, byte[] attachment)        {            SmtpClient client = new SmtpClient(SMTP服務地址, SMTP服務連接埠);            client.UseDefaultCredentials = true;            client.Credentials = new NetworkCredential(form, pw); // 驗證資訊要與郵件發送源地址一致,否則無法發送            client.DeliveryMethod = SmtpDeliveryMethod.Network;            MailMessage message = new MailMessage();            message.From = new MailAddress(form);            foreach (string toItem in to)            {                message.To.Add(toItem);            }            foreach (string ccItem in cc)            {                message.CC.Add(ccItem);            }            message.Subject = subject;            message.SubjectEncoding = Encoding.UTF8;            message.Body = body;            message.BodyEncoding = Encoding.UTF8;            message.IsBodyHtml = true;            message.Priority = MailPriority.High;            Stream stream = null;            Attachment data = null;            try            {                if (attachment != null && attachment.Length > 0)                {                    stream = new MemoryStream(attachment);                    data = new Attachment(stream, attachmentName, System.Net.Mime.MediaTypeNames.Application.Octet);                    message.Attachments.Add(data);                }                client.Send(message);            }            finally            {                if (null != stream)                {                    stream.Close();                }            }        }

 

三、三種發送方式                            

  發送郵件有三種方式:

  ①. client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

  直接通過遠程SMTP伺服器發送郵件

  ②. client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.PickupDirectoryFromIis;

  通過原生IIS伺服器的SMTP服務發送郵件(後續在研究什麼配置SMTP伺服器?)

  ③. client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.SpecifiedPickupDirectory;

  將郵件發送到本機指定目錄中,再通過其他手段(如通過FTP)發送到目的地

 

四、總結                                

 僅僅是實踐後的爪印,沒啥好總結的。

 尊重原創,轉載請註明來自:http://www.cnblogs.com/fsjohnhuang/p/3848355.html ^_^肥仔John

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.