0

我需要在 MVC3 中发送带有附件的电子邮件

我有发送邮件代码,但是如何在 MVC3 中附加文件并发送邮件

public void SendConfirmationEmail(string file)
        {
            string verifyUrl = //need to send that file URL code saving that file  
            //on server drive
            var message = new MailMessage("YOUR_USER_ACCOUNT_HERE@YOUR_DOMAIN_HERE", "SENDERS EMAIL ID")
            {
                Subject = "Please confirm attachment",
                Body = verifyUrl
                //Also send attachment file code
            };
            var client = new SmtpClient();
            client.Send(message);
        }
4

2 回答 2

1

您可以通过以下方式附加文件:

要将消息字符串添加为附件,您可以简单地执行以下操作:

message.Attachments.Add(new Attachment("message"));

要将文件添加为附件,您必须这样做:

  string file = "test.xls";
  message.Attachments.Add(new Attachment(file, MediaTypeNames.Application.Octet));
于 2012-07-20T05:42:12.597 回答
1

您需要做的就是定义一个附件对象,因为您仍然使用 System.net.mail

Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
message.Attachments.Add(data);

http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.attachments.aspx

于 2012-07-20T05:43:46.960 回答