csharp Send an email

using System.Net.Mail;

//create the mail message
MailMessage mail = new MailMessage();

//set the addresses
mail.From = new MailAddress(Email);
mail.To.Add(Properties.Settings.Default.Mail_ToEmail);

//set the content
mail.Subject = Properties.Settings.Default.Mail_Subject;
mail.Body = Message;

if (Properties.Settings.Default.Mail_Type.ToLower() == "html")
{
	mail.IsBodyHtml = true;
}

//set smtp properties
SmtpClient smtp = new SmtpClient(Properties.Settings.Default.Mail_SMTP);
smtp.Port = Properties.Settings.Default.Mail_Port;
smtp.Credentials = new NetworkCredential(Properties.Settings.Default.Mail_User, Properties.Settings.Default.Mail_Password);

//send the message and cleanup
smtp.Send(mail);
mail.Dispose();

//web.config settings
/*
<applicationSettings>
	<example.Properties.Settings>
		<setting name="Mail_ToEmail" serializeAs="String">
			<value>hi@example.com</value>
		</setting>
		<setting name="Mail_SMTP" serializeAs="String">
			<value>mail.example.com</value>
		</setting>
		<setting name="Mail_User" serializeAs="String">
			<value>hello@example.com</value>
		</setting>
		<setting name="Mail_Password" serializeAs="String">
			<value>1234657890</value>
		</setting>
		<setting name="Mail_Port" serializeAs="String">
			<value>25</value>
		</setting>
		<setting name="Mail_Type" serializeAs="String">
			<value>HTML</value>
		</setting>
		<setting name="Mail_Subject" serializeAs="String">
			<value>Contact from Example.com</value>
		</setting>
		<setting name="LogFile" serializeAs="String">
			<value>C:\Websites\example\Contact.log</value>
		</setting>
	</example.Properties.Settings>
</applicationSettings>
*/
Send an email. See Scott Guthrie's blog for more information.

Updated: Thursday 26th May 2011, 11:09am

There are 0 comments

Leave a comment of your own

Comments are currently closed.