0

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

2 Answers 2

1

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));
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.