Update to mailkit for better email standard
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
using System.Net.Mail;
|
||||
using System.Threading.Tasks;
|
||||
using MailKit.Net.Smtp;
|
||||
using MailKit.Security;
|
||||
using MimeKit;
|
||||
|
||||
namespace BoredCareers.Services {
|
||||
public partial class EmailService {
|
||||
@@ -17,24 +20,28 @@ namespace BoredCareers.Services {
|
||||
EmailPassword = _EmailPassword;
|
||||
}
|
||||
|
||||
public string Send( string Destination, string Subject, string Body ) {
|
||||
using (SmtpClient client = new SmtpClient( EmailServer, EmailPort )){
|
||||
client.EnableSsl = true;
|
||||
client.Credentials = new System.Net.NetworkCredential( EmailAddress, EmailPassword );
|
||||
try {
|
||||
MailMessage msg = new MailMessage(){
|
||||
IsBodyHtml = true,
|
||||
Subject = Subject,
|
||||
Body = Body
|
||||
};
|
||||
msg.From = new MailAddress( EmailAddress, "no-reply" );
|
||||
msg.To.Add( new MailAddress( Destination ) );
|
||||
client.Send( msg );
|
||||
return "Success";
|
||||
} catch( Exception e ) {
|
||||
public async Task<string> Send(string Destination, string Subject, string Body) {
|
||||
try {
|
||||
MimeMessage message = new MimeMessage();
|
||||
message.From.Add(new MailboxAddress(EmailAddress.Split('@')[0], EmailAddress));
|
||||
message.To.Add(new MailboxAddress(Destination.Split('@')[0], Destination));
|
||||
message.Subject = Subject;
|
||||
|
||||
BodyBuilder messageBody = new BodyBuilder();
|
||||
messageBody.HtmlBody = Body;
|
||||
|
||||
message.Body = messageBody.ToMessageBody();
|
||||
|
||||
using (SmtpClient client = new SmtpClient()) {
|
||||
await client.ConnectAsync(EmailServer, EmailPort);
|
||||
client.Authenticate(new SaslMechanismLogin(EmailAddress, EmailPassword));
|
||||
string serverResponse = await client.SendAsync(message);
|
||||
await client.DisconnectAsync(true);
|
||||
return serverResponse;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return "An Error Has Occurred Sending Email : " + e.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user