现在的位置: 首页 > 综合 > 正文

ASP.net发送邮件

2013年11月24日 ⁄ 综合 ⁄ 共 1034字 ⁄ 字号 评论关闭

 

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Net;
using System.Net.Mail;

/// <summary>
///GM_Mail 的摘要说明
/// </summary>

public class GM_Mail
{
    public GM_Mail()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }
    
    //subject:标题; body:邮件正文; destAddr:目的邮箱地址
    public static bool sendMail(string subject, string body, string destAddr)
    {
        try
        {
            MailAddress from = new MailAddress("senderEmail@Address");//通过该邮箱地址进行发送
            MailAddress to = new MailAddress(destAddr);
            MailMessage message = new MailMessage(from, to);
            message.Subject = subject;
            message.Body = body;
            SmtpClient client = new SmtpClient();
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Port = 25; //发送邮箱的服务器端口
            client.Host = "The smtp address of the sender's email address"; //发送邮箱的smtp地址
            client.Credentials = new System.Net.NetworkCredential("Sender email address", "email password"); //邮箱用户名和密码,发送时需要登录该邮箱进行发送
            client.Send(message);

            return true;
        }
        catch (System.Exception ex)
        {
            return false;
        }
    }
}

抱歉!评论已关闭.