genexus

Can I send a Calendar appointment via mail using Genexus?


I need to send an email via Genexus. I tried to send and email with an smtp object attaching an ICS file containing the appointment, but this is shown by Outlook as a normal attachment.

I also tried to add an header to the message &MailMessage.AddHeader("Content-Type","text/calendar")

but it's not working, the ICS file is always shown as attachment and not as event. If I open the attachment, outlook seems to read it well so I assume that the ics is structured correctly.

Am I doing something wrong or is it not possible to send events via genexus?


Solution

  • I didn't find a solution purely in Genexus since Outlook kept seeing the ics files as simple attachments. However, I wrote a csharp procedure that seems to work fine. It uses the Mailkit and MimeKit libraries

    var message = new MimeKit.MimeMessage();
    message.From.Add(new MimeKit.MailboxAddress("Bob", "bob@email.it"));
    message.To.Add(new MimeKit.MailboxAddress("Alice", "alice@email.it"));
    message.Subject = "Oggetto";
    
    // Build ICalendar
    System.Text.StringBuilder str = new System.Text.StringBuilder();
    str.AppendLine("BEGIN:VCALENDAR");
    str.AppendLine("VERSION:2.0");
    str.AppendLine("METHOD:REQUEST");
    str.AppendLine("X-WR-TIMEZONE:Europe/Berlin");
    str.AppendLine("BEGIN:VEVENT");
    str.AppendLine("DTSTART:20210215T100000");
    str.AppendLine("DTEND:20210219T110000");
    str.AppendLine("DTSTAMP:20250611T084058Z");
    str.AppendLine("UID:" + Guid.NewGuid().ToString());
    str.AppendLine("CREATED:20250611T084058Z");
    str.AppendLine("LAST-MODIFIED:20250611T084058Z");
    str.AppendLine("LOCATION:NYC");
    str.AppendLine("SEQUENCE:0");
    str.AppendLine("STATUS:CONFIRMED");
    str.AppendLine("SUMMARY:Titolo");
    str.AppendLine("TRANSP:OPAQUE");
    str.AppendLine("ORGANIZER;CN=Bob:mailto:bob@email.it");
    str.AppendLine("ATTENDEE;CN=Alice;RSVP=TRUE:mailto:alice@email.it");
    str.AppendLine("END:VEVENT");
    str.AppendLine("END:VCALENDAR");
    
    // Create the alternative body
    var plainTextPart = new MimeKit.TextPart("plain") { Text = "You received an invitation." };
    
    var calendarPart = new MimeKit.MimePart("text", "calendar")
    {
        Content = new MimeKit.MimeContent(new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(str.ToString())), MimeKit.ContentEncoding.SevenBit),
        ContentDisposition = new MimeKit.ContentDisposition(MimeKit.ContentDisposition.Inline),
        ContentTransferEncoding = MimeKit.ContentEncoding.SevenBit,
        FileName = "invite.ics"
    };
    calendarPart.ContentType.Parameters.Add("method", "REQUEST");
    calendarPart.ContentType.Charset = "UTF-8";
    
    // multipart/alternative body
    var multipart = new MimeKit.Multipart("alternative");
    multipart.Add(plainTextPart);
    multipart.Add(calendarPart);
    
    message.Body = multipart;
    
    // Send message
    using (var client = new MailKit.Net.Smtp.SmtpClient())
    {
        client.Connect("smtp.office365.com", 587, MailKit.Security.SecureSocketOptions.StartTls);
    
        var accessToken = "[MyAccessToken]";
        var oauth2 = new MailKit.Security.SaslMechanismOAuth2("bob@email.it", accessToken);
    
        client.Authenticate(oauth2);
        client.Send(message);
        client.Disconnect(true);
    }
    

    Objects have fully qualified name since Genexus does not read using

    The only flaw I found (but I think it's a more general problem) is that I can't send invitations to the email that sends the message