How to send an email using the new System.Net.Mail
I searched this forum for this issue, and only found 2 posts, one which i responded to a while ago before i started using 2.0, and i didnt answer my own question!
My question was using the default credential supplied in the web config from my codebehind. I had to reinitialize the credentials in code until i found the client.UseDefaultCredentials = true setting.
This new version is easier to use, so you may all have it figured out already, but for those that are still slightly confused as i was, hopefully this will help.
web.config section
code behind
I searched this forum for this issue, and only found 2 posts, one which i responded to a while ago before i started using 2.0, and i didnt answer my own question!
My question was using the default credential supplied in the web config from my codebehind. I had to reinitialize the credentials in code until i found the client.UseDefaultCredentials = true setting.
This new version is easier to use, so you may all have it figured out already, but for those that are still slightly confused as i was, hopefully this will help.
web.config section
Code:
<system.net>
<mailSettings>
<smtp from="sales@domainname.com" deliveryMethod="Network">
<network host="mail.domainname.com" port="25" userName="sales@domainname.com" password="nottellingyou" defaultCredentials="true" />
</smtp>
</mailSettings>
</system.net>
code behind
Code:
//dont forget these
...
using System.Net
using System.Net.Mail
...
public void sendEmail(object sender, EventArgs e)
{
string From = "me@domainname.com";
string fromName = "Silly Website"; //set email display name
string Subject = "Web Site Question Submitted";
string body = "<font style='font-family:arial;font-size:9pt;'";
body += " From: " + txtReqName.Text + "<br>"
+ "Email: " + txtReqEmail.Text + "<br>"
+ "Question: " + txtQuestion.Text;
body += "</font>";
sendEmails(From, fromName, From, Subject, body);
}
public void sendEmails(string from, string fromName, string to, string subj, string body)
{
MailMessage mailMsg = new MailMessage();
mailMsg.From = new MailAddress(from, fromName);
mailMsg.To.Add(new MailAddress(to));
mailMsg.Subject = subj;
mailMsg.Body = body;
mailMsg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
try
{
client.UseDefaultCredentials = true;
client.Send(mailMsg);
}
catch (Exception ex)
{
lblMessage.Text += ex.Message;
}
}