发送邮件程序,亲测

public partial class Test_Default5 : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        

    }

    /// <summary> 

    /// 发送邮件 

    /// </summary> 

    /// <param name="address">接收人:Eamil地址</param> 

    /// <param name="subject">主题</param> 

    /// <param name="body">邮件内容</param> 

    /// <param name="attachment">附件</param> 

    /// <param name="isBodyHtml">邮件是Html格式</param> 

    /// <returns></returns> 

    public  bool Send(string address, string subject, string body, Attachment attachment, bool isBodyHtml)

    {

        try

        {

            MailMessage mail = new MailMessage();

            mail.From = new MailAddress("[email protected]");

            mail.To.Clear();

            mail.To.Add(new MailAddress(address));

            mail.Body = body;

            mail.BodyEncoding = Encoding.UTF8;

            mail.SubjectEncoding = Encoding.UTF8;

            mail.Subject = subject;

            mail.IsBodyHtml = isBodyHtml;

            mail.Priority = System.Net.Mail.MailPriority.Normal;

            if (attachment != null) mail.Attachments.Add(attachment);

            SmtpClient smtp = new SmtpClient("smtp.163.com", 25);

            smtp.UseDefaultCredentials = true;

            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

            smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "");

            smtp.Send(mail);

            return true;

        }

        catch

        {

            Response.Write("发送失败!");

            return false;

        }

    }



    protected void Button1_Click(object sender, EventArgs e)

    {

        Send("[email protected]", "Test", "<br/>测试邮件", null, true);

       

    }

}

你可能感兴趣的:(发送邮件)