Neo
Neo

Reputation: 16219

Email Attachment using MVC3

I need to send an email with an attachment in MVC3

i have send mail code but how can i attach file in MVC3 and send a mail

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);
        }

Upvotes: 0

Views: 1081

Answers (2)

Akash KC
Akash KC

Reputation: 16310

You can attach the file in following way:

To add the message string as attachement, you can simply do like this:

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

To add the file as attachment, you have to do like this:

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

Upvotes: 1

COLD TOLD
COLD TOLD

Reputation: 13569

ALL you need to do is define an atachment object because you still use 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

Upvotes: 1

Related Questions