Pawan Pawan - 11 months ago 64
C# Question

Attachment is not going with email

And i am sending mails from below details :

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

private void SendEmail()
const string SERVER = "";
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;
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 = "";
var oAttch = new System.Web.Mail.MailAttachment(sFile);


But it's not adding the attachment in mail.

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

Answer Source

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