Asked by:
How to send calendar invite inside the email? I have working email and below is the code. Now i want to add calendar invite inside of it. Can someone help me to add it?

Question
-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Web.Mvc;
using webAppointment.Models;
namespace webAppointment.Controllers
{
public class EmailController : Controller
{
private string Message { get; set; }
// GET: Email
//Welcome method
public void Welcome()
{
Message = "Thank you for joining us! We will provide you the best service.";
}
//Cancellation Message
public void Cancel()
{
Message = "We appologies for your service cancellation!";
}
[HttpPost]
public ActionResult Form(string receiverEmail, string SelectSubject, string message)
{
try
{
if (ModelState.IsValid)
{
var From = new MailAddress("abc@gmail.com", "Test Mail");
var TO = new MailAddress(receiverEmail, "Receiver");
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential("from@gmail.com", "password")
};
using (var mess = new MailMessage(From, TO)
{
Subject = SelectSubject,
Body = message
})
{
smtp.Send(mess);
}
Response.Write("Email sent!");
return View();
}
}
catch (Exception)
{
ViewBag.Error = "Couldn't send email.";
}
return View();
}
}
}- Edited by Chintan_K Tuesday, July 3, 2018 8:56 PM
- Moved by Wendy ZangMicrosoft contingent staff Thursday, August 2, 2018 4:07 PM
Tuesday, July 3, 2018 8:54 PM
All replies
-
You will have to generate a file attachment with the .ics extension and add it to your message.
To add an attachment, you would create a "new Attachment(...)" and then add it to the Attachments collection of your variable "mess".
There is no automatic way as far as I know for creating the .ics invite, but is a relatively simple text file that you can generate with a few calls to a StreamWriter. You can see the format here:
Wednesday, July 4, 2018 5:44 AM -
Hi Chintan_K,
Thank you for posting here.
Since your question is more related to web, you could post a new thread in Azure and ASP.NET forum for suitable support.
https://forums.asp.net/1247.aspx/1?Azure+and+ASP+NET
The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.
Best Regards,
Wendy
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Wednesday, July 4, 2018 6:46 AM -
@AlbertoYou mean create a new file with extension.ics and include below code:
BEGIN:VCALENDAR VERSION:1.0 BEGIN:VEVENT CATEGORIES:MEETING STATUS:TENTATIVE DTSTART:19960401T033000Z DTEND:19960401T043000Z SUMMARY:Your Proposal Review DESCRIPTION:Steve and John to review newest proposal material CLASS:PRIVATE END:VEVENT END:VCALENDAR
and create a new method inside my existing file and call that method using variable mess? My main concern is to send invite to different people.
I am sorry I am not strong in programming.
Friday, July 6, 2018 1:27 PM -
Well, although you could create a temporary file and then attach it, it is more efficient to build the text in memory and then build the attachment in memory from the text. The recipient of the message will not notice any difference, as the attachment will be received exactly the same as if you had used a file for creating it.
If you take a look at the documentation of the Attachment class here:
https://msdn.microsoft.com/en-us/library/system.net.mail.attachment(v=vs.110).aspx
you will notice that there are a couple of constructors that accept a Stream instead of a file name. These let you build the attachment in memory. If you don't know how to use Streams, read a bit about them. They are quite useful, since you can pass any type of stream to any method that requires a Stream, such as the constructor for the Attachment. The example ni the documentation (linked above) uses a FileStream, but in your specific case, you will want to use a MemoryStream.
The content that you have to put inside the MemoryStream is the text for the .ics. And yes, you can build that text by concatenating the "fixed" parts with variables that contain your specific details for the appointment.
- Proposed as answer by Zhanglong WuMicrosoft contingent staff Thursday, July 12, 2018 8:11 AM
Friday, July 6, 2018 4:50 PM