您的位置:首页 > 其它

.net发送邮件

2015-11-13 10:51 239 查看
private void sendmmail()

{

//设置MailMessage类的to属性所需的MailAddress

MailAddress toAddress = new MailAddress(this.tbReceiver.Text);

//设置MailMessage类的from属性所需的MailAddress

MailAddress fromAddress = new MailAddress(this.tbSender.Text);

//新建一个MailMessage类实例

MailMessage message = new MailMessage(fromAddress, toAddress);

//设置这个实例的Subject属性

message.Subject = this.tbSubject.Text;

//设置这个实例的Body属性

message.Body = this.tbMessage.Text;

//添加附件

//获得文件

HttpPostedFile postedFile = file.PostedFile;

//当有附件时

if (postedFile.ContentLength != 0)

{

//声明一个Attachment类实例

Attachment data = new Attachment(postedFile.FileName);

message.Attachments.Add(data);

}

//设置正文格式

if (rblFormat.SelectedItem.Text == "纯文本格式")

message.IsBodyHtml = false;

else

message.IsBodyHtml = true;

//添加抄送地址

if (this.tbCc.Text != "")

{

MailAddress ccAddress = new MailAddress(this.tbCc.Text);

message.CC.Add(ccAddress);

}

//添加暗送地址

if (this.tbBcc.Text != "")

{

MailAddress bccAddress = new MailAddress(this.tbBcc.Text);

message.Bcc.Add(bccAddress);

}

//新建一个SmtpClient类的实例

SmtpClient client = new SmtpClient();

//设置在本机smtp服务器中绑定的ip地址,本例为本机ip地址

client.Host = "159.226.58.89";

//smtp端口,默认为25

client.Port = 25;

//发送

client.Send(message);

//发送完毕后提示

Response.Write("<script language='javascript'>alert('发送成功')</script>");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: