Attachment is not going with email

1.5k views Asked by At

And i am sending mails from below details :

// language -- C#
// import namespace
using System.Web.Mail;

private void SendEmail()
{
   const string SERVER = "relay-hosting.secureserver.net";
   MailMessage oMail = new System.Web.Mail.MailMessage();
   oMail.From = "emailaddress@domainname";
   oMail.To = "emailaddress@domainname";
   oMail.Subject = "Test email subject";
   oMail.BodyFormat = MailFormat.Html;  // enumeration
   oMail.Priority = MailPriority.High;  // enumeration
   oMail.Body = "Sent at: " + DateTime.Now;
   SmtpMail.SmtpServer = SERVER;
   SmtpMail.Send(oMail); 
   oMail = null;    // free up resources

}

Mails are going properly with above details and now i want to add attachment on email.And for that i have added below code :

String sFile = "http://www.demo.com/abc.pdf";
var oAttch = new System.Web.Mail.MailAttachment(sFile);

oMail.Attachments.Add(oAttch);

But it's not adding the attachment in mail.

It's giving error that "URI formats are not supported".

2

There are 2 answers

0
nsgocev On BEST ANSWER

Mail attachment supports only files from the local drive.

If you want to attach a file that is hosted on the web you should download it to your local drive first.

If you have the file in your local drive you can do something like this:

String sFile = "abc.pdf";
var oAttch = new System.Web.Mail.MailAttachment(Server.MapPath(sFile));

oMail.Attachments.Add(oAttch);
0
dknaack On

You cant use a file from the web. It must be located on your local drive.

Check out MSDN - MailAttachment Constructor (String)