· 9 years ago · Feb 04, 2017, 03:24 PM
1try
2 {
3 using (var mail = new MailMessage())
4 {
5 const string email = "*******@gmail.com";
6 const string password = "*********";
7 var loginInfo = new NetworkCredential(email, password);
8 mail.From = new MailAddress("*******@gmail.com");
9 mail.To.Add(new MailAddress("email@email.com"));
10 mail.Subject = subject;
11 mail.Body = body;
12 mail.IsBodyHtml = true;
13 try
14 {
15 using (var smtpClient = new SmtpClient("smtp.gmail.com", 587))
16 {
17 smtpClient.EnableSsl = true;
18 smtpClient.UseDefaultCredentials = false;
19 smtpClient.Credentials = loginInfo;
20 smtpClient.Send(mail);
21 }
22 }
23 finally
24 {
25 mail.Dispose();
26 }
27
28 }
29 }
30 catch (SmtpFailedRecipientsException ex)
31 {
32 foreach (SmtpFailedRecipientException t in ex.InnerExceptions)
33 {
34 var status = t.StatusCode;
35 if (status == SmtpStatusCode.MailboxBusy ||
36 status == SmtpStatusCode.MailboxUnavailable)
37 {
38 return View(status);
39 }
40 else
41 {
42 return View(status);
43 }
44 }
45
46 }
47 catch (SmtpException Se)
48 {
49 // handle exception here
50 return View(Se);
51 }
52
53 catch (Exception ex)
54 {
55 return View(ex);
56 }