Search code examples
c#winformsemailvisual-studio-2022hotmail

Getting the "From" address from the textbox(Sending e-mails via C# application)


So I wrote everything that's needed to send mail via Windows Forms. But I also want to get the "from" address from the text that is written to textbox by the user.Is there a way to do this? So far I couldn't find it.

Any help is appreciated,thanks in advance..

This is my code so far.

MailMessage msg= new MailMessage();
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("[email protected]", "1234"); 

client.Port = 587; 

client.Host = "smtp.live.com"; 

client.EnableSsl = true;
msg.To.Add(textBox1.Text);

msg.From.Add(textBox4.Text); // The part where I got stuck
msg.Subject = textBox2.Text;
msg.Body = textBox3.Text;
client.Send(msg);


Solution

  • You get that error because MailAddress does not have an Add method.

    Look into the example: To is a MailAddressCollection. From is a MailAddress?. So you need to set it (either in ctor arg or) msg.From = new MailAddress(textBox4.Text);