使用 C# 的电子邮件中的多个附件文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1457940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple attachment file in email using C#
提问by nithi
How to attach the multiple files in an email using c#.
如何使用 c# 在电子邮件中附加多个文件。
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
//get the userID, Pass
userID= register.userName;
password = register.pass;
string aa=txtTo.Text;
mail.From = new MailAddress(userID);
mail.To.Add(aa);
mail.Subject = txtsubject.Text;
mail.Body = txtComments.Text;
//Attach file
mail.Attachments.Add(new Attachment(txtAttachments.Text.ToString()));
SmtpServer.Port = 587;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = new System.Net.NetworkCredential(userID, password);
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("Email sent successfully");
this.Cursor = Cursors.Default;
//close the page
Email email = new Email();
email.Close();
this code is used for attach one file only. How can i attach multiple files in c# 2008.??? Plz give me the solution.
此代码仅用于附加一个文件。如何在 c# 2008 中附加多个文件。???请给我解决方案。
采纳答案by David
...
mail.Body = txtComments.Text;
//Attach file
mail.Attachments.Add(new Attachment(txtAttachments.Text.ToString()));
mail.Attachments.Add(new Attachment(txtAttachments2.Text.ToString()));
mail.Attachments.Add(new Attachment(txtAttachments3.Text.ToString()));
mail.Attachments.Add(new Attachment(txtAttachments4.Text.ToString()));
SmtpServer.Port = 587;
...
回答by Mitch Wheat
Multiple attachments can be added to the Message.Attachments
collection
可以将多个附件添加到Message.Attachments
集合中
C#:
C#:
Message.Attachments.Add(new System.Net.Mail.Attachment(strAttPath));
VB:
VB:
Message.Attachments.Add(New Net.Mail.Attachment(strAttPath))
Simply call .Add
multiple times, pointing to each attachment.
只需.Add
多次调用,指向每个附件。
回答by phoebus
Just add more attachments to the mail.Attachments collection as you did above.
只需像上面一样向 mail.Attachments 集合中添加更多附件即可。
回答by dpminusa
What about releasing the attachment files after the send?
发送后释放附件怎么办?
For example, you send a temporary file used to create the attachment content. This file is used repetitively for this purpose ongoing. The attachment file will need to be released with a dispose()
on the attachment.
例如,您发送一个用于创建附件内容的临时文件。该文件被反复用于此目的。附件文件将需要dispose()
在附件上发布。
To accomplish this create the attachment first to give it an object name to use with dispose() later.
要完成此操作,请先创建附件,然后为其指定一个对象名称,以便稍后与 dispose() 一起使用。
Attachment attach = new Attachment(txtAttachments.Text.ToString());
Message.Attachments.Add(attach);
...
attach.dispose();
回答by J. Schaefer
protected void SendMail(List<string> attachments)
{
UserManagement Users = new UserManagement();
Users.GetUserInformation();
SmtpClient client = new SmtpClient(ip_address);
MailMessage Message = new MailMessage();
Message.From = new MailAddress(senderaddress);
Message.To.Add(Users._CurUser_Destination_Email);
Message.Subject = "Neue Umlagerung - " + cb_auflieger_limburg.SelectedItem.ToString();
Message.Body = string.Format("Datum: {0}", DateTime.Now) + Environment.NewLine +
"AufliegerNr.: " + cb_auflieger_limburg.SelectedItem.ToString() + Environment.NewLine +
"Benutzer: " + Environment.UserName;
client.UseDefaultCredentials = true;
Attachment Attachment = null;
try
{
foreach (string attachment in attachments)
{
Attachment = new Attachment(attachment);
Message.Attachments.Add(Attachment);
}
client.Send(Message);
Attachment.Dispose();
Message.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
foreach(string attachment in attachments)
{
//Dateien nach Versendung l?schen
FileInfo fi = new FileInfo(attachment);
if (fi.Exists)
{
fi.Delete();
}
}
}
}
The parameter List attachmentsgets filled by a class that exports a DataGridView in different formats like .csv and .pdf
参数列表附件由一个类填充,该类以不同的格式(如 .csv 和 .pdf)导出 DataGridView
The List "atttachments" contains the strings of the folder and filename.
列表“附件”包含文件夹和文件名的字符串。
//Exporting to CSV.
string FileName = $"YourFileName_{datetime}.csv";
File.WriteAllText(ExportPath + FileName, csv);
AttachmentsToExport.Add(ExportPath + FileName);