如何上传多个文件并作为附件发送到电子邮件地址 [英] How Do I Upload Multiple Files And Send To Emails Address As An Attachment
本文介绍了如何上传多个文件并作为附件发送到电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在发送多个文件作为电子邮件地址的附件时遇到问题,发送成功,但文件附件是0Kb!
以下是我的代码:
i am having issues sending multiple files as an attachment to email address, the sending is successful, but the file attachments are 0Kb!
below is my code:
foreach (HttpPostedFile postedFile in FileUpload1.PostedFiles)
{
if (postedFile != null && postedFile.ContentLength > 0)
{
string filename = Path.GetFileName(postedFile.FileName);
string contentType = postedFile.ContentType;
int size = FileUpload1.PostedFile.ContentLength;
using (Stream fs = postedFile.InputStream)
{
using (var br = new BinaryReader(fs))
{
byte[] bytes = br.ReadBytes((Int32) fs.Length);
DataAccessLayer.SaveFile(filename, contentType, transNumber, size, bytes);
Sendmail(email, filename);
}
}
}
}
电子邮件代码:
Email code:
public void Sendmail(string mailTo, string subject)
{
if(FileUpload1.HasFiles)
{
try
{
var mail = new MailMessage();
var smtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("removed", "Interview study material");
mail.To.Add(mailTo);
mail.Subject = subject;
mail.Body = "Please find attachment document!";
string strFileName =
Path.GetFileName(FileUpload1.PostedFile.FileName);
var attachFile = new Attachment(FileUpload1.PostedFile.InputStream, strFileName);
mail.Attachments.Add(attachFile);
smtpServer.Port = 587;
smtpServer.Credentials = new NetworkCredential("removed", "removed");
smtpServer.EnableSsl = true;
smtpServer.Send(mail);
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
推荐答案
只需阅读以下文章并希望它有所帮助< br $> b $ b
http://www.c-sharpcorner.com/UploadFile/47548d/sending-mail-with-attachment-and-multiple-file-upload-using/ [ ^ ]
Just go through the below article and hope it helps
http://www.c-sharpcorner.com/UploadFile/47548d/sending-mail-with-attachment-and-multiple-file-upload-using/[^]
这篇关于如何上传多个文件并作为附件发送到电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文