using System using System Collections Generic using System Linq using

 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Net.Mail;
/// <summary>
/// Summary description for Controller
/// </summary>
namespace ControllerMail
{
public class Controller
{
public static void SendMail(string smtpServer, string from, string password, string mailto, string caption, string message, string attachFile = null)
{
try
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress(from);
mail.To.Add(new MailAddress(mailto));
mail.Subject = caption;
mail.Body = message;
if (!string.IsNullOrEmpty(attachFile))
mail.Attachments.Add(new Attachment(attachFile));
SmtpClient client = new SmtpClient();
client.Host = smtpServer;
client.Port = 465;
client.EnableSsl = true;
client.Credentials = new NetworkCredential(from.Split('@')[0], password);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(mail);
mail.Dispose();
}
catch (Exception e)
{
throw new Exception("Mail.Send: " + e.Message);
}
}
}
}