程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#發郵件的代碼 (vs2005)

C#發郵件的代碼 (vs2005)

編輯:關於C語言

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Mail;
public partial class sendemail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        string s_from = TextBox1.Text.Trim().ToString();
        string s_to = TextBox3.Text.Trim().ToString();
        string pwd=TextBox2.Text .Trim().ToString();
        string s_body =TextBox5.Text.Trim().ToString();
        s_body = s_body.Replace("\r\n","<br/>");
        s_body = s_body.Replace("\n", "<br/>");
        s_body = s_body.Replace(" ", " ");

        int i = s_from.IndexOf("@");
        string username = s_from.Substring(0,i);


        MailAddress from = new MailAddress(s_from);
        MailAddress to = new MailAddress(s_to);
        MailMessage mailobj = new MailMessage(from ,to);
        mailobj.Subject = TextBox4.Text.Trim().ToString();
        mailobj.Body = s_body;
        mailobj.IsBodyHtml = true;
        mailobj.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
        mailobj.Priority = MailPriority.High;

        if (FileUpload1.PostedFile.ContentLength > 0)
        {
            mailobj.Attachments.Add(new Attachment(  FileUpload1.PostedFile.FileName));
        }

        SmtpClient smtp = new SmtpClIEnt("smtp.163.com");
       // smtp.Host = "smtp.163.com";
        smtp.UseDefaultCredentials = false;
        smtp.Credentials = new NetworkCredential(username,pwd);
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

        try {
            smtp.Send(mailobj);
            Response.Write("ok!");
       
        }
        catch
       
        {
            Response.Write("sorry!");
        }
      
       
         }
}

////因為用的郵箱都是163的,所以就設置了smtp的地址是163的地址,如果用別的郵箱,只要smtp的地址改下就可以了.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved