i already have a mail service, but i need to use different from address, because this service is used by many services. Now, i have this code, which works fine:
public static bool SendMail(Mail mail)
{
var smtp = new SmtpClient();
var credential = (NetworkCredential) smtp.Credentials;
var mailMessage = new MailMessage
{
From = new MailAddress(credential.UserName, mail.DisplayName),
Subject = mail.Subject,
Body = mail.Body,
IsBodyHtml = true
};
mailMessage.To.Add(new MailAddress(mail.To));
if (!string.IsNullOrEmpty(mail.TemplatePath))
mailMessage = embedImages(mailMessage, mail);
smtp.Send(mailMessage);
return true;
}
> And the web.config:
<mailSettings>
<smtp from="mail@gmail.com">
<network host="smtp.gmail.com" enableSsl="true" port="587" userName="mail@gmail.com" password="123456" />
</smtp>
</mailSettings>
> The Mail parameter, is an object:
public class Mail
{
public string Subject { get; set; }
public string Body { get; set; }
public string To { get; set; }
public string TemplatePath { get; set; }
public string DisplayName { get; set; }
public string From { get; set; }
}
So, by default, it should use the mailSettings, but, if the property mail.From != null, it should be sent by that mail.
Thanks