c# - Windows Form to Send email -
i'm trying build windows form 3 textboxt sender , object , body
to use smtpclient feature on .net , following, let users send me email using windows form.
i can't figure out , forgive me because i'm new c# , still learning.
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms; using system.net.mail; using system.net; namespace arcadiapatcher { public partial class form2 : form { public form2() { initializecomponent(); } private void button1_click(object sender, eventargs e) { smtpclient smtpclient = new smtpclient(); networkcredential basiccredential = new networkcredential("login", "pass"); mailmessage message = new mailmessage(); mailaddress fromaddress = new mailaddress(text1); smtpclient.host = "mail.domain2.com"; smtpclient.usedefaultcredentials = false; smtpclient.credentials = basiccredential; message.from = fromaddress; message.subject = text2; //set isbodyhtml true means can send html email. message.isbodyhtml = false; message.body = text3; message.to.add("recipient"); try { smtpclient.send(message); } { } } private void textbox1_textchanged(object sender, eventargs e) { } private void textbox2_textchanged(object sender, eventargs e) { } private void textbox3_textchanged(object sender, eventargs e) { } }
}
here working sample using gmail smtp :
public static mailaddress = new mailaddress("myaddress@gmail.com","ramy mohamed"); string password = "balabala"; var smtp = new smtpclient { host = "smtp.gmail.com", port = 587, enablessl = true, usedefaultcredentials = false, deliverymethod = smtpdeliverymethod.network, credentials = new networkcredential(from.address, password) }; using (var message = new mailmessage(from, sendtoaddress.text) { subject = mysubjecttext.text, body = mycontenttext.text }) { smtp.send(message); }
Comments
Post a Comment