我正在使用一个库来生成ics文件(iCalendar或RFC 2445,或者你称之为),这些库将内容序列化为MemoryStream,或者实际上是任何类型的流.
这是我的一大堆代码:
public ActionResult iCal(int id) {
MyApp.Event kiEvt = evR.Get(id);
// Create a new iCalendar
iCalendar iCal = new iCalendar();
// Create the event, and add it to the iCalendar
DDay.iCal.Components.Event evt = iCal.Create<DDay.iCal.Components.Event>();
// Set information about the event
evt.Start = kiEvt.event_date;
evt.End = evt.Start.AddHours(kiEvt.event_duration); // This also sets the duration
evt.Description = kiEvt.description;
evt.Location = kiEvt.place;
evt.Summary = kiEvt.title;
// Serialize (save) the iCalendar
iCalendarSerializer serializer = new iCalendarSerializer(iCal);
System.IO.MemoryStream fs = new System.IO.MemoryStream();
serializer.Serialize(fs, System.Text.Encoding.UTF8); …Run Code Online (Sandbox Code Playgroud) 我能够通过邮件发送附件,但附件内容为空白,大小显示为0字节.
在通过互联网进行一些搜索后发现我们需要将内存流位置重置为0才能从头开始.
我也试过了,但它似乎无法正常工作.你能帮忙吗?
请在下面找到我的代码段:
注意:我能够保存工作簿,并且数据存在于保存的工作簿中.
MemoryStream memoryStream = new MemoryStream();
StreamWriter writer = new StreamWriter(memoryStream);
writer.Write(xlWorkbook);
writer.Flush();
memoryStream.Position = 0;
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtpclient");
mail.From = new MailAddress("from@gmail.com");
mail.To.Add("To@gmail.com");
mail.Subject = "Entry";
mail.Body = "Hello, PFA ";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(memoryStream,"xls");
attachment.ContentDisposition.FileName = "Input" + DateTime.Now.ToString("yyyyMMdd_hhss") + ".xls";
mail.Attachments.Add(attachment);
SmtpServer.Port = 465;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = new System.Net.NetworkCredential("Username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
writer.Dispose();
Run Code Online (Sandbox Code Playgroud) 我在电子邮件中遇到附件问题.每隔几天,用户就无法在电子邮件中找到预期的附件.这似乎发生了大约10-20分钟,然后它纠正了自己意味着后来的电子邮件将包含附件.我不确定这背后的原因是什么.这就是我的代码的样子
模型
public class EmailAttachment
{
public string FileName { get; set; }
public byte[] FileContent { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
代码触发器发送电子邮件
var emailAttachment= new EmailAttachment();
emailAttachment.FileContent = CreatePDFFile();
emailAttachment.FileName = "file.pdf";
EmailGeneratedCertificate(emailAttachment);
Run Code Online (Sandbox Code Playgroud)
电子邮件准备代码
public void EmailGeneratedCertificate(EmailAttachment file)
{
//file.FileContent is a byte array
var ms = new MemoryStream(file.FileContent);
ms.Position = 0;
var contentType = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Application.Pdf);
var from = "xx@x.com";
var fromTargetName = "XXX";
var recepient="xx2@x.com"
var subject = "Attachment";
var body="<strong>Please find attachment.</strong>"
var attachment = new Attachment(ms, …Run Code Online (Sandbox Code Playgroud)