简体   繁体   English

使用C#创建ics文件并发送带有附件的电子邮件

[英]Create ics file and send email with Attachment using c#

I need to send calendar appointment email attached with ics file contains appointment details, I am getting Mails but Attachment is missing help me, someone to overcome this 我需要发送带有ics文件的日历约会电子邮件,附件中包含约会详细信息,我收到邮件,但是缺少附件帮助我,有人可以解决此问题

Need to create simple ics file and attach it with the email. 需要创建简单的ics文件,并将其与电子邮件一起附加。

Please Look at below code as I tried 我尝试时请看下面的代码

   public IHttpActionResult cal(calendar objApptEmail)
    {
        MailMessage msg = new MailMessage();
        //Now we have to set the value to Mail message properties

        //Note Please change it to correct mail-id to use this in your application
        msg.From = new MailAddress("ssss@gmail.com", "ABC");
        msg.To.Add(new MailAddress("ssss@outlook.com", "BCD"));
        //  msg.CC.Add(new MailAddress("zzzzz@xyz.com", "DEF"));// it is optional, only if required
        //   msg.Headers.Add("Content-class", "urn:content-classes:calendarmessage");
        msg.Subject = "Send mail with ICS file as an Attachment";
        msg.Body = "Please Attend the meeting with this schedule";

        // Now Contruct the ICS file using string builder
        StringBuilder str = new 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: " + objApptEmail.Location);
        str.AppendLine(string.Format("UID:{0}", Guid.NewGuid()));
        str.AppendLine(string.Format("DESCRIPTION:{0}", msg.Body));
        str.AppendLine(string.Format("X-ALT-DESC;FMTTYPE=text/html:{0}", msg.Body));
        str.AppendLine(string.Format("SUMMARY:{0}", msg.Subject));
        str.AppendLine(string.Format("ORGANIZER:MAILTO:{0}", msg.From.Address));

        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");

        System.Net.Mime.ContentType contype = new System.Net.Mime.ContentType("text/calendar");
        contype.Parameters.Add("method", "REQUEST");
        //  contype.Parameters.Add("name", "Meeting.ics");
        AlternateView avCal = AlternateView.CreateAlternateViewFromString(str.ToString(), contype);
        msg.AlternateViews.Add(avCal);

        //Now sending a mail with attachment ICS file.                     
        System.Net.Mail.SmtpClient smtpclient = new System.Net.Mail.SmtpClient();
        smtpclient.Host = "smtp.gmail.com"; //-------this has to given the Mailserver IP
        smtpclient.EnableSsl = true;
        smtpclient.Credentials = new System.Net.NetworkCredential("dddd@gmail.com", "xxxxx");
        smtpclient.Send(msg);
        return Ok();       

}

I removed all the code and enter the code after testing it on my machine. 我删除了所有代码,并在我的机器上对其进行测试后输入了代码。 This is working fine in my case. 就我而言,这很好。 In your case it's not working try to setup Less secure app in Gmail (I see you are using gmail to send emails) 您的情况不起作用,请尝试在Gmail中设置安全性较低的应用程序(我看到您正在使用gmail发送电子邮件)

System.Net.Mail.MailMessage msg = new MailMessage("anirudhagupta01@example.com","anirudha@testing.com");

            StringBuilder str = new 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}", msg.Body));
            str.AppendLine(string.Format("X-ALT-DESC;FMTTYPE=text/html:{0}", msg.Body));
            str.AppendLine(string.Format("SUMMARY:{0}", msg.Subject));
            str.AppendLine(string.Format("ORGANIZER:MAILTO:{0}", msg.From.Address));

            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 = Encoding.ASCII.GetBytes(str.ToString());
            MemoryStream stream = new MemoryStream(byteArray);

            Attachment attach = new Attachment(stream,"test.ics");

            msg.Attachments.Add(attach);

            System.Net.Mime.ContentType contype = new System.Net.Mime.ContentType("text/calendar");
            contype.Parameters.Add("method", "REQUEST");
            //  contype.Parameters.Add("name", "Meeting.ics");
            AlternateView avCal = AlternateView.CreateAlternateViewFromString(str.ToString(), contype);
            msg.AlternateViews.Add(avCal);

            //Now sending a mail with attachment ICS file.                     
            System.Net.Mail.SmtpClient smtpclient = new System.Net.Mail.SmtpClient();
            smtpclient.Host = "smtp.gmail.com"; //-------this has to given the Mailserver IP
            smtpclient.EnableSsl = true;
            smtpclient.Credentials = new System.Net.NetworkCredential("anirudhagupta0011@gmail.com", "testing");
            smtpclient.Send(msg);

I once had the same requirement and went well with the assembly Ical.Net. 我曾经有过相同的要求,并且与程序集Ical.Net配合得很好。

With this library you dont need to parse all by hand but can use "normal programming" :) 有了这个库,您不需要手动解析所有内容,但可以使用“常规编程” :)

using Ical.Net;
using Ical.Net.DataTypes;
using Ical.Net.Serialization.iCalendar.Serializers;

private static Calendar CreateCalendarEntry(DateTime? start, DateTime? end, string title, string description, string location)
{
    Calendar iCal = new Calendar();
    iCal.Method = "PUBLISH";
    // Create the event, and add it to the iCalendar
    Event evt = iCal.Create<Event>();
    // Set information about the event
    evt.Start = new CalDateTime(start.Value);
    evt.End = new CalDateTime(end.Value); // This also sets the duration  
    evt.Description = description;
    evt.Location = location;
    evt.Summary = title;
    // Create a reminder 24h before the event
    Alarm reminder = new Alarm();
    reminder.Action = AlarmAction.Display;
    reminder.Trigger = new Trigger(new TimeSpan(-24, 0, 0));
    evt.Alarms.Add(reminder);

    return iCal;
}

You can download the assembly here 您可以在此处下载程序集

After creating the Calendar , you can easily send it by mail via a stream. 创建Calendar ,您可以轻松地通过流通过邮件发送Calendar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM