邮件正在运行,但我想向每个不同的人发送两个excel文件 [英] Mail is working but I want send two excel file to each different person

查看:72
本文介绍了邮件正在运行,但我想向每个不同的人发送两个excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照以下方式尝试了我的代码,



我的代码正在运行,但我希望First excel发送给一个人,另一个excel发送给另一个人。



我的代码如下



I tried my code as follows,

My code is working, But i want the First excel send to one person and another excel to send to another person.

My code as follows

int count = 0;
string connectionstring = "Server=(local);initial catalog=Test;Trusted_Connection=True";
SqlConnection sqlConnection = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
DataSet ds = new DataSet();
cmd.CommandText = "select * from Empdetails";
cmd.CommandText += " where shifttype = @par";
cmd.Parameters.Add("@par",SqlDbType.Int).Value = j;
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection;
sqlConnection.Open();
reader = cmd.ExecuteReader();
if (reader.HasRows)
{
	string filePath = @"C:\Users\God\Desktop\DataDump\" + j + "Excel.xls";
	System.IO.StreamWriter sw_In = new System.IO.StreamWriter(filePath);
	while (reader.Read())
	{
		if (count == 0)
		{
			for (int i = 0; i < reader.FieldCount; i++)
			{
				sw_In.AutoFlush = true;
				sw_In.Write(reader.GetName(i) + "\t");
			}
			sw_In.Write("\n");
			count = 1;
		}
		for (int i = 0; i < reader.FieldCount; i++)
		{
			sw_In.AutoFlush = true;
			sw_In.Write(reader[i].ToString() + "\t");
		}
		sw_In.Write("\n");
	}
	sw_In.Dispose();
	MailMessage mis = new MailMessage();
	mis.From = new MailAddress("rajesh@gmail.com","Report");
	mis.IsBodyHtml = true;
	mis.To.Add("rajesh@gmail.com");
	mis.Subject = "Data Dump Report";
	mis.Attachments.Add(new Attachment(filePath, "application/vnd.ms-excel"));
	SmtpClient smtpserver = new SmtpClient("smtp.gmail.com"); 
	 
	smtpserver.Credentials = new System.Net.NetworkCredential("rajesh@gmail.com", "Password");
	smtpserver.Host = "smtp.gmail.com";
	smtpserver.Port = 587;
	smtpserver.EnableSsl = true;
	smtpserver.Send(mis);
}
reader.Close();
sqlConnection.Close();





我尝试过:



当我在桌面C文件夹中运行上面的1Excel和2Excel以便在该文件夹中下载。



我发送的两个excel文件邮件。



excel文件收到邮件没问题。



i am将1Excel和2Excel同时[邮件地址1删除]收到此邮件。





但是我希望将1Excel文件发送到[邮件地址1删除]。



和2Excel文件发送到[邮件地址2删除]。





在上面的代码中,1Excel和2Excel文件都将邮件发送到[邮件地址1已删除]。



对于那个有什么变化需要在我的代码中完成。



请帮助我



What I have tried:

When i run the above in desktop C folder 1Excel and 2Excel to be download in that folder.

I am sending the both the excel file to mail.

That excel file is getting a mail no problem.

i am getting a both 1Excel and 2Excel to [mail address 1 removed] to this mail.


But i want 1Excel file to be sent to [mail address 1 removed].

And 2Excel file to be sent to [mail address 2 removed].


In my above code both 1Excel and 2Excel file getting mail to [mail address 1 removed].

For that what changes need to be done in my code.

Please help me

推荐答案

首先,你不是编写Excel文件。您正在编写CSV(逗号分隔值)文本文件,除了分隔符不常见,它是一个选项卡。仅仅因为你给文件一个.xls扩展名并没有使它成为一个Excel文件。



你需要将这些代码分成不同的方法。这个概念被称为关注点分离。现在,你有一个单一的方法,可以做任何事情。将其分解为只做一件事和一件事的方法。将某些数据导出到CSV文件的方法。一种组装电子邮件消息的方法。发送电子邮件的方法。 ...以及你需要的任何其他东西。



一旦你突破这个问题,很容易一遍又一遍地使用代码向很多人发送电子邮件如你所愿,你想要的任何内容。
First, you're not writing an Excel files. You're writing CSV (Comma Separated Values) text files, except the delimiter isn't a common, it's a tab. Just because you gave the files an ".xls" extension does not make it an Excel file.

You need to separate this code into distinct methods. The concept is called "separation of concerns". Right now, you have a monolithic method that does everything. Break this out into methods that do one thing and one ting only. A method that exports certain data to a CSV file. A method that assembles an email message. A method that sends the email message. ... and whatever else you need.

Once you have this broken out, it's pretty easy to use the code over and over again to send emails to as many people as you want with any content you want.


这篇关于邮件正在运行,但我想向每个不同的人发送两个excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆