I send mails following this pattern:
public static bool SendPasswordMail(MembershipUser user, Control owner, string password)
{
var definition = new MailDefinition { BodyFileName = string.Concat(AccountRoot, "password.htm"), IsBodyHtml = true };
var subject = "Your new password - {0}".FormatWith(ApplicationConfiguration.ApplicationName);
var data = ExtendedData(DefaultData, subject, user);
data.Add("<%Password%>", password);
return definition.CreateMailMessage(user.Email, data, owner).Send(subject);
}
public static bool Send(this MailMessage message, string subject)
{
try
{
using (message)
{
message.Subject = subject;
using (var client = new SmtpClient())
client.Send(message);
}
}
catch
{
return false;
}
return true;
}
Rather than setting the subject to “Your new password – Website”, I’d like to set the from user name to “Website”, and the subject to just “Your new password”. But I can’t figure out how to set the from user name.
My smtp element in web.config looks like this:
<smtp deliveryMethod="Network" from="noreply@site.com">
<network host="localhost" port="25" userName="noreply@site.com" password="******" />
</smtp>
Take a look a the MailDefinition.CreateMailMessage Method definition on MSDN.
It specifies that the second parameter (an IDictionary object, variable “data” in your sample code) should contain strings that are replaced in the email message.
Using this object you could add a replacement for the from address like so:
However due to the ability to forge sender addresses using SMTP almost all mail exchangers will be using an email validation system such as Sender Policy Framework (SPF) to ensure that the emails received have been sent from a valid source.
So you cannot use any email address in the from field if you wish your recipients to receive your emails. You must use an email address matching the domain you are sending from. DNS changes are also required to make the necessary checks possible.
Checkout this link: An Overview of the Sender Policy Framework
It states:
Edit
There is no way to pass the sender alias (display name) to the MailDefinition on creation and Im not sure how to add the data to the IDictionary object, but the following should work (in the Send extension method):
Hope this helps.