Send email with attachment from a specific url in C#












1















In my view, users can search for a document and once they get the result, they can click on its id and they can download the document from specific url based on id: http://test.com/a.ashx?format=pdf&id={0}



For example, if the id is 10, then url to download document will be: http://test.com/a.ashx?format=pdf&id=10, and when user click on it they are able to download the document.



This is how it looks like in my view:



 foreach (var item in Model)
{

<td>
<a href=@string.Format("http://test.com/a.ashx?format=pdf&id={0}",item.id)>
@Html.DisplayFor(modelItem => item.id)
</a>
</td>
}


And below is my controller action for SendEmail.



I am able to send email to the user. But i am having problem with sending attachments.
My question is: how can i attach the document that comes with the url to the email?



 public static bool SendEmail(string SentTo, string Text, string cc)
{

MailMessage msg = new MailMessage();

msg.From = new MailAddress("test@test.com");
msg.To.Add(SentTo);
msg.CC.Add(cc);
msg.Subject = "test";
msg.Body = Text;
msg.IsBodyHtml = true;

System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(???);
msg.Attachments.Add(attachment);

SmtpClient client = new SmtpClient("mysmtp.test.com", 25);

client.UseDefaultCredentials = false;
client.EnableSsl = false;
client.Credentials = new NetworkCredential("test", "test");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
//client.EnableSsl = true;

try
{
client.Send(msg);
}
catch (Exception)
{
return false;
}
return true;
}









share|improve this question




















  • 4





    What is "attach the document that comes with the url"? Do you want your code to download the URL contents and attach that file to the mail message?

    – CodeCaster
    Oct 14 '14 at 10:04











  • Yes, in this case my url content is a PDF.

    – sensahin
    Oct 14 '14 at 10:07






  • 2





    Do you have the PDF files on your server already, or are they generated? What part specifically are you having trouble with? See for example Get file to send as attachment from byte array.

    – CodeCaster
    Oct 14 '14 at 10:10













  • I am not sure how to define my pdf location in here: attachment = new System.Net.Mail.Attachment(???);. PDF files are not in server, they are generated.

    – sensahin
    Oct 14 '14 at 10:11








  • 1





    in that case you need to download the whole pdf in temporary folder and then attach it to email send code...

    – Shaz
    Oct 14 '14 at 10:21
















1















In my view, users can search for a document and once they get the result, they can click on its id and they can download the document from specific url based on id: http://test.com/a.ashx?format=pdf&id={0}



For example, if the id is 10, then url to download document will be: http://test.com/a.ashx?format=pdf&id=10, and when user click on it they are able to download the document.



This is how it looks like in my view:



 foreach (var item in Model)
{

<td>
<a href=@string.Format("http://test.com/a.ashx?format=pdf&id={0}",item.id)>
@Html.DisplayFor(modelItem => item.id)
</a>
</td>
}


And below is my controller action for SendEmail.



I am able to send email to the user. But i am having problem with sending attachments.
My question is: how can i attach the document that comes with the url to the email?



 public static bool SendEmail(string SentTo, string Text, string cc)
{

MailMessage msg = new MailMessage();

msg.From = new MailAddress("test@test.com");
msg.To.Add(SentTo);
msg.CC.Add(cc);
msg.Subject = "test";
msg.Body = Text;
msg.IsBodyHtml = true;

System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(???);
msg.Attachments.Add(attachment);

SmtpClient client = new SmtpClient("mysmtp.test.com", 25);

client.UseDefaultCredentials = false;
client.EnableSsl = false;
client.Credentials = new NetworkCredential("test", "test");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
//client.EnableSsl = true;

try
{
client.Send(msg);
}
catch (Exception)
{
return false;
}
return true;
}









share|improve this question




















  • 4





    What is "attach the document that comes with the url"? Do you want your code to download the URL contents and attach that file to the mail message?

    – CodeCaster
    Oct 14 '14 at 10:04











  • Yes, in this case my url content is a PDF.

    – sensahin
    Oct 14 '14 at 10:07






  • 2





    Do you have the PDF files on your server already, or are they generated? What part specifically are you having trouble with? See for example Get file to send as attachment from byte array.

    – CodeCaster
    Oct 14 '14 at 10:10













  • I am not sure how to define my pdf location in here: attachment = new System.Net.Mail.Attachment(???);. PDF files are not in server, they are generated.

    – sensahin
    Oct 14 '14 at 10:11








  • 1





    in that case you need to download the whole pdf in temporary folder and then attach it to email send code...

    – Shaz
    Oct 14 '14 at 10:21














1












1








1








In my view, users can search for a document and once they get the result, they can click on its id and they can download the document from specific url based on id: http://test.com/a.ashx?format=pdf&id={0}



For example, if the id is 10, then url to download document will be: http://test.com/a.ashx?format=pdf&id=10, and when user click on it they are able to download the document.



This is how it looks like in my view:



 foreach (var item in Model)
{

<td>
<a href=@string.Format("http://test.com/a.ashx?format=pdf&id={0}",item.id)>
@Html.DisplayFor(modelItem => item.id)
</a>
</td>
}


And below is my controller action for SendEmail.



I am able to send email to the user. But i am having problem with sending attachments.
My question is: how can i attach the document that comes with the url to the email?



 public static bool SendEmail(string SentTo, string Text, string cc)
{

MailMessage msg = new MailMessage();

msg.From = new MailAddress("test@test.com");
msg.To.Add(SentTo);
msg.CC.Add(cc);
msg.Subject = "test";
msg.Body = Text;
msg.IsBodyHtml = true;

System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(???);
msg.Attachments.Add(attachment);

SmtpClient client = new SmtpClient("mysmtp.test.com", 25);

client.UseDefaultCredentials = false;
client.EnableSsl = false;
client.Credentials = new NetworkCredential("test", "test");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
//client.EnableSsl = true;

try
{
client.Send(msg);
}
catch (Exception)
{
return false;
}
return true;
}









share|improve this question
















In my view, users can search for a document and once they get the result, they can click on its id and they can download the document from specific url based on id: http://test.com/a.ashx?format=pdf&id={0}



For example, if the id is 10, then url to download document will be: http://test.com/a.ashx?format=pdf&id=10, and when user click on it they are able to download the document.



This is how it looks like in my view:



 foreach (var item in Model)
{

<td>
<a href=@string.Format("http://test.com/a.ashx?format=pdf&id={0}",item.id)>
@Html.DisplayFor(modelItem => item.id)
</a>
</td>
}


And below is my controller action for SendEmail.



I am able to send email to the user. But i am having problem with sending attachments.
My question is: how can i attach the document that comes with the url to the email?



 public static bool SendEmail(string SentTo, string Text, string cc)
{

MailMessage msg = new MailMessage();

msg.From = new MailAddress("test@test.com");
msg.To.Add(SentTo);
msg.CC.Add(cc);
msg.Subject = "test";
msg.Body = Text;
msg.IsBodyHtml = true;

System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(???);
msg.Attachments.Add(attachment);

SmtpClient client = new SmtpClient("mysmtp.test.com", 25);

client.UseDefaultCredentials = false;
client.EnableSsl = false;
client.Credentials = new NetworkCredential("test", "test");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
//client.EnableSsl = true;

try
{
client.Send(msg);
}
catch (Exception)
{
return false;
}
return true;
}






c# asp.net-mvc mailmessage






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 14 '14 at 10:09







sensahin

















asked Oct 14 '14 at 10:02









sensahinsensahin

3982930




3982930








  • 4





    What is "attach the document that comes with the url"? Do you want your code to download the URL contents and attach that file to the mail message?

    – CodeCaster
    Oct 14 '14 at 10:04











  • Yes, in this case my url content is a PDF.

    – sensahin
    Oct 14 '14 at 10:07






  • 2





    Do you have the PDF files on your server already, or are they generated? What part specifically are you having trouble with? See for example Get file to send as attachment from byte array.

    – CodeCaster
    Oct 14 '14 at 10:10













  • I am not sure how to define my pdf location in here: attachment = new System.Net.Mail.Attachment(???);. PDF files are not in server, they are generated.

    – sensahin
    Oct 14 '14 at 10:11








  • 1





    in that case you need to download the whole pdf in temporary folder and then attach it to email send code...

    – Shaz
    Oct 14 '14 at 10:21














  • 4





    What is "attach the document that comes with the url"? Do you want your code to download the URL contents and attach that file to the mail message?

    – CodeCaster
    Oct 14 '14 at 10:04











  • Yes, in this case my url content is a PDF.

    – sensahin
    Oct 14 '14 at 10:07






  • 2





    Do you have the PDF files on your server already, or are they generated? What part specifically are you having trouble with? See for example Get file to send as attachment from byte array.

    – CodeCaster
    Oct 14 '14 at 10:10













  • I am not sure how to define my pdf location in here: attachment = new System.Net.Mail.Attachment(???);. PDF files are not in server, they are generated.

    – sensahin
    Oct 14 '14 at 10:11








  • 1





    in that case you need to download the whole pdf in temporary folder and then attach it to email send code...

    – Shaz
    Oct 14 '14 at 10:21








4




4





What is "attach the document that comes with the url"? Do you want your code to download the URL contents and attach that file to the mail message?

– CodeCaster
Oct 14 '14 at 10:04





What is "attach the document that comes with the url"? Do you want your code to download the URL contents and attach that file to the mail message?

– CodeCaster
Oct 14 '14 at 10:04













Yes, in this case my url content is a PDF.

– sensahin
Oct 14 '14 at 10:07





Yes, in this case my url content is a PDF.

– sensahin
Oct 14 '14 at 10:07




2




2





Do you have the PDF files on your server already, or are they generated? What part specifically are you having trouble with? See for example Get file to send as attachment from byte array.

– CodeCaster
Oct 14 '14 at 10:10







Do you have the PDF files on your server already, or are they generated? What part specifically are you having trouble with? See for example Get file to send as attachment from byte array.

– CodeCaster
Oct 14 '14 at 10:10















I am not sure how to define my pdf location in here: attachment = new System.Net.Mail.Attachment(???);. PDF files are not in server, they are generated.

– sensahin
Oct 14 '14 at 10:11







I am not sure how to define my pdf location in here: attachment = new System.Net.Mail.Attachment(???);. PDF files are not in server, they are generated.

– sensahin
Oct 14 '14 at 10:11






1




1





in that case you need to download the whole pdf in temporary folder and then attach it to email send code...

– Shaz
Oct 14 '14 at 10:21





in that case you need to download the whole pdf in temporary folder and then attach it to email send code...

– Shaz
Oct 14 '14 at 10:21












2 Answers
2






active

oldest

votes


















2














If the PDF file is generated on an external site, you will need to download it in some way, for this you can use WebClient:



var client = new WebClient();

// Download the PDF file from external site (pdfUrl)
// to your local file system (pdfLocalFileName)
client.DownloadFile(pdfUrl, pdfLocalFileName);


Then you can use it on Attachment:



attachment = new Attachment(pdfLocalFileName, MediaTypeNames.Application.Pdf);
msg.Attachments.Add(attachment)





share|improve this answer

































    1














    Consider using Postal It's open source libriary for ASP.Net MVC allowing you to send emails very easy. For example to attach file you can use such code:



    dynamic email = new Email("Example");
    email.Attach(new Attachment("c:\attachment.txt"));
    email.Send();


    Also you may want to use HangFire to send email in background, please take a look at the Sending Mail in Background with ASP.NET MVC



    Update: In order to get the PDF file path you can use Server.MapPath method to convert virtual path to the corresponding physical directory on the server.






    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26357987%2fsend-email-with-attachment-from-a-specific-url-in-c-sharp%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      If the PDF file is generated on an external site, you will need to download it in some way, for this you can use WebClient:



      var client = new WebClient();

      // Download the PDF file from external site (pdfUrl)
      // to your local file system (pdfLocalFileName)
      client.DownloadFile(pdfUrl, pdfLocalFileName);


      Then you can use it on Attachment:



      attachment = new Attachment(pdfLocalFileName, MediaTypeNames.Application.Pdf);
      msg.Attachments.Add(attachment)





      share|improve this answer






























        2














        If the PDF file is generated on an external site, you will need to download it in some way, for this you can use WebClient:



        var client = new WebClient();

        // Download the PDF file from external site (pdfUrl)
        // to your local file system (pdfLocalFileName)
        client.DownloadFile(pdfUrl, pdfLocalFileName);


        Then you can use it on Attachment:



        attachment = new Attachment(pdfLocalFileName, MediaTypeNames.Application.Pdf);
        msg.Attachments.Add(attachment)





        share|improve this answer




























          2












          2








          2







          If the PDF file is generated on an external site, you will need to download it in some way, for this you can use WebClient:



          var client = new WebClient();

          // Download the PDF file from external site (pdfUrl)
          // to your local file system (pdfLocalFileName)
          client.DownloadFile(pdfUrl, pdfLocalFileName);


          Then you can use it on Attachment:



          attachment = new Attachment(pdfLocalFileName, MediaTypeNames.Application.Pdf);
          msg.Attachments.Add(attachment)





          share|improve this answer















          If the PDF file is generated on an external site, you will need to download it in some way, for this you can use WebClient:



          var client = new WebClient();

          // Download the PDF file from external site (pdfUrl)
          // to your local file system (pdfLocalFileName)
          client.DownloadFile(pdfUrl, pdfLocalFileName);


          Then you can use it on Attachment:



          attachment = new Attachment(pdfLocalFileName, MediaTypeNames.Application.Pdf);
          msg.Attachments.Add(attachment)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Oct 14 '14 at 16:38

























          answered Oct 14 '14 at 10:21









          giacomelligiacomelli

          5,38922129




          5,38922129

























              1














              Consider using Postal It's open source libriary for ASP.Net MVC allowing you to send emails very easy. For example to attach file you can use such code:



              dynamic email = new Email("Example");
              email.Attach(new Attachment("c:\attachment.txt"));
              email.Send();


              Also you may want to use HangFire to send email in background, please take a look at the Sending Mail in Background with ASP.NET MVC



              Update: In order to get the PDF file path you can use Server.MapPath method to convert virtual path to the corresponding physical directory on the server.






              share|improve this answer






























                1














                Consider using Postal It's open source libriary for ASP.Net MVC allowing you to send emails very easy. For example to attach file you can use such code:



                dynamic email = new Email("Example");
                email.Attach(new Attachment("c:\attachment.txt"));
                email.Send();


                Also you may want to use HangFire to send email in background, please take a look at the Sending Mail in Background with ASP.NET MVC



                Update: In order to get the PDF file path you can use Server.MapPath method to convert virtual path to the corresponding physical directory on the server.






                share|improve this answer




























                  1












                  1








                  1







                  Consider using Postal It's open source libriary for ASP.Net MVC allowing you to send emails very easy. For example to attach file you can use such code:



                  dynamic email = new Email("Example");
                  email.Attach(new Attachment("c:\attachment.txt"));
                  email.Send();


                  Also you may want to use HangFire to send email in background, please take a look at the Sending Mail in Background with ASP.NET MVC



                  Update: In order to get the PDF file path you can use Server.MapPath method to convert virtual path to the corresponding physical directory on the server.






                  share|improve this answer















                  Consider using Postal It's open source libriary for ASP.Net MVC allowing you to send emails very easy. For example to attach file you can use such code:



                  dynamic email = new Email("Example");
                  email.Attach(new Attachment("c:\attachment.txt"));
                  email.Send();


                  Also you may want to use HangFire to send email in background, please take a look at the Sending Mail in Background with ASP.NET MVC



                  Update: In order to get the PDF file path you can use Server.MapPath method to convert virtual path to the corresponding physical directory on the server.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Oct 14 '14 at 10:35

























                  answered Oct 14 '14 at 10:24









                  Max BrodinMax Brodin

                  3,4911819




                  3,4911819






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26357987%2fsend-email-with-attachment-from-a-specific-url-in-c-sharp%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                      ComboBox Display Member on multiple fields

                      Is it possible to collect Nectar points via Trainline?