Send_Emil.aspx.cs
上传用户:njhtgyjx
上传日期:2022-02-07
资源大小:2k
文件大小:5k
- using System;
- using System.Collections;
- using System.Configuration;
- using System.Data;
- using System.Linq;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Xml.Linq;
- using System.Net.Mime;
- using System.ComponentModel;
- using System.Drawing;
- using System.Text;
- using System.Net;
- using System.IO;
- using System.Net.Mail;
- public partial class Send_Emil : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!Page.IsPostBack)
- {
- if (Test2())
- {
- Response.Write("<script>alert('发送成功!')</script>");
- }
- else
- {
- Response.Write("<script>alert('发送失败!')</script>");
- }
- }
- }
- public bool Test2()
- {
- MailAddress from = new MailAddress("****@126.com", "x123123"); //邮件的发件人
- MailMessage mail = new MailMessage();
- //设置邮件的标题
- mail.Subject = "测试数据";
- //设置邮件的发件人
- mail.From = from;
- string[] mailNames = (txtMailTo.Text + ";").Split(';');
- string displayName = "";
- string address = "";
- foreach (string name in mailNames)
- {
- if (name != string.Empty)
- {
- if (name.IndexOf('<') > 0)
- {
- displayName = name.Substring(0, name.IndexOf('<'));
- address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');
- }
- else
- {
- displayName = string.Empty;
- address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');
- }
- mail.To.Add(new MailAddress(address, displayName));
- }
- }
- //设置邮件的抄送收件人
- //这个就简单多了,如果不想快点下岗重要文件还是CC一份给领导比较好
- mail.CC.Add(new MailAddress("****@126.com", " 抄送2313123"));
- //设置邮件的内容
- mail.Body = "我的测试数据内容";
- //设置邮件的格式
- mail.BodyEncoding = System.Text.Encoding.UTF8;
- mail.IsBodyHtml = true;
- //设置邮件的发送级别
- mail.Priority = MailPriority.Normal;
- //设置邮件的附件,将在客户端选择的附件先上传到服务器保存一个,然后加入到mail中
- // string fileName = txtUpFile.PostedFile.FileName.Trim();
- // fileName = @"D:UpFile" + fileName.Substring(fileName.LastIndexOf("\") + 1);
- // txtUpFile.PostedFile.SaveAs(fileName); // 将文件保存至服务器
- // mail.Attachments.Add(new Attachment(fileName));
- mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
- SmtpClient client = new SmtpClient();
- //设置用于 SMTP 事务的主机的名称,填IP地址也可以了
- client.Host = "smtp.126.com";
- //设置用于 SMTP 事务的端口,默认的是 25
- client.Port = 25;
- client.UseDefaultCredentials = false;
- //邮箱登陆名和密码
- System.Net.NetworkCredential client1 = new NetworkCredential("****n@126.com", "****");
- client.Credentials = client1;
- //client.Credentials = new System.Net.NetworkCredential("****@live.cn", "*****");
- client.DeliveryMethod = SmtpDeliveryMethod.Network;
- try
- {
- client.Send(mail);
- }
- catch (SmtpException ex)
- {
- return false;
- }
- return true;
- }
- public bool Test1()
- {
- try
- {
- //准备邮件内容
- MailMessage message = new MailMessage("****@qq.com", "****@163.com", "我的邮件主题", "<a href='#'>正文测试数据</a>");
- // message.Attachments.Add(new Attachment(@"D:a.txt"));
- message.Priority = MailPriority.Normal;
- message.IsBodyHtml = true;
- //准备安全凭证
- NetworkCredential nc = new NetworkCredential("***@qq.com", "****");
- //准备SMTP,发送电子邮件
- SmtpClient client = new SmtpClient("smtp.163.com", 25);
- // SmtpClient client = new SmtpClient("smtp.126.com",25);
- client.DeliveryMethod = SmtpDeliveryMethod.Network;
- client.Credentials = nc;
- client.Send(message);
- return true;
- }
- catch (FormatException ex)
- {
- return false;
- }
- catch (SmtpException ex)
- {
- return false;
- }
- }
- }