-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilaAttachment.cs
89 lines (74 loc) · 2.85 KB
/
FilaAttachment.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System.Web.Mvc;
using System.IO;
using System.Net.Mail;
using System.Net;
using System.Text;
[HttpPost]
public ActionResult SendEmail()
{
try
{
System.Web.HttpFileCollectionBase files = Request.Files;
System.Net.Mail.MailMessage _objMail = new System.Net.Mail.MailMessage();
string FromId = "[email protected]";
// Set properties needed for the email
_objMail.From = new MailAddress(FromId);
string[] ToId = Request.Params["EmailTo"].Split(';');
foreach (string ToEmail in ToId)
{
_objMail.To.Add(new MailAddress(ToEmail)); //Adding Multiple To email Id
}
if (Request.Params["EmailCC"] != null && Request.Params["EmailCC"] != "")
{
string[] CCId = Request.Params["EmailCC"].Split(';');
foreach (string CcEmail in CCId)
{
_objMail.CC.Add(new MailAddress(CcEmail)); //Adding Multiple CC email Id
}
}
if (Request.Params["EmailBCC"] != null && Request.Params["EmailBCC"] != "")
{
string[] CCId = Request.Params["EmailBCC"].Split(';');
foreach (string EmailBccd in CCId)
{
_objMail.Bcc.Add(new MailAddress(EmailBccd)); //Adding Multiple CC email Id
}
}
_objMail.Subject = Request.Params["EmailSubject"];
_objMail.SubjectEncoding = Encoding.UTF8;
_objMail.Body = Request.Params["EmailMessage"];
_objMail.IsBodyHtml = true;
_objMail.Priority = MailPriority.Normal;
_objMail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
for (int i = 0; i < files.Count; i++)
{
System.Web.HttpPostedFileBase file = files[i];
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = file.InputStream.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
buffer = ms.ToArray();
}
System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(new MemoryStream(buffer),file.FileName);
_objMail.Attachments.Add(attach);
}
// Set the mail object's smtpserver property
using (SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587))
{
smtpClient.EnableSsl = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential(FromId, "password");
smtpClient.Send(_objMail);
}
return Json("Mail sent Successfully!");
}
catch (Exception ex)
{
return Json("Error While sending the mail!");
}
}