C#發送郵件及附件

來源:互聯網
上載者:User

C#發送郵件的功能在網上找了很多也有利用socket的 ,試了一下不行的原因是smtp伺服器的問題。在這裡我用了mailmessage和搜狐的stmp.sohu.com。源碼如下:

 

    protected void Button1_Click(object sender, EventArgs e)
    {

        string from = ******@sohu.com;
        string fromer = "寄件者";
        string to = "*****@126.com";
        string toer = "收件者";
        string Subject = "郵件標題";
        string file="附件地址";
        string Body ="發送內容";
        string SMTPHost = "smtp.sohu.com";
        string SMTPuser = "******@sohu.com";
        string SMTPpass = "*******";
        sendmail(from, fromer, to, toer, Subject, Body,file,SMTPHost, SMTPuser, SMTPpass);
    }

 

 

 

 

    /// <summary>
    /// C#發送郵件函數
    /// </summary>
    /// <param name="from">寄件者郵箱</param>
    /// <param name="fromer">發送人</param>
    /// <param name="to">接受者郵箱</param>
    /// <param name="toer">收件者</param>
    /// <param name="Subject">主題</param>
    /// <param name="Body">內容</param>
    /// <param name="file">附件</param>
    /// <param name="SMTPHost">smtp伺服器</param>
    /// <param name="SMTPuser">郵箱</param>
    /// <param name="SMTPpass">密碼</param>

    /// <returns></returns>
    public bool sendmail(string sfrom, string sfromer, string sto, string stoer, string sSubject, string sBody, string sfile, string sSMTPHost, string sSMTPuser, string sSMTPpass)
    {
        ////設定from和to地址
        MailAddress from = new MailAddress(sfrom, sfromer);
        MailAddress to = new MailAddress(sto, stoer);

        ////建立一個MailMessage對象
        MailMessage oMail = new MailMessage(from, to);

        //// 添加附件
        if (sfile != "")
        {
            oMail.Attachments.Add(new Attachment(sfile));
        }

 

        ////郵件標題
        oMail.Subject = sSubject;

        ////郵件內容
        oMail.Body = sBody;

        ////郵件格式
        oMail.IsBodyHtml = false;

        ////郵件採用的編碼
        oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");

        ////設定郵件的優先順序為高
        oMail.Priority = MailPriority.High;

        ////發送郵件
        SmtpClient client = new SmtpClient();
        ////client.UseDefaultCredentials = false;
        client.Host = sSMTPHost;
        client.Credentials = new NetworkCredential(sSMTPuser, sSMTPpass);
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        try
        {
            client.Send(oMail);
            return true;
        }
        catch (Exception err)
        {
            Response.Write(err.Message.ToString());
            return false;
        }
        finally
        {
            ////釋放資源
            oMail.Dispose();
        }

    }

 

 

聯繫我們

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