MailMessage mail = new MailMessage();
mail.To.Add("makovetskiyd@yahoo.co.uk");
mail.From = new MailAddress("makovetskiyd@yahoo.co.uk");
mail.Subject = "Test Email";
string Body = "Welcome to CodeDigest.Com!!";
mail.Body = Body;
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["SMTP"];//i get nuller point exception here.. what does the class configuration manager do?
smtp.Send(mail);
Asked
Active
Viewed 88 times
1

Dmitry Makovetskiyd
- 6,942
- 32
- 100
- 160
-
Possible duplicate, Refer http://stackoverflow.com/questions/2118413/how-to-send-the-mail-from-c http://stackoverflow.com/questions/2354436/how-to-send-mail-using-c http://stackoverflow.com/questions/1972491/send-mail-script-in-asp-net-with-c – Sandeep G B May 10 '11 at 05:23
-
*each time he receives a message in the forum* ? – V4Vendetta May 10 '11 at 05:26
-
yeah. i have the code when a message is being accepted. i want it to work like youtube! – Dmitry Makovetskiyd May 10 '11 at 05:29
-
Please don't completely change your question. If you have a new question, please post a new question. – Jonathan Wood May 10 '11 at 14:22
2 Answers
1
You can use the built-in classes from Microsoft (System.Net.Mail). For example, here is a quick and easy method for sending email:
public static void SendEmail(string messageText, string subjectText,
string fromAddress, string toAddress, string ccAddress,
string bccAddress, string hostName, string attachments,
string userName, string password)
{
try
{
string[] toAddressList = toAddress.Split(';');
string[] ccAddressList = ccAddress.Split(';');
string[] bccAddressList = bccAddress.Split(';');
string[] attachmentList = attachments.Split(';');
MailMessage mail = new MailMessage();
//Loads the To address field
foreach (string address in toAddressList)
{
if (address.Length > 0)
{
mail.To.Add(address);
}
}
//Loads the CC address field
foreach (string address in ccAddressList)
{
if (address.Length > 0)
{
mail.CC.Add(address);
}
}
//Loads the BCC address field
foreach (string address in bccAddressList)
{
if (address.Length > 0)
{
mail.Bcc.Add(address);
}
}
//Loads the attachment list
foreach (string attachment in attachmentList)
{
if (attachment.Length > 0)
{
mail.Attachments.Add(new Attachment(attachment));
}
}
mail.From = new MailAddress(fromAddress);
mail.Subject = subjectText;
string Body = messageText;
mail.Body = Body;
SmtpClient smtp = new SmtpClient();
smtp.Host = hostName;
smtp.Credentials = new System.Net.NetworkCredential(userName,password);
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Send(mail);
mail.Dispose();
smtp.Dispose();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
In this example, you can send email through Gmail.

IAmTimCorey
- 16,412
- 5
- 39
- 75
-
@Dmitry Makovetskiyd - Right now, this will work just with Gmail. However, you could add more options so that you could change the port number, etc. from a config file. However, the rest of it is fairly flexible. – IAmTimCorey May 10 '11 at 05:35
0
You can use the System.Net.Mail classes.
Here's a sample of how to do it using gmail: Sending email in .NET through Gmail