我正在向单个电子邮件发送多个附件。但我的要求是使用foreach循环发送sendig文件。这里我通过创建两个附件内容和两个文件名来发送附件,这不是一个完美的代码。请帮我。提前谢谢你。
**Here it is send mail method:**
public static void SendMail(EmailDetails ObjEmailDtl){
try
{
SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = false;
string hostName = string.Empty;
using (eCoreDbEntities db = new eCoreDbEntities())
{
hostName = db.Parameters.Where(m => m.Name == "SMTP").FirstOrDefault().Value;
}
client.Host = hostName;
client.Port = 25;
MailMessage msg = new MailMessage();
msg.From = new MailAddress("contactus@karvyclick.com");
if (!string.IsNullOrEmpty(ObjEmailDtl.ToMailId))
{
string[] arrCC = ObjEmailDtl.ToMailId.Trim().Split(',');
foreach (var item in arrCC)
{
msg.To.Add(new MailAddress(item));
}
}
if (!string.IsNullOrEmpty(ObjEmailDtl.CCList))
{
string[] arrCC = ObjEmailDtl.CCList.Trim().Split(',');
foreach (var item in arrCC)
{
msg.CC.Add(new MailAddress(item));
}
}
msg.Subject = ObjEmailDtl.Subject;
msg.IsBodyHtml = true;
msg.Body = ObjEmailDtl.Body;
if (ObjEmailDtl.AttachmentContect != null)
{
Attachment att = new Attachment(new MemoryStream(ObjEmailDtl.AttachmentContect), ObjEmailDtl.AttachmentName);
msg.Attachments.Add(att);
}
if (ObjEmailDtl.AttachmentContect1 != null)
{
Attachment att = new Attachment(new MemoryStream(ObjEmailDtl.AttachmentContect1), ObjEmailDtl.AttachmentName1);
msg.Attachments.Add(att);
}
client.Send(msg);
}
catch (Exception ex)
{
Common.WriteLog("Common-SendMail", "", ex.Message);
}
}
这是我的模型类,我添加了多个附件内容名称和多个文件名
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Services.Models
{
public class CommonModel
{
public class EmailDetails
{
public string ToMailId { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
public string CCList { get; set; }
public byte[] AttachmentContect { get; set; }
public byte[] AttachmentContect1 { get; set; }
public string AttachmentName { get; set; }
public string AttachmentName1 { get; set; }
}
}
}
这是我创建文件的代码
byte[] file1 = Encoding.ASCII.GetBytes(str1.ToString());
byte[] File = Encoding.ASCII.GetBytes(str.ToString());
EmailDetails objEmail = new EmailDetails();
var tomail = db.Parameters.ToList().Where(m => m.Name == "ToList" &&
m.Category == "DynamicReport").FirstOrDefault().Value;
objEmail.ToMailId = tomail;
objEmail.AttachmentContect = File;
objEmail.AttachmentContect1 = file1;
objEmail.Subject = "Katalister Dynamic Report";
objEmail.Body = " Dear Leader,<br/><br/> Please find the attached Katalister Daily Report and Order Details Report.<br/><br/><br/><br/> Team Karvy";
objEmail.CCList = db.Parameters.ToList().Where(m => m.Name == "CCList" && m.Category == "DynamicReport").FirstOrDefault().Value;
objEmail.AttachmentName = "katalister_daily_report_" + DateTime.Now + ".xls";
objEmail.AttachmentName1 = "OrderDetails_BD.xls";
Common.SendMail(objEmail);
答案 0 :(得分:1)
您应该创建一个表示附件详细信息的类,如下所示。
public class AttachmentDetails
{
public byte[] Content {get;set;}
public string Name {get;set;}
}
然后在EmailDetails
类中使用此类对象的集合。
public class EmailDetails
{
public string ToMailId { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
public string CCList { get; set; }
public List<AttachmentDetails> Attachments {get;set;}
}
然后您应该按照以下方式填充附件。
byte[] file1 = Encoding.ASCII.GetBytes(str1.ToString());
byte[] File = Encoding.ASCII.GetBytes(str.ToString());
EmailDetails objEmail = new EmailDetails();
var tomail = db.Parameters.ToList().Where(m => m.Name == "ToList" &&
m.Category == "DynamicReport").FirstOrDefault().Value;
objEmail.ToMailId = tomail;
objEmail.Attachments = new List<AttachmentDetails>();
var attachment = new AttachmentDetails();
attachment.Name = "katalister_daily_report_" + DateTime.Now + ".xls";
attachment.Content = File;
objEmail.Attachments.Add(attachment);
attachment = new AttachmentDetails();
attachment.Name = "OrderDetails_BD.xls";
attachment.Content = file1;
objEmail.Attachments.Add(attachment);
objEmail.Subject = "Katalister Dynamic Report";
objEmail.Body = " Dear Leader,<br/><br/> Please find the attached Katalister Daily Report and Order Details Report.<br/><br/><br/><br/> Team Karvy";
objEmail.CCList = db.Parameters.ToList().Where(m => m.Name == "CCList" && m.Category == "DynamicReport").FirstOrDefault().Value;
然后在Attachment
方法中循环SendMail
属性,如下所示。
//Skipping the other code before this in the method.
msg.Subject = ObjEmailDtl.Subject;
msg.IsBodyHtml = true;
msg.Body = ObjEmailDtl.Body;
foreach(var attachment in ObjEmailDtl.Attachments)
{
Attachment att = new Attachment(new MemoryStream(attachment.Content), attachment.Name);
msg.Attachments.Add(att);
}
// Other code in the method.
这可以帮助您解决问题。