2

如何将多个文件附加到发送电子邮件。

我使用以下方法发送电子邮件。

   public static void sendMail(string to, string from, string password, string subject, string body, int dep_code)
        {
                MailMessage mail = new MailMessage();
                SmtpClient smtp = new SmtpClient();
                if (to == "")
                    to = "-------";
                MailAddressCollection m = new MailAddressCollection();
                m.Add(to);
                mail.Subject = subject;
                mail.From = new MailAddress(from);

                string banneredBody = @"<table width='100%' border='0' dir='rtl'>" +
                                          "<tr>" +
                                            "<td align='center'><img src=cid:Image1  /></td>" +
                                          "</tr>" +
                                          "<tr>" +
                                            "<td align='center'>" + body + "</td>" +
                                          "</tr>" +
                                       "</table>";

                mail.Body = banneredBody;
                //mail.Body = body;
                mail.IsBodyHtml = true;
                mail.ReplyTo = new MailAddress(from);
                mail.To.Add(m[0]);
                smtp.Host = "....";
                smtp.Port = 25;
                smtp.EnableSsl = false;
                smtp.Credentials = new System.Net.NetworkCredential(from, password);
                ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };

                smtp.Send(mail);

        }

如何传递文件参数来附加它们。

我使用RadAsyncUpload

 private List<Telerik.Web.UI.UploadedFileInfo> uploadedFiles = new List<Telerik.Web.UI.UploadedFileInfo>();
        public List<Telerik.Web.UI.UploadedFileInfo> UploadedFiles
        {
            get { return uploadedFiles; }
            set { uploadedFiles = value; }
        }

 private void PopulateUploadedFilesList()
        {
            foreach (UploadedFile file in rad_upload.UploadedFiles)
            {
                UploadedFileInfo uploadedFileInfo = new UploadedFileInfo(file);
                UploadedFiles.Add(uploadedFileInfo);
            }
        }
4

2 回答 2

3

您应该使用以下Attachments属性:

foreach (var file in UploadedFiles)
{
    mail.Attachments.Add(file);
}

顺便说一句,您可以使用MailAddress指定要将邮件发送到的地址:

mail.To.Add(new MailAddress(to));
于 2012-11-25T18:37:42.937 回答
1

MailMessage有一个名为Attachments...的属性...您需要添加要发送到该属性的所有附件...一些示例源代码和解释可以在 MSDN此处此处找到

于 2012-11-25T17:08:54.560 回答