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 'testweb@gmail.com'. I use testweb@gmail.com in webconfig and I want to set from as testfrom@gmail.com. but not working as per below code and when I got mail it always from 'testweb@gmail.com' instead of 'testfrom@gmail.com' 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("testfrom@gmail.com");
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="testweb@gmail.com">
<network host="smtp.gmail.com" port="587" userName="testweb@gmail.com" password="test" />
</smtp>
</mailSettings>
also try with below setting in webconfig:
<smtp deliveryMethod="Network">
<network host="smtp.gmail.com" port="587" userName="testweb@gmail.com" password="test" />
</smtp>
</mailSettings>