Share via


Email Attachment using URL in C#

Question

Sunday, August 28, 2011 4:25 AM

Hello,

I want to attach documents  in the email. I have to get the document using url. I need to assign the Response( MyResponse) to attachment object so that I can send email. Thanks for your help

   string strSubject = "Subject";

            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();

            message.To.Add("[email protected]");

            message.Subject = strSubject;

            message.From = new System.Net.Mail.MailAddress("[email protected]");

            message.Body = "emailbody";

             HttpWebRequest MyRequest = (HttpWebRequest)WebRequest.Create("http://companyname.com/xyz/Book1.xlsx");

            try

            {

                MyRequest.Credentials = CredentialCache.DefaultCredentials;

                HttpWebResponse MyResponse = (HttpWebResponse)MyRequest.GetResponse();

 

                if (HttpStatusCode.OK == MyResponse.StatusCode)

                {

                  // Need help here

                   Attachment data = new  Attachment(MyResponse);  

                   

                }

            }

           message.Attachments.Add(data);

            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("0.0.0.0");

            smtp.Send(message);

All replies (5)

Sunday, August 28, 2011 5:58 AM âś…Answered | 2 votes

Hi

Try with this,

var stream = new WebClient().OpenRead("http location"

);

Attachment attachement = new Attachment(stream, ""

);

 

If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

 


Sunday, August 28, 2011 6:22 AM

Hi Kris444,

I got 401 error and I know how to handle that. IT worked.  Thanks for your time.

 


Monday, August 29, 2011 4:35 AM

Thanks Kris for your great help.

Hi SACSSP,

Welcome to MSDN Forum.

If you still have any concern about this issue or any difficulty in future programming, we welcome you to post here again.

Martin Xie [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Friday, August 2, 2013 7:13 AM

How you resolved, 401 error , Iam getting the same.


Friday, August 2, 2013 7:27 AM

 wb.UseDefaultCredentials = true;

resolved

Thanks