0

I am trying to send the .ics attachment I created earlier via webmail. Unfortunately I don't know how to convert it as a string array and even if I take a test string array it doesn't send the email out. Does anyone know what the problem is? Here is my code:

@{
  System.Text.StringBuilder str = new System.Text.StringBuilder();
            str.AppendLine("BEGIN:VCALENDAR");
            str.AppendLine("PRODID:-//Schedule a Meeting");
            str.AppendLine("VERSION:2.0");
            str.AppendLine("METHOD:REQUEST");
            str.AppendLine("BEGIN:VEVENT");
            str.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", DateTime.Now.AddMinutes(+330)));
            str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", DateTime.UtcNow));
            str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", DateTime.Now.AddMinutes(+660)));
            str.AppendLine("LOCATION: " + "abcd");
            str.AppendLine(string.Format("UID:{0}", Guid.NewGuid()));
            str.AppendLine(string.Format("DESCRIPTION:{0}", "MY DESCRIPTIOsN"));
            str.AppendLine(string.Format("X-ALT-DESC;FMTTYPE=text/html:{0}", "MY DESCRIPTION"));
            str.AppendLine(string.Format("SUMMARY:{0}", "SUBJECT"));
            str.AppendLine(string.Format("ORGANIZER:MAILTO:{0}", "Avenue 55"));

            str.AppendLine(string.Format("ATTENDEE;CN=\"{0}\";RSVP=TRUE:mailto:{1}", "msg.To[0].DisplayName", "msg.To[0].Address"));

            str.AppendLine("BEGIN:VALARM");
            str.AppendLine("TRIGGER:-PT15M");
            str.AppendLine("ACTION:DISPLAY");
            str.AppendLine("DESCRIPTION:Reminder");
            str.AppendLine("END:VALARM");
            str.AppendLine("END:VEVENT");
            str.AppendLine("END:VCALENDAR");

            byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(str.ToString());
            MemoryStream stream = new MemoryStream(byteArray);
           var filesList = new string [] { stream.ToString() };

           string body = "hey123";
            

            WebMail.Send(to: email,
                subject: "Hey111",
                body:  "test",
                filesToAttach: filesList);
    }
    
   
Mike Brind
  • 28,238
  • 6
  • 56
  • 88

1 Answers1

0

The System.Web.Helpers.WebMail Send method includes a filesToAttach parameter that takes a collection of strings representing the names of file to attach. It doesn't support adding a stream as an attachment. It was only ever designed to cater for simple scenarios.

You would be better off using the MailMessage and SmtpClient classes from System.Net.Mail and creating your email that way: Attach a file from MemoryStream to a MailMessage in C#

Mike Brind
  • 28,238
  • 6
  • 56
  • 88