2

I've written a mail sending code for asp.net. it is working fine. I can attach files that are saved on disk. Now I want to attach a file that is on the internet. I can download the file and save it somewhere on disk and attach the file. but I don't want to save the file on disk. I just need to download the file in memory and attach it to my mail. need help to do that.

here is my code for sending email

    public void SendMail(string To, string Subject, string Body)
    {
        MailMessage mail = new MailMessage();

        mail.From = new MailAddress("noreply@xyz.com", "xyz");
        mail.To.Add(new MailAddress(To));
        mail.Subject = Subject;

        mail.Body = Body;
        mail.IsBodyHtml = true;

        using (SmtpClient smtpClient = new SmtpClient())
        {
            try
            {
                smtpClient.Send(mail);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

I want some thing like this

    public void SendMail(string To, string Subject, string Body, URI onlineFileURI)
    {
        MailMessage mail = new MailMessage();

        mail.From = new MailAddress("noreply@xyz.com", "xyz");
        mail.To.Add(new MailAddress(To));
        mail.Subject = Subject;

        mail.Body = Body;
        mail.IsBodyHtml = true;

        //Create the attachment from URL
        var attach = [Attachemnt from URL]

        mail.Attachments.Add(attach)

        using (SmtpClient smtpClient = new SmtpClient())
        {
            try
            {
                smtpClient.Send(mail);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

How I can download the file in memory and attach it?

Krishanu Dey
  • 6,326
  • 7
  • 51
  • 69

2 Answers2

3

This is how I ended up doing it based of previous posts:

var url = "myurl.com/filename.jpg"
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
using (HttpWebResponse HttpWResp = (HttpWebResponse)req.GetResponse())
using (Stream responseStream = HttpWResp.GetResponseStream())
using (MemoryStream ms = new MemoryStream())
{
    responseStream.CopyTo(ms);
    ms.Seek(0, SeekOrigin.Begin);
    Attachment attachment = new Attachment(ms, filename, MediaTypeNames.Image.Jpeg);
    message.Attachments.Add(attachment);
    _smtpClient.Send(message);
}
Sebastian
  • 925
  • 9
  • 11
0

You can do it by converting page into Stream/byte array and send it

Here is the code

string strReportUser = "RSUserName";
string strReportUserPW = "MySecretPassword";
string strReportUserDomain = "DomainName";

string sTargetURL = "http://SqlServer/ReportServer?" +
   "/MyReportFolder/Report1&rs:Command=Render&rs:format=PDF&ReportParam=" +
   ParamValue;

HttpWebRequest req =
      (HttpWebRequest)WebRequest.Create( sTargetURL );
req.PreAuthenticate = true;
req.Credentials = new System.Net.NetworkCredential(
    strReportUser,
    strReportUserPW,
    strReportUserDomain );

HttpWebResponse HttpWResp = (HttpWebResponse)req.GetResponse();

Stream fStream = HttpWResp.GetResponseStream();

HttpWResp.Close();

//Now turn around and send this as the response..
ReadFullyAndSend( fStream );
ReadFullyAnd send method. NB: the SendAsync call so your not waiting for the server to send the email completely before you are brining the user back out of the land of nod.

public static void ReadFullyAndSend( Stream input )
{
   using ( MemoryStream ms = new MemoryStream() )
   {
      input.CopyTo( ms );

        MailMessage message = new MailMessage("from@foo.com", "too@foo.com");
            Attachment attachment = new Attachment(ms, "my attachment",, "application/vnd.ms-excel");
            message.Attachments.Add(attachment);
            message.Body = "This is an async test.";

            SmtpClient smtp = new SmtpClient("localhost");
            smtp.Credentials = new NetworkCredential("foo", "bar");
            smtp.SendAsync(message, null);
   }
} 

courtesy:Get file to send as attachment from byte array

Community
  • 1
  • 1
Nipun Ambastha
  • 2,553
  • 1
  • 16
  • 26