Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set "From" address when using System.Net.Mail.MailMessage?

I'm trying to send a password reset email, but I'm having trouble figuring out how to specify the sender's address.

Here's what I'm trying to do:

MailMessage mail = new MailMessage();
mail.From.Address = "[email protected]";
mail.To.Add(Email);
mail.Subject = "Forgot Password";
mail.Body = "<a href=\"" + url + "\">Click here to reset your password.</a>";
SmtpClient smtp = new SmtpClient();
smtp.SendAsync(mail, null);

I'm sure it's possible, so how can I accomplish this in ASP.Net?

like image 554
keeehlan Avatar asked Jun 21 '13 21:06

keeehlan


People also ask

What is System Net Mail?

SmtpClient Class (System. Net.Mail) Allows applications to send email by using the Simple Mail Transfer Protocol (SMTP). The SmtpClient type is obsolete on some platforms and not recommended on others; for more information, see the Remarks section.

How to send mail from console application in C#?

C# send simple mail with Mailkit using MailKit. Net. Smtp; using MailKit. Security; using MimeKit; var host = "smtp.mailtrap.io"; var port = 2525; var username = "username"; // get from Mailtrap var password = "password"; // get from Mailtrap var message = new MimeMessage(); message.


1 Answers

It turns out I was getting ahead of myself.

Removing Address from mail.From.Address allowed me to set the value, but needed the type MailAddress.

Here's the solution:

MailMessage mail = new MailMessage();
mail.From = new MailAddress("[email protected]");
mail.To.Add(Email);
mail.Subject = "Forgot Password";
mail.Body = "<a href=\"" + url + "\">Click here to reset your password.</a>";
SmtpClient smtp = new SmtpClient();
smtp.SendAsync(mail, null);
like image 162
keeehlan Avatar answered Oct 21 '22 06:10

keeehlan