Pawan Pawan - 3 months ago 19
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 = "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".

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