如何使用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();
此代码仅用于附加一个文件。我怎样才能在c#2008中附加多个文件。??? Plz给我解决方案。
答案 0 :(得分:4)
可以将多个附件添加到Message.Attachments
集合
C#:
Message.Attachments.Add(new System.Net.Mail.Attachment(strAttPath));
VB:
Message.Attachments.Add(New Net.Mail.Attachment(strAttPath))
只需多次拨打.Add
,指向每个附件。
答案 1 :(得分:4)
...
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;
...
答案 2 :(得分:2)
只需像上面那样在mail.Attachments集合中添加更多附件。
答案 3 :(得分:1)
发送后发布附件文件怎么样?
例如,您发送用于创建附件内容的临时文件。为了这个目的,该文件被重复使用。附件文件需要在附件上以dispose()
发布。
要完成此操作,请首先创建附件,以便为其提供一个对象名称,以便稍后与dispose()一起使用。
Attachment attach = new Attachment(txtAttachments.Text.ToString());
Message.Attachments.Add(attach);
...
attach.dispose();
答案 4 :(得分:0)
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();
}
}
}
}
参数列表附件由一个类填充,该类以.csv和.pdf的不同格式导出DataGridView。
列表“联系人”包含文件夹和文件名的字符串。
//Exporting to CSV.
string FileName = $"YourFileName_{datetime}.csv";
File.WriteAllText(ExportPath + FileName, csv);
AttachmentsToExport.Add(ExportPath + FileName);