Jp Heydenrichd Jp Heydenrichd - 22 days ago 8
ASP.NET (C#) Question

Pushing an Email with a noreply to an employee in C#

I was hoping someone would be able to help me, I'm trying to send an email from a noreply idea to a people in a company email domain, but it keeps using my personal email instead. Here is my code, maybe I'm missing something.

namespace Mail
{
public class MailerController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(MailModel model, List<HttpPostedFileBase> attachments)
{
using (MailMessage mm = new MailMessage(model.To, model.To))
{
mm.Subject = model.Subject;
mm.Body = model.Body;
foreach (HttpPostedFileBase attachment in attachments)
{
if (attachment != null)
{
string fileName = Path.GetFileName(attachment.FileName);
mm.Attachments.Add(new Attachment(attachment.InputStream, fileName));
}
}
mm.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
Console.WriteLine("smtp : " + smtp);
smtp.Host = "10.12.6.12";
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
Console.WriteLine("SMTP : " + smtp);
smtp.UseDefaultCredentials = true;
smtp.Port = 465;
ServicePointManager.ServerCertificateValidationCallback += (o, c, ch, er) => true;
ServicePointManager.ServerCertificateValidationCallback =
delegate(object s, X509Certificate certificate,
X509Chain chain, SslPolicyErrors sslPolicyErrors)
{ return true; };
smtp.Send(mm);
Console.WriteLine("mm : " + mm);
ViewBag.Message = "Email has been sent.";
var emailService = new IdentityConfig.EmailService();

IdentityMessage msg = new IdentityMessage()
{
Destination = "jp.pretorius@smec.com",
Subject = "Project Document Review",
Body = "Dear Mr/Mrs/Miss, You have recieved a Document that needs to be viewed, Please Find attached document and document number for viewing, once the document has been viewed please assign it to Accepted or rejected status, thereafter please make sure you have marked the number to the project "
};
emailService.Send(msg);
}
return View("Index");
}*

Answer Source

You create an instance of the MailMessage class with model.To for both sender and receiver. The first one should be the email of the sender:

using (MailMessage mm = new MailMessage(model.From, model.To)) { }

Also, check out the link in the comment by Hans Kesting.