0
MailMessage mail = new MailMessage {
    From = new MailAddress("sender@live.com"),
    IsBodyHtml = true,
    Subject = topicLists.SelectedItem.Value,
    Body = txtMsg.Text
};

// Configuring the SMTP Client
SmtpClient smtp = new SmtpClient() {
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential("to@gmail.com", "to-pass"),
    Host = "smtp.gmail.com"
};

// Recipient Address
mail.To.Add("to@gmail.com");

smtp.Send(mail);

I expect the result should be something like


From: sender@live.com

To: to@gmail.com


But what I got is that the sender is emailing themselves

From: sender@live.com

To: sender@gmail.com

I don't get why it's not using the other email

Afiq Rosli
  • 357
  • 1
  • 7
  • 22
  • The scenario makes no sense? You want to fake an email from "sender@live.com" in "to@gmail.com"'s inbox? – Mikael Dúi Bolinder Mar 15 '19 at 16:35
  • May be this helps https://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail?rq=1 – Sarker Mar 15 '19 at 16:36
  • @MikaelDúiBolinder The scenario is something [like this](https://imgur.com/a/CWL09XF). You have an organization contact page, where the user input their email, subject and message (inquiry). So it's from the user's email (sender) to the organization's email (to-receiver) – Afiq Rosli Mar 15 '19 at 16:46
  • Thanks for that @Sarker, but unfortunately this isn't what I need. The credential part is from the sender, which is the user in my case. Asking the user to ask for their email password is definitely wrong – Afiq Rosli Mar 15 '19 at 16:49
  • @AfiqRosli how about https://developers.google.com/gmail/api/v1/reference/users/messages/insert ? I think Gmail is very strict about the "From" when using SMTP. – Mikael Dúi Bolinder Mar 15 '19 at 17:29

0 Answers0