0

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>
Hitesh
  • 1,188
  • 5
  • 30
  • 52
  • Possible duplicate of [change sender address when sending mail through gmail in c#](http://stackoverflow.com/questions/3871577/change-sender-address-when-sending-mail-through-gmail-in-c-sharp) – Hans Kesting Apr 13 '17 at 13:27

1 Answers1

0

You cant!!

You are using the google SMTP server, it doesn't allows to change the from address, if you want to change the from address try using some other smtp provider that allows so.