asked on
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.Configuration;
using SendGrid;
namespace smashandgrab
{
public class EmailSender
{
string EmailSenderId = WebConfigurationManager.AppSettings["EmailSenderId"];
string EmailSenderPassword = WebConfigurationManager.AppSettings["EmailSenderPassword"];
string EmailSenderEnableSsl = WebConfigurationManager.AppSettings["EmailSenderEnableSsl"];
string EmailSenderHost = WebConfigurationManager.AppSettings["EmailSenderHost"];
string EmailSenderPort = WebConfigurationManager.AppSettings["EmailSenderPort"];
string SenderTitle = WebConfigurationManager.AppSettings["SenderTitle"];
string emailBody = "";
public void SendEmail(DataResult obj)
{
string path = HttpContext.Current.Server.MapPath("/EmailFormat/EstimationEmail.html");
if (File.Exists(path))
{
emailBody = File.ReadAllText(path);
emailBody = emailBody.Replace("#FloorDepth#", obj.FloorDepth);
emailBody = emailBody.Replace("#val220#", obj.val220);
emailBody = emailBody.Replace("#val240#", obj.val240);
emailBody = emailBody.Replace("#val300#", obj.val300);
emailBody = emailBody.Replace("#Include1#", obj.Include1);
emailBody = emailBody.Replace("#Include2#", obj.Include2);
emailBody = emailBody.Replace("#Include3#", obj.Include3);
emailBody = emailBody.Replace("#Include4#", obj.Include4);
emailBody = emailBody.Replace("#Include5#", obj.Include5);
emailBody = emailBody.Replace("#Include6#", obj.Include6);
emailBody = emailBody.Replace("#Include7#", obj.Include7);
emailBody = emailBody.Replace("#Include8#", obj.Include8);
emailBody = emailBody.Replace("#Include9#", obj.Include9);
emailBody = emailBody.Replace("#Include10#", obj.Include10);
}
using (MailMessage mail = new MailMessage())
{
mail.DeliveryNotificationOptions = System.Net.Mail.DeliveryNotificationOptions.OnSuccess;
mail.From = new MailAddress(EmailSenderId);
mail.To.Add(obj.Email);
mail.Subject = "Quote";
mail.Body = emailBody;
mail.IsBodyHtml = true;
MailAddress mailaddress = new MailAddress(EmailSenderId, SenderTitle);
mail.From = mailaddress;
using (SmtpClient smtp = new SmtpClient(EmailSenderHost, Convert.ToInt32(EmailSenderPort)))
{
smtp.Credentials = new NetworkCredential(EmailSenderId, EmailSenderPassword);
smtp.EnableSsl = Convert.ToBoolean(EmailSenderEnableSsl);
smtp.Send(mail);
}
}
}
}
}
ASKER
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.Configuration;
using SendGrid;
namespace smashandgrab
{
public class EmailSender
{
string EmailSenderId = WebConfigurationManager.AppSettings["EmailSenderId"];
string EmailSenderPassword = WebConfigurationManager.AppSettings["EmailSenderPassword"];
string EmailSenderEnableSsl = WebConfigurationManager.AppSettings["EmailSenderEnableSsl"];
string EmailSenderHost = WebConfigurationManager.AppSettings["EmailSenderHost"];
string EmailSenderPort = WebConfigurationManager.AppSettings["EmailSenderPort"];
string SenderTitle = WebConfigurationManager.AppSettings["SenderTitle"];
string emailBody = "";
public void SendEmail(DataResult obj)
{
string path = HttpContext.Current.Server.MapPath("/EmailFormat/EstimationEmail.html");
if (File.Exists(path))
{
emailBody = File.ReadAllText(path);
emailBody = emailBody.Replace("#FloorDepth#", obj.FloorDepth);
emailBody = emailBody.Replace("#val220#", obj.val220);
emailBody = emailBody.Replace("#val240#", obj.val240);
emailBody = emailBody.Replace("#val300#", obj.val300);
emailBody = emailBody.Replace("#Include1#", obj.Include1);
emailBody = emailBody.Replace("#Include2#", obj.Include2);
emailBody = emailBody.Replace("#Include3#", obj.Include3);
emailBody = emailBody.Replace("#Include4#", obj.Include4);
emailBody = emailBody.Replace("#Include5#", obj.Include5);
emailBody = emailBody.Replace("#Include6#", obj.Include6);
emailBody = emailBody.Replace("#Include7#", obj.Include7);
emailBody = emailBody.Replace("#Include8#", obj.Include8);
emailBody = emailBody.Replace("#Include9#", obj.Include9);
emailBody = emailBody.Replace("#Include10#", obj.Include10);
}
//api credentials
var username = System.Environment.GetEnvironmentVariable("SENDGRID_USER");
var pswd = System.Environment.GetEnvironmentVariable("SENDGRID_PASS");
var apiKey = System.Environment.GetEnvironmentVariable("SENDGRID_APIKEY");
// Create the email object first, then add the properties.
SendGrid.SendGridAPIClient = new SendGridAPIClient("", "");
// .SendGridMessage myMessage = new SendGridMessage();
myMessage.AddTo(obj.Email);
myMessage.From = new MailAddress(EmailSenderId, SenderTitle);
myMessage.Subject = "Quote";
myMessage.Text = emailBody;
// Create credentials, specifying your user name and password.
var credentials = new NetworkCredential(EmailSenderId, EmailSenderPassword);
// Create an Web transport for sending email.
var transportWeb = new Web(credentials);
// Send the email, which returns an awaitable task.
transportWeb.DeliverAsync(myMessage);
// If developing a Console Application, use the following
// transportWeb.DeliverAsync(mail).Wait();
//using (MailMessage mail = new MailMessage())
//{
// mail.DeliveryNotificationOptions = System.Net.Mail.DeliveryNotificationOptions.OnSuccess;
// mail.From = new MailAddress(EmailSenderId);
// mail.To.Add(obj.Email);
// mail.Subject = "Quote";
// mail.Body = emailBody;
// mail.IsBodyHtml = true;
// MailAddress mailaddress = new MailAddress(EmailSenderId, SenderTitle);
// mail.From = mailaddress;
// using (SmtpClient smtp = new SmtpClient(EmailSenderHost, Convert.ToInt32(EmailSenderPort)))
// {
// smtp.Credentials = new NetworkCredential(EmailSenderId, EmailSenderPassword);
// smtp.EnableSsl = Convert.ToBoolean(EmailSenderEnableSsl);
// smtp.Send(mail);
// }
//}
}
}
}
public static bool SendEmail(string toList, string subject, string body, string fromAddress, string fromName, string apiKey)
{
try
{
SendGridMessage myMessage = new SendGridMessage();
myMessage.AddTo(toList);
myMessage.From = new MailAddress(fromAddress, fromName);
myMessage.Subject = subject;
myMessage.Html = body;
var transportWeb = new Web(apiKey);
var task = transportWeb.DeliverAsync(myMessage);
task.Wait();
return task.IsCompleted;
}
catch (Exception ex)
{
throw ex;
}
}
ASKER
ASKER
ASKER
The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.
TRUSTED BY
do u have the correct smptp address/port
This is wht send grid uses:
host:smtp.sendgrid.net
port:587
it would be better if you use their api's for sending emails, then you can track the emails as well whether it was delivered correctly and so on