EmailHelper.cs
3.53 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Net.Mime;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNet.Identity;
namespace CWA.CpoOnline.Helpers
{
public class EmailHelper
{
public static Task SendSmptEmail(IdentityMessage message)
{
try
{
MailMessage mailMsg = new MailMessage();
// To
mailMsg.To.Add(message.Destination);
// From
mailMsg.From = new MailAddress(WebConfig.SmtpEmailFrom);
// Subject and multipart/alternative Body
mailMsg.Subject = message.Subject;
string text = message.Body;
//mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(text, null, MediaTypeNames.Text.Plain));
mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(message.Body, null, MediaTypeNames.Text.Html));
// Init SmtpClient and send
SmtpClient smtpClient = new SmtpClient(WebConfig.SmtpSettings.Host, WebConfig.SmtpSettings.Port);
smtpClient.EnableSsl = WebConfig.SmtpSettings.EnableSsl;
smtpClient.Credentials = WebConfig.SmtpSettings.Credential;
// Send email
smtpClient.Send(mailMsg);
// Report result
return Task.FromResult(0);
}
catch (Exception ex)
{
return Task.FromException(ex);
}
}
public static Task SendPasswordResetEmail(string userName, string userEmail, string resetEmailUrl)
{
try
{
string subject = string.Format("Reset Password - {0}", WebConfig.AppName);
string body = @"
<div style=""font-family: calibri, arial, sans serif; font-size: 12pt;"">
<p>Dear {0},</p>
<p>Please reset your password by clicking <a href =""{1}"">here</a>.</p>
<p>{2}</p>
<p></p>
<div style=""font-size: 8pt;"">
Direct Link:<br />
{1}
<div>
</div>
";
body = string.Format(body, userName, resetEmailUrl, WebConfig.AppName);
var message = new MailMessage(WebConfig.SmtpEmailFrom, userEmail);
message.Subject = subject;
message.Body = body;
message.IsBodyHtml = true;
// Init SmtpClient and send
SmtpClient smtpClient = new SmtpClient(WebConfig.SmtpSettings.Host, WebConfig.SmtpSettings.Port);
smtpClient.EnableSsl = WebConfig.SmtpSettings.EnableSsl;
smtpClient.Credentials = WebConfig.SmtpSettings.Credential;
// Send email
smtpClient.Send(message);
// Report result
return Task.FromResult(0);
}
catch (Exception ex)
{
return Task.FromException(ex);
}
}
public static Task SendPasswordUpdateRequestEmail(string userName, string userEmail)
{
try
{
string subject = string.Format("Password Update Confirmation - {0}", WebConfig.AppName);
string body = @"
<div style=""font-family: calibri, arial, sans serif; font-size: 12pt;"">
<p>Dear {0},</p>
<p>Your password has been updated.</p>
<p>{1}</p>
</div>";
body = string.Format(body, userName, WebConfig.AppName);
var message = new MailMessage(WebConfig.SmtpEmailFrom, userEmail);
message.Subject = subject;
message.Body = body;
message.IsBodyHtml = true;
// Init SmtpClient and send
SmtpClient smtpClient = new SmtpClient(WebConfig.SmtpSettings.Host, WebConfig.SmtpSettings.Port);
smtpClient.EnableSsl = WebConfig.SmtpSettings.EnableSsl;
smtpClient.Credentials = WebConfig.SmtpSettings.Credential;
// Send email
smtpClient.Send(message);
// Report result
return Task.FromResult(0);
}
catch (Exception ex)
{
return Task.FromException(ex);
}
}
}
}