首先,我是 C# 的新手,如果有任何帮助或建议,我将不胜感激。在其中一个应用程序中,我正在使用一些数据,这些数据是从字符串 SQL 字符串创建的,并保存为 DataSet() 中的变量,而我想要完成的是将这些数据发送到 DataSet (); 作为附件 csv 扩展名,在这里我既有创建文件的时候,也有想通过电子邮件发送的时候,我也不想物理地创建 CSV 文件,我只想通过电子邮件发送保存的任何数据。任何帮助,将不胜感激。
public partial class CreateFile: Page
{
public string CreateData(string cdata)
{
string getVals = "";
try
{
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
Configuration rootwebconfig;
rootwebconfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/MyWebSiteRoot");
var conn = new SqlConnection();
var cmd = new SqlCommand();
SqlDataReader dr;
string yesno = "No";
string theName = "";
conn.ConnectionString = rootwebconfig.ConnectionStrings.ConnectionStrings["MyConnection"].ToString();
cmd = new SqlCommand("Select something" + dp_props.SelectedValue.ToString() + "'", conn);
conn.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
{
Configuration rootwebconfig1;
rootwebconfig1 = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/MyWebSiteRoot");
string sql = "Here creates the csv by selecting neccesary columns and calculations";
var connection1 = new SqlConnection();
connection1.ConnectionString = rootwebconfig1.ConnectionStrings.ConnectionStrings["MyConnection"].ToString();
var dataadapter1 = new SqlDataAdapter(sql, connection1);
var dss1 = new DataSet();
connection1.Open();
dataadapter1.Fill(dss1, "csvtable");
connection1.Close();
}
conn.Close();
}
catch (Exception ex)
{
getVals = ex.ToString();
}
return getVals;
}
public void SendEmail(string props)
{
try
{
// Sending the email
var mailMessage = new MailMessage();
mailMessage.From = new MailAddress("admin@something.com");
mailMessage.Subject = "This is a test" + props + DateTime.Now +;
System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(dss1);
mailMessage.Attachments.Add(attachment);
attachment.Name = "csvtable.csv";
mailMessage.Body = "Something";
mailMessage.IsBodyHtml = true;
mailMessage.To.Add(new MailAddress("user1@something.com"));
mailMessage.Bcc.Add(new MailAddress("user1@something.com.com"));
var smtp = new SmtpClient();
smtp.Host = "host.com";
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("username.com", "password");
smtp.Port = 999;
smtp.EnableSsl = true;
smtp.Send(mailMessage);
}
catch (Exception ex)
{
string smsg;
smsg = ex.ToString();
}
}
}