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;
public partial class mail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
mailBind();
Response.Write("<script>alert('发送成功!')</script>");
}
protected void mailBind()
{
MailMessage msg = new MailMessage();
msg.Body = this.TextBox2.Text;
msg.From = new MailAddress("Leihaijiayuan@163.com"); //发送邮件的地址
//msg.Sender = new MailAddress("");
msg.To.Add("285105613@qq.com"); //接受邮件的地址
msg.Subject = this.TextBox1.Text;
msg.IsBodyHtml = true;//邮件内容默认是纯文本!如果指定html内容,需要使用isbodyHtml
SmtpClient sc = new SmtpClient();
sc.Host = "smtp.163.com";
sc.Port = 25;
NetworkCredential nc = new NetworkCredential();
nc.UserName = "leihaijiayuan"; //发送邮件的名称
nc.Password = "*************"; // 发送邮件的密码
sc.Credentials = nc;
string filename = this.FileUpload1.FileName; //获取要上传的文件的文件名
this.FileUpload1.SaveAs(Server.MapPath("~/") + filename); //先把你要上传得文件 保存在您的 web 站点里!以为这样才可以使用
Attachment att = new Attachment(Server.MapPath("~/") + filename); //定义邮箱需要的附件
msg.Attachments.Add(att); //添加邮箱需要的附件
sc.Send(msg); //发送信息
}
protected void Button2_Click(object sender, EventArgs e) // jmail实现邮件的发送
{
jmail.Message msg = new jmail.Message();
msg.Subject = this.TextBox1.Text;
msg.Body = this.TextBox2.Text;
msg.From = "leihaijiayuan@163.com"; //发送邮件的地址
msg.AddRecipient("285105613@qq.com"); //接受邮件的地址
//msg.AddRecipient("1370969042@qq.com"); //接受邮件的地址
msg.MailServerUserName = "leihaijiayuan";//发送邮件的账号
msg.MailServerPassWord = "*********"; //发送邮件的密码
msg.Charset = "utf-8"; // 文本支持的类型
msg.ContentType = "text/html"; //是否支持html 格式的文本(一般用作超链接)
//string filename = this.FileUpload1.FileName; //文件的上传 这步跟上面的一样 ,但是jmail 有一个很不好解决的地方!就是文件的上传和那个超链接
//this.FileUpload1.SaveAs(Server.MapPath("~/") + filename); //有点不兼容,而这之间 最好使用一个!这样才能更好的保证你的文件的安全,易用!简单!
//jmail.AttachmentClass att = new jmail.AttachmentClass();
//msg.AddAttachment(Server.MapPath("~/") + filename);
msg.Send("smtp.163.com");
//Response.Write("<script>alert('发送成功!')</script>");
}
}