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="[email protected]">
<network host="smtp.gmail.com" enableSsl="true" port="587" userName="[email protected]" 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
Are you trying to send to a message from multiple from addresses or do you want to be able to use this same method with different from addresses?
If you want to be able to reuse this method with different from addresses at different points of time, you could pass in the credentials in the method as a parameter. Then you will be able to set up smtp based off the credentials supplied from the parameter.
I don't see any place you are reading the mailSettings you set up. So I don't see how youre configuring your smtp.