I send a mail using WebApi. Mail sent successfully. I want to change from mail in it and I change it using below code but it take from mail as '[email protected]'. I use [email protected] in webconfig and I want to set from as [email protected]. but not working as per below code and when I got mail it always from '[email protected]' instead of '[email protected]' Note : I use above emails only to ask question while development I use my real mail id.
Is there any other way to achieve this? Or I need to change anything.
Below is my code to send mail:
public static bool SendMail(string toAddress, string subject, string body)
{
try
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("[email protected]");
msg.To.Add(new MailAddress(toAddress));
msg.Subject = subject;
msg.Body = body;
msg.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.EnableSsl = true;
smtp.Send(msg);
return true;
}
catch (Exception ex)
{
return false;
}
}
Below is webconfig smpt setting:
<mailSettings>
<smtp from="[email protected]">
<network host="smtp.gmail.com" port="587" userName="[email protected]" password="test" />
</smtp>
</mailSettings>
also try with below setting in webconfig:
<smtp deliveryMethod="Network">
<network host="smtp.gmail.com" port="587" userName="[email protected]" password="test" />
</smtp>
</mailSettings>