📄 sendemail.aspx.cs
字号:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Net.Mail;
using System.Net.NetworkInformation;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Threading;
public partial class SendMsg : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Session["MailTo"] ="sdfl";
// if (Session["UserID"] == null)
// Response.Redirect("index.aspx");
if (Session["StudentID"]== null)
Session["StudentID"] = "04157008";
Session["Email"] = "fjptcjn@gmail.com";
if (!IsPostBack)
{
//登录时初始化
if (Session["Email"] != null && Session["Email"].ToString() != null && Session["Email"].ToString().Trim() != "" && Session["Email"].ToString().Length != 0 && Session["Email"].ToString() != "")
{
// Session["Email"] = "fjptcjn@gmail.com";
//------------------------------------------------
object Getemail = Session["Email"];
this.EServer.Text = Getemail.ToString();
string server;
string name;
string[] Temp = new string[2]; ;
Temp = Getemail.ToString().Split('@');
server = "smtp." + Temp[1];
name = Temp[0];
//this.ETitle.Text = server;
if (Session["EmailServer"] != null)
{
this.EmailServer.Text = this.Session["EmailServer"].ToString();
}
else
{
this.EmailServer.Text = server;
}
//if (Session["MailTo"] != null)
//{
// this.SelList.Items.Add(new ListItem(Session["MailTo"].ToString(), Session["MailTo"].ToString()));
//}
}
}
if (Session["MailTo"]!= null)
///Session["MailTo"].ToString()
this.SelList.Items.Add(new ListItem(Session["MailTo"].ToString(), Session["MailTo"].ToString()));
Session["MailTo"]= null;
//登录时初始化
// Session["StudentID"] = "04157008";
//-------------------------
if (!IsPostBack)
{
object source = Session["UserID"];
string UserId = source.ToString();
GetClass(UserId);
// this.ClassList.Text = "2004软件工程";
if(ClassList.SelectedIndex!=-1)
GetEmailList(ClassList.SelectedItem.Text);
}
}
private void Send(string Email,string ToEmail)
{
// 创建MailMessage对象
//MailMessage mailMsg = new MailMessage();
////设置收件人的邮件地址
//// //send
//MailAddress ma = new MailAddress(Email);
//mailMsg.From = ma;
////accept
//mailMsg.To.Add(ToEmail);
////mailMsg.To = "fjptcjn@gmail.com";
//////设置发送者的邮件地址
////mailMsg.From = "fjptcjn@Gmail.com";
////设置邮件主题
//mailMsg.Subject = "测试";
////设置邮件内容
//mailMsg.Body = "你的用户名是866666密码是533332";
////设置为需要用户验证
//mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
////设置验证用户名(邮箱的用户名)
//mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "fjptcjn");
////设置验证密码(邮箱的密码)
//mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "ptcjn2391092");
////设置发送邮件服务器
//SmtpMail.SmtpServer = "smtp.gmail.com";
////发送邮件
//SmtpMail.Send(mailMsg);
////msg.Text = "邮件发送成功";
MailMessage mailMsg = new MailMessage();
mailMsg.BodyEncoding = System.Text.Encoding.GetEncoding(936);
//send
MailAddress ma = new MailAddress(Email);
mailMsg.From = ma;
//accept
mailMsg.To.Add(ToEmail);
mailMsg.IsBodyHtml = false;
//title
mailMsg.Subject = this.ETitle.Text;
//content
mailMsg.Body = this.Econtent.Text;
string[] Temp ;
string server;
string name;
Temp = Email.Split('@');
server = "smtp." + Temp[1];
name = Temp[0];
this.EServer.Text = name;
SmtpClient client = new SmtpClient("smtp.gmail.com", 25);
System.Net.NetworkCredential usepwd = new System.Net.NetworkCredential("fjptcjn", "ptcjn2391092");
client.Credentials = usepwd;
client.Timeout = 100000;
mailMsg.Priority = MailPriority.High;
client.Send(mailMsg);
}
protected void GetClass(string UserId)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select distinct Num ,ClassName from UserToClass where UserID='" + UserId + "'";
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.HasRows ==true)
{
this.ClassList.DataSource = sdr;
this.ClassList.DataTextField = "ClassName";
this.ClassList.DataValueField = "Num";
this.ClassList.DataBind();
}
else
{
}
conn.Close();
}
protected void GetEmailList(string ClassName)
{
string sql = "SELECT StudentInfo.Name, UserInfo.UserID, UserInfo.Email FROM UserInfo INNER JOIN StudentInfo ON UserInfo.StudentID = StudentInfo.StudentID where UserInfo.UserID IN (select UserID from UserToClass where ClassName='" + ClassName + "')";//ClassName"+this.Province.Text.ToString()+"
//string sql="select distinct Name,Email from UserInfo,StudentInfo where StudentID IN (select StudentID from UserToClass where ClassName='"+ClassName+"')";//ClassName"+this.Province.Text.ToString()+"
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = sql;
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.HasRows == true)
{
this.AllList.DataSource = sdr;
this.AllList.DataTextField = "Name";
this.AllList.DataValueField = "Email";
this.AllList.DataBind();
}
else
{
return;
}
conn.Close();
}
protected void EmailBtn_Click(object sender, EventArgs e)
{
this.IsSend.Text = "";
//string[] ToEmail = new string[this.SelList.Items.Count];
//for (int i = 0; i < this.SelList.Items.Count; i++)
//{
// ToEmail[i] = this.SelList.Items[i].Text;
//}
// SendEmail("fjptcjn@gmail.com", "fjptcjn@gmail.com");
string[] ToEmail = new string[this.SelList.Items.Count];
int EmailCount = 0;
for (int i = 0; i < this.SelList.Items.Count; i++)
{
if (this.SelList.Items[i].Value != "" || this.SelList.Items[i].Value == null)
{
ToEmail[i] = this.SelList.Items[i].Value;
EmailCount++;
}
}
string server;
string name;
string[] Temp = new string[2]; ;
Temp = this.EServer.Text.ToString().Split('@');
server = "smtp." + Temp[1];
name = Temp[0];
//this.ETitle.Text = server;
//if (this.EmailServer.Text.Length == 0 || this.EmailServer.Text == "" || this.EmailServer.Text == null)
// this.EmailServer.Text= server;
Session["Email"] = this.EServer.Text;
SendEmail(this.EServer.Text, this.EmailServer.Text, name, ToEmail, EmailCount);
Session["EmailServer"] = this.EmailServer.Text;
}
private void SendEmail(string Email,string server,string name, string[] ToEmail, int nEmail)
{
this.IsSend.Text = "";
System.Net.Mail.SmtpClient mail = new System.Net.Mail.SmtpClient();
mail.Host = "127.0.0.1";
mail.Timeout = 20000;
System.Net.NetworkCredential nc = new System.Net.NetworkCredential("","");//验证的用户名和密码
mail.Credentials = nc;
mail.UseDefaultCredentials = true;
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
MailAddress mailaddrFrom = new MailAddress("398856042@QQ.com");
//====================================
message.From = mailaddrFrom;
// message.Sender = mailaddrFrom;
//===========================================
//for (int i = 0; i < nEmail; i++)
//{
// message.To.Add(ToEmail[i].Trim());
//}
// message.
message.To.Add("hkvoer@163.com");
////===================================
// message.To.Add("fjptcjn@gmail.com");
//// message.To.Add("fjptcjn@gmail.com");
message.Subject = this.ETitle.Text.Trim();
message.Body = this.Econtent.Text.Trim();
message.SubjectEncoding = System.Text.Encoding.GetEncoding("gb2312");
message.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312");
message.Priority = MailPriority.Normal;
message.IsBodyHtml = true;
mail.DeliveryMethod = SmtpDeliveryMethod.Network;
// string temp =""; ;
//for (int i = 0; i < nEmail; i++)
//{
// message.To.Add(ToEmail[i]);
try
{
mail.Send(message);
this.IsSend.Text = "发送成功";
//for (int a = 0; a < 10000; a++)
// if (a == 9999) break;
}
catch (Exception)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -