使用MVC3发送电子邮件附件

时间:2012-07-20 05:37:52

标签: c# sendmail

我需要在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);
        }

2 个答案:

答案 0 :(得分:1)

您可以按以下方式附加文件:

要将消息字符串添加为附件,您可以这样做:

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

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

  string file = "test.xls";
  message.Attachments.Add(new Attachment(file, MediaTypeNames.Application.Octet));

答案 1 :(得分: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