c# - Send email through SMTP -
i have contact page on website , want users enter name , email id (no password) , send email on button click.
i did google , solutions getting requires user password,
public static void sendmessage(string subject, string messagebody, string toaddress, string ccaddress, string fromaddress) { mailmessage message = new mailmessage(); smtpclient client = new smtpclient(); // set sender's address message.from = new mailaddress(fromaddress); // allow multiple "to" addresses separated semi-colon if (toaddress.trim().length > 0) { foreach (string addr in toaddress.split(';')) { message.to.add(new mailaddress(addr)); } } // allow multiple "cc" addresses separated semi-colon if (ccaddress.trim().length > 0) { foreach (string addr in ccaddress.split(';')) { message.cc.add(new mailaddress(addr)); } } // set subject , message body text message.subject = subject; message.body = messagebody; // smtp settings var smtp = new system.net.mail.smtpclient(); { smtp.host = "smtphost"; smtp.port =portno; smtp.enablessl = true; smtp.deliverymethod = system.net.mail.smtpdeliverymethod.network; smtp.credentials = new networkcredential("fromemail", "frompassword"); smtp.timeout = 20000; } // send e-mail message smtp.send(message); }
but don't want force users enter password. there other solution using smtp?
the error getting a connection attempt failed because connected party did not respond after period of time, or established connection failed because connected host has failed respond
i have function in utility class , , works charm.
public static bool sendmail(string email, string mailsubject, string mailbody) { bool issent = false, ismailviassl = convert.toboolean(configurationmanager.appsettings["mailserverusessl"]); string mailhost = configurationmanager.appsettings["mailserveraddress"].tostring(), senderaddress = configurationmanager.appsettings["mailserversenderusername"].tostring(), senderpassword = configurationmanager.appsettings["mailserversenderpassword"].tostring(); int serverport = convert.toint32(configurationmanager.appsettings["mailserverport"]); mailmessage msgemail = new mailmessage(new mailaddress(senderaddress), new mailaddress(email)); using (msgemail) { msgemail.isbodyhtml = true; msgemail.bodyencoding = system.text.encoding.utf8; msgemail.subject = mailsubject; msgemail.body = mailbody; using (smtpclient smtp = new smtpclient(mailhost)) { smtp.usedefaultcredentials = false; smtp.enablessl = ismailviassl; smtp.credentials = new networkcredential(senderaddress, senderpassword); smtp.port = serverport; try { smtp.send(msgemail); issent = true; } catch (exception ex) { throw ex; } } } return issent; } <!-- mail server settings --> <add key="mailserveraddress" value="smtp.gmail.com" /> <add key="mailserverport" value="25" /> <add key="mailserversenderusername" value="username@gmail.com" /> <add key="mailserversenderpassword" value="password" /> <add key="mailserverusessl" value="true" />
Comments
Post a Comment