asp.net 发送邮件

把代码贴出来,供大家参考
using System;
using System.Data;
using System.Configuration;
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.Mail;
/// <summary>
/// SendMail 的摘要说明
/// </summary>
public class SendMail
{
    /// <summary>
    /// 发送邮件,不能发送附件。
    /// </summary>
    /// <param name="send">发件人地址</param>
    /// <param name="recieve">收件人地址</param>
    /// <param name="subject">邮件主题</param>
    /// <param name="mailbody">邮件内容允许是html</param>
    /// <param name="host">发件主机</param>
    /// <param name="pwd">密码</param>
    public void SendMailUseGmail(string send, string recieve, string subject, string mailbody, string host, string pwd,Page page)
    {
        try
        {
            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
            client.Host = host;
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential(send, pwd);
           
            client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
            message.To.Add(recieve); message.From = new System.Net.Mail.MailAddress(send, "派遣168网", System.Text.Encoding.UTF8);
            message.Subject = subject;
            message.Body = mailbody;
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.IsBodyHtml = true;
            //添加附件
            //System.Net.Mail.Attachment data = new Attachment(@"附件地址如:e:/a.jpg", System.Net.Mime.MediaTypeNames.Application.Octet);
            //message.Attachments.Add(data);


            client.Send(message);
            System.Web.UI.ScriptManager.RegisterStartupScript(page, this.GetType(), "success", "alert('邮件发送成功!');", true);

        }
        catch (Exception ex)
        {

            System.Web.UI.ScriptManager.RegisterStartupScript(page, this.GetType(), "eeror", "alert('邮件发送失败!"+ex.Message+"');", true);
        }




    }

    /// <summary>
    /// 发送邮件,不能发送附件。
    /// </summary>
    /// <param name="send">发件人地址</param>
    /// <param name="recieve">收件人地址</param>
    /// <param name="subject">邮件主题</param>
    /// <param name="mailbody">邮件内容允许是html</param>
    /// <param name="host">发件主机</param>
    /// <param name="sendname">发件人的称呼</param>
    /// <param name="pwd">密码</param>
    public bool SendMailUseGmail(string send, string recieve, string subject, string mailbody, string host, string pwd,string sendname, Page page)
    {
        try
        {
            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
            client.Host = host;
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential(send, pwd);

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

            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
            message.To.Add(recieve); message.From = new System.Net.Mail.MailAddress(send, sendname, System.Text.Encoding.UTF8);
            message.Subject = subject;
            message.Body = mailbody;
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.IsBodyHtml = true;
            //添加附件
            //System.Net.Mail.Attachment data = new Attachment(@"附件地址如:e:/a.jpg", System.Net.Mime.MediaTypeNames.Application.Octet);
            //message.Attachments.Add(data);


            client.Send(message);
            return true;
            //System.Web.UI.ScriptManager.RegisterStartupScript(page, this.GetType(), "success", "alert('邮件发送成功!');", true);

        }
        catch (Exception ex)
        {
            return false;
            //System.Web.UI.ScriptManager.RegisterStartupScript(page, this.GetType(), "eeror", "alert('邮件发送失败!" + ex.Message + "');", true);
        }




    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值