데이터베이스에 저장된 텍스트 데이터를 이메일에 txt 첨부 파일로 여러 행 형식으로 보낼 수 있어야합니다. 다양한 시점에 다른 사람들이 무언가에 추가했다고 생각하십시오.메모리 파일을 여러 개 만들고 이메일에 첨부하기
현재 이메일은 전송되며 test0.txt 및 test1.txt처럼 생성 된 텍스트 파일이 있지만 비어 있습니다. 나는 streamwriter를 내뿜고있어 파일에 텍스트가 있어야하는 것처럼 보인다. 전자 메일을 보내기 전에 스트림 작성자를 닫을 수 없습니다. 왜냐하면 닫힌 스트림에서 읽을 수 없다는 오류가 발생하기 때문입니다. 나는 memorystream과 streamwriter를 컨테이너에 저장하므로 맨 끝까지 처리되거나 정리되지 않아야합니다. 전자 메일 개체가 손실되거나 어떤 이유로 컨테이너에 저장된 스트림에 액세스 할 수 없는지 궁금합니다.
나는 비슷한 질문을 한 것 같지만 작동하지 않는 것 같습니다. 대신 메모리에서이 난 그냥 임시 파일을 작성해야하는 것을 시도하고 첨부하는 등의
This person is using byte[] so only memory stream no streamwriter
This person disposes of their streamwriter before sending email which errors out for me
? 디스크에 쓰는 것이 느려서 첨부 파일을 첨부하기 위해 디스크에서 읽을 수 있습니다.
var companyEmail = new MailAddress("[email protected]", "Person Name");
email.To.Add(companyEmail);
email.From = new System.Net.Mail.MailAddress("[email protected]", "doesn't matter");
email.Subject = "subject";
email.Body = "body";
email.IsBodyHtml = true;
var nonAttCounter = 0;
var nonAttStreamHolder = new List<MemoryStream>();
var nonAttWriterHolder = new List<StreamWriter>();
//churn through the attachments and see if any of them are checked in the form
foreach (DataRow datarow in claim.attachments.Rows)
{
string cbFormName = "ctl00$MainBody$att" + datarow["attNum"].ToString();//name of checkbox controls on page and in form.
var includedInForm = rForm[cbFormName];
//see if the attachment was selected as one to include.ie the attNum is in the posted form.
if (includedInForm != null)
{
string origData = datarow["origData"].ToString();
string[] fIDs = origData.Split(',');
foreach (var item in fIDs)
{
//not all attachments are real attachments with files...cause why would attachments be attachments.
int fid;
bool isInt = int.TryParse(item, out fid);
if (isInt)
{
var tempDS = new datastore(ref fid);
var tempData = tempDS.blobData;
email.Attachments.Add(new Attachment(tempData, tempDS.fileNameWithExt));
}
else
{
//grab all the textual data from the database for this "attachment" and write it to a memory stream and upload to the email
nonAttStreamHolder.Add(new MemoryStream());
nonAttWriterHolder.Add(new StreamWriter(nonAttStreamHolder[nonAttCounter]));
nonAttWriterHolder[nonAttCounter].WriteLine("This is a test.");
nonAttWriterHolder[nonAttCounter].WriteLine("Why this no work?!");
nonAttWriterHolder[nonAttCounter].Flush();
//nonAttWriterHolder[nonAttCounter].Close();
System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Text.Plain);
System.Net.Mail.Attachment tempFile = new System.Net.Mail.Attachment(nonAttStreamHolder[nonAttCounter], ct);
tempFile.ContentDisposition.FileName = "test" + nonAttCounter + ".txt";
email.Attachments.Add(tempFile);
nonAttCounter++;
}
}
}
}
Global_Utilities.SharedFunctions.emailQuickSend(email);
foreach (var writer in nonAttWriterHolder)
{ writer.Close(); }
foreach (var stream in nonAttStreamHolder)
{ stream.Close(); }
왜 'StreamWriter'를 사용해야합니까? 당신은 당신이 링크 한'byte []'질문과 같은 것을 할 수있다; 'Encoding'을 사용하여'byte []'s에있는 문자열을 변환하십시오 ... –
@MikeMcCaughan은 그렇게 간단하게 똑똑합니다. 어쩌면 그것을 답으로 만들고 나는 그것을 최고의 답으로 표시 할 것입니다! – Kevin