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未测试。。。。
在网上找了一个类说明,结合了一下,重新写了,只要再改动一下,就很实用了。
引用三个类:
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未测试。。。。
相关文章推荐
- asp.net 发送邮件,带附件
- asp.net 发送邮件,带附件
- asp.net 带附件发送邮件
- asp.net带附件功能的简单发送邮件
- ASP.NET 实现发送邮件 + 多个收件人 + 多个附件
- 【Vegas原创】ASP.NET读取Excel,并以邮件正文方式和附件方式发送实例
- ASP.NET发送邮件和添加附件
- asp.net C# 发送 邮件 和 附件 代码
- ASP.net下调用SmtpClient实现smtp方式邮件发送(带附件)
- asp.net使用smtp协议实现简单的发送邮件(支持附件)
- ASP.NET发送邮件附件代码
- ASP.net下调用SmtpClient实现smtp方式邮件发送(带附件)
- ASP.NET用QQ,网易发送邮件以及添加附件
- 【Vegas原创】ASP.NET读取Excel,并以邮件正文方式和附件方式发送实例
- asp.net C# 发送 邮件 和 附件 代码
- 学习记录 用C#.net 加 asp.net发送带附件邮件
- asp.net 发送邮件,带附件
- asp.net 验证码 及发送带附件邮件 (完整demo)
- ASP.NET使用SMTP协议简单的发送邮件(支持附件)
- asp.net mvc发送邮件附带附件代码~~~~