發現用System.Net.Mail發郵件(代碼附後),附件稍微大一點就會造成程式假死. 有沒有什麼簡單的解決辦法呢? 多謝!!

來源:互聯網
上載者:User

標籤:io   ar   os   使用   sp   for   on   div   art   

附件大,上傳,發送一定會慢.程式卡,應該是主線程正在發送,郵件造成的.建立其他線程在後台去發.這樣就不影響主線程做其他工作了 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Net.Mail;namespace WindowsFormsApplication1{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }         private void button1_Click(object sender, EventArgs e)        {             MailMessage MailMessage = new MailMessage();            //這個是我們的郵件對象,包含主題,內容等主要屬性            SmtpClient SmtpServer = new SmtpClient();            //這個是我們的SMTP用戶端對象,通過這個對象將我們的郵件發送出去              MailMessage.From = new MailAddress("[email protected]","網站郵件", System.Text.Encoding.UTF8);            //例如[email protected] ‘MailMessage對象的From屬性意為郵件的寄件者,顧名思義在此處設定郵件的寄件者.            MailMessage.To.Add("[email protected]");            //例如[email protected] ‘MailMessage對象的To屬性意為該郵件的收件者集合,使用該屬性的Add方法來添加收件者            MailMessage.Subject = "大家好!~我是郵件標題";            //Subject屬性就是郵件的標題內容            //MailMessage.Body = "大家好!~我是郵件內容" ‘Body屬性是郵件的內容            MailMessage.Priority = MailPriority.Normal;            //Normal是普通優先順序,這裡還可以設定成High或Low            SmtpServer.Host = "SMTP.qq.com";            //這裡設定我們的SMTP伺服器,例如smtp.163.com            SmtpServer.Credentials = new System.Net.NetworkCredential("9520848""nnnnnnnnnnnnn");            //這裡的使用者名稱和密碼用於SMTP伺服器認證            //SmtpServer.Timeout = 100 ‘設定發送逾時的時間,預設是100秒             MailMessage.Attachments.Add(new Attachment("c:\\網頁.txt"));            MailMessage.Attachments.Add(new Attachment("G:\\Ultt_SC17.rar"));            MailMessage.Body = "111111111111";             SmtpServer.Send(MailMessage);        }    }}

發現用System.Net.Mail發郵件(代碼附後),附件稍微大一點就會造成程式假死. 有沒有什麼簡單的解決辦法呢? 多謝!!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.