C# 使用C在电子邮件中创建多个附件文件#
如何使用c#在电子邮件中附加多个文件 此代码仅用于附加一个文件。如何在c#2008中附加多个文件。???C# 使用C在电子邮件中创建多个附件文件#,c#,C#,如何使用c#在电子邮件中附加多个文件 此代码仅用于附加一个文件。如何在c#2008中附加多个文件。??? 请给我一个解决方案。可以将多个附件添加到邮件中。附件集合 C#: VB: 只需多次调用.Add,指向每个附件。可以将多个附件添加到邮件中。attachments集合 C#: VB: 只需多次调用.Add,指向每个附件。只需像上面那样向mail.attachments集合添加更多附件。只需像上面那样向mail.attachments集合添加更多附件。发送后释放附件文件怎么样 ... mail.
请给我一个解决方案。可以将多个附件添加到
邮件中。附件
集合
C#:
VB:
只需多次调用
.Add
,指向每个附件。可以将多个附件添加到邮件中。attachments
集合
C#:
VB:
只需多次调用
.Add
,指向每个附件。只需像上面那样向mail.attachments集合添加更多附件。只需像上面那样向mail.attachments集合添加更多附件。发送后释放附件文件怎么样
...
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;
...
例如,发送用于创建附件内容的临时文件。此文件为此目的重复使用。发布附件文件时,需要在附件上添加dispose()
要完成此操作,请首先创建附件,为其指定一个对象名,以便稍后与dispose()一起使用
发送后释放附件文件怎么样 例如,发送用于创建附件内容的临时文件。此文件为此目的重复使用。发布附件文件时,需要在附件上添加
dispose()
要完成此操作,请首先创建附件,为其指定一个对象名,以便稍后与dispose()一起使用
谢谢!你帮我做这个+1-我编辑了你的问题(对不起),因为你的解决方案是用vb编写的,我必须将它翻译成c#,正如问题所说的那样。但无论如何,非常感谢!谢谢!你帮我做这个+1-我编辑了你的问题(对不起),因为你的解决方案是用vb编写的,我必须将它翻译成c#,正如问题所说的那样。但无论如何,非常感谢@David如何才能添加动态数量的附件?@David如何才能添加动态数量的附件?
Message.Attachments.Add(new System.Net.Mail.Attachment(strAttPath));
Message.Attachments.Add(New Net.Mail.Attachment(strAttPath))
...
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;
...
Attachment attach = new Attachment(txtAttachments.Text.ToString());
Message.Attachments.Add(attach);
...
attach.dispose();
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();
}
}
}
}
//Exporting to CSV.
string FileName = $"YourFileName_{datetime}.csv";
File.WriteAllText(ExportPath + FileName, csv);
AttachmentsToExport.Add(ExportPath + FileName);