提问人:ibrahimerd 提问时间:8/20/2023 最后编辑:tripleeeibrahimerd 更新时间:8/20/2023 访问量:13
使用 MVC5 发送邮件时的附件问题
Attached file issue when sending mail with MVC5
问:
我想向客户发送活动邮件。附有一个PDF文件。
我的联系人列表是一个 MS SQL 表。我曾经选择每个收件人。所有电子邮件都有一个附件,但只有第一封电子邮件具有正确的文件;其他人的大小是 0。
我检查了调试模式,但我无法弄清楚。foreach
public void SendEmail(HttpPostedFileBase Attachedfile)
{
var smtpHost = GetSmtpInfo("SmtpHost");
var smtpEmailAccount = GetSmtpInfo("SmtpEmailAccount");
var smtpPassword = GetSmtpInfo("SmtpPassword");
int smptPort = Convert.ToInt32(GetSmtpInfo("SmtpPort"));
string subject = "Test Attached Mail";
string body = "Test Mail with attachement";
foreach (var item in db.TblContact)
{
using (MailMessage mailMessage = new MailMessage())
{
mailMessage.From = new MailAddress(smtpEmailAccount);
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = true;
mailMessage.To.Add(new MailAddress(item.Email));
if (Attachedfile != null)
{
string fileName = Path.GetFileName(Attachedfile.FileName);
mailMessage.Attachments.Add(new Attachment(Attachedfile.InputStream, fileName));
}
SmtpClient smtp = new SmtpClient();
smtp.Host = smtpHost;
smtp.EnableSsl = false;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = smtpEmailAccount;
NetworkCred.Password = smtpPassword;
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = smptPort;
try
{
smtp.Send(mailMessage);
ViewBag.success = "Email Sent";
}
catch (Exception ex)
{
ViewBag.error = "Error sending mail. Reason : " + ex.Message;
}
}
}
}
我需要向每个客户发送一封单独的电子邮件。
答: 暂无答案
评论