您的位置:首页 > 编程语言 > ASP

asp.net 带附件发送邮件

2008-10-29 01:02 381 查看
在51aspx.com下了个发邮件的,不过不怎么好用,也没有详细的说明,也没有发送成功,

在网上找了一个类说明,结合了一下,重新写了,只要再改动一下,就很实用了。

引用三个类:

using System.Net.Mail;

using System.Net;

using System.Net.Mime;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Net.Mail;

using System.Net;

using System.Net.Mime;

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

{

protected void Page_Load(object sender, EventArgs e)

{

}

protected void Button1_Click(object sender, EventArgs e)

{

MailMessage mailMsg = new MailMessage();

mailMsg.From = new MailAddress("*****@163.com");//发送邮件用户名

mailMsg.To.Add("*******@163.com");//接收邮件用户

mailMsg.Subject = "邮件主题";

mailMsg.Body = "邮件主体内容ffffffffffffffffffff";

mailMsg.BodyEncoding = System.Text.Encoding.UTF8;

mailMsg.IsBodyHtml = false;

mailMsg.Priority = MailPriority.High;

string file = Request.PhysicalApplicationPath + "test//data.xls"; //附件的地址

// Create the file attachment for this e-mail message.

Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);

// Add time stamp information for the file.

ContentDisposition disposition = data.ContentDisposition;

disposition.CreationDate = System.IO.File.GetCreationTime(file);

disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);

disposition.ReadDate = System.IO.File.GetLastAccessTime(file);

mailMsg.Attachments.Add(data);

SmtpClient smtp = new SmtpClient();

// 提供身份验证的用户名和密码

// 网易邮件用户可能为:username password

// Gmail 用户可能为:username@gmail.com password

smtp.Credentials = new NetworkCredential("userName", "userpwd");

smtp.Port = 25; // Gmail 使用 465 和 587 端口

smtp.Host = "smtp.163.com"; // 如 smtp.163.com, smtp.gmail.com

smtp.EnableSsl = false; // 如果使用GMail,则需要设置为true

//smtp.SendCompleted += new SendCompletedEventHandler(SendMailCompleted);

smtp.SendAsync(mailMsg, mailMsg);

}

}

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