专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-683-0016      微信咨询  |  联系我们

aspnet发邮件的几种方法汇总_CSS学习

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/3/4 4:01:46       共计:3605 浏览

aspnet发邮件的几种方法汇总?

MailMessage

提供属性和方法来创建一个邮件消息对象。通常可以先构建好MailMessage对象,然后设置它的属性的方式来构建邮件程序。

常用的属性:

From -- 发送邮件的地址

To -- 接受邮件的地址

Subject -- 邮件的标题

Priority -- 邮件的优先级(有效值为High,Low,Normal)

Attachments -- 返回一个集合,代表附件

Bcc -- 密送地址

Cc -- 抄送地址

Body -- 获取或是设置电子邮件消息的内容

BodyFormat -- 获取或是设置MailFormat的枚举值,此值指定消息体邮件的格式(Html格式、Text格式)

Bodyencoding -- 指定消息的编码方式编码(主要有Base64,UUencode)

UrlContentBase :在HTML格式邮件中的URL编码方式

UrlContentLocation:邮件信息的优先级(High, Medium,Low)

SmtpMail

负责发送邮件的SMTP协议,提供属性和方法通过使用windows 2000 CDOSYS 的消息组件的联合数据对象来发送邮件消息。

SmtpMail类用到的是Send方法,它的目的就是发送邮件,有两个重载方法。

1. SmtpMail.Send("发送邮件的地址","接受邮件的地址","邮件的标题","邮件消息的内容") 这个方法很简单,不适合发送带附件的邮件。

2. SmtpMail.Send(MailMessage) 此方法复杂、灵活,适合发送附件,而且可以设置MailMessage对象的各种属性值。 如果我们用asp.net写一个邮件发送的程序,那么首先应该如何得到SMTP。有两种方法:第一种方法调用目前知名的邮件服务提供商的SMTP,比如新浪、搜狐、网易的免费电子邮箱的SMTP;第二种方法是自己装一个SMTP虚拟服务器,这个在安装IIS时一起装上去的。

MailAttachment

与邮件附件有关的对象类,主要用来提供属性和方法来创建一个邮件附件对象。

构造函数

创建一个附件对象

MailAttachment objMailAttachment = new MailAttachment( "d:\test。txt" );//发送邮件的附件

调用形式

MailMessage objMailMessage= new MailMessage();

objMailMessage.Attachments.Add( objMailAttachment );//将附件附加到邮件消息对象中

封装的邮件发送类

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;

using System.Net.Mail;

using System.Text;

public class SendMail

...{

public SendMail()

...{

}

private string _host;

/**//// <summary>

/// 服务器地址

/// </summary>

public string Host

...{

get ...{ return _host; }

set ...{ _host = value; }

}

private int _port;

/**//// <summary>

/// 服务器端口

/// </summary>

public int Port

...{

get ...{ return _port; }

set ...{ _port = value; }

}

private string _smtpUsername;

/**//// <summary>

/// 发送人邮箱用户名

/// </summary>

public string SmtpUsername

...{

get ...{ return _smtpUsername; }

set ...{ _smtpUsername = value; }

}

private string _sendemail;

/**//// <summary>

/// 发送人邮箱帐号(只接收加密串,发邮件时解密)

/// </summary>

public string SendEmail

...{

get

...{

return _sendemail;

}

set ...{ _sendemail = value; }

}

private string _replyToEmail;

/**//// <summary>

/// 回复人邮箱账号

/// </summary>

public string ReplyToEmail

...{

get ...{ return _replyToEmail; }

set ...{ _replyToEmail = value; }

}

private string _replyUserName;

/**//// <summary>

/// 回复人用户名

/// </summary>

public string ReplyUserName

...{

get ...{ return _replyUserName; }

set ...{ _replyUserName = value; }

}

private string _getemail;

/**//// <summary>

/// 收件人邮箱帐号

/// </summary>

public string GetEmail

...{

get ...{ return _getemail; }

set ...{ _getemail = value; }

}

private string _smtpPassword;

/**//// <summary>

/// 发送人邮箱密码(只接收加密串,发邮件时解密)

/// </summary>

public string SmtpPassword

...{

get ...{ return _smtpPassword; }

set ...{ _smtpPassword = value; }

}

private string _content;

/**//// <summary>

/// 邮件内容

/// </summary>

public string Content

...{

get ...{ return _content; }

set ...{ _content = value; }

}

private string _title;

/**//// <summary>

/// 邮件标题

/// </summary>

public string Title

...{

get ...{ return _title; }

set ...{ _title = value; }

}

private string[] _cc = null;

/**//// <summary>

/// 抄送邮箱

/// </summary>

public string[] cc

...{

get ...{ return _cc; }

set ...{ _cc = value; }

}

private string[] _bcc = null;

/**//// <summary>

/// 密送邮箱

/// </summary>

public string[] bcc

...{

get ...{ return _bcc; }

set ...{ _bcc = value; }

}

/**//// <summary>

///发送邮件

/// </summary>

/// <returns>返回是否成功</returns>

public bool Send()

...{

try

...{

MailMessage objMailMessage;

objMailMessage = new MailMessage(SendEmail, _getemail, _title, _content);

if (!string.IsNullOrEmpty(_replyToEmail) && !string.IsNullOrEmpty(_replyUserName))

...{

MailAddress reply = new MailAddress(_replyToEmail, _replyUserName);

objMailMessage.ReplyToList.Add(reply);

}

objMailMessage.BodyEncoding = Encoding.GetEncoding(936);

objMailMessage.IsBodyHtml = true;

if (cc != null && cc.Length > 0)

...{

foreach (string ccAddress in cc)

...{

objMailMessage.CC.Add(new MailAddress(ccAddress));

}

}

if (bcc != null && bcc.Length > 0)

...{

foreach (string bccAddress in bcc)

...{

objMailMessage.Bcc.Add(new MailAddress(bccAddress));

}

}

SmtpClient client = new SmtpClient(this._host, this._port);

if (!String.IsNullOrEmpty(this.SmtpUsername) && !String.IsNullOrEmpty(this.SmtpPassword))

...{

client.Credentials = new NetworkCredential(this.SmtpUsername, this.SmtpPassword);

}

client.EnableSsl = false;

client.Send(objMailMessage);

objMailMessage.Dispose();

return true;

}

catch

...{

return false;

}

}

}

版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:为什么乒乓球打发球机拉的挺好_CSS学习 | ·下一条:3dmax矩形如何倒内圆角_CSS学习

Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有