using System;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Threading;
using System.ComponentModel;
namespace Examples.SmptExamples.Async
{
public class SimpleAsynchronousExample
{
static bool mailSent = false;
private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
// Get the unique identifier for this asynchronous operation.
String token = (string) e.UserState;
if (e.Cancelled)
{
Console.WriteLine("[{0}] Send canceled.", token);
}
if (e.Error != null)
{
Console.WriteLine("[{0}] {1}", token, e.Error.ToString());
} else
{
Console.WriteLine("Message sent.");
}
mailSent = true;
}
public static void Main(string[] args)
{
// Command line argument must the the SMTP host.
SmtpClient client = new SmtpClient(args[0]);
// Specify the e-mail sender.
// Create a mailing address that includes a UTF8 character
// in the display name.
MailAddress from = new MailAddress("jane@contoso.com",
"Jane " + (char)0xD8+ " Clayton",
System.Text.Encoding.UTF8);
// Set destinations for the e-mail message.
MailAddress to = new MailAddress("ben@contoso.com");
// Specify the message content.
MailMessage message = new MailMessage(from, to);
message.Body = "This is a test e-mail message sent by an application. ";
// Include some non-ASCII characters in body and subject.
string someArrows = new string(new char[] {'\u2190', '\u2191', '\u2192', '\u2193'});
message.Body += Environment.NewLine + someArrows;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "test message 1" + someArrows;
message.SubjectEncoding = System.Text.Encoding.UTF8;
// Set the method that is called back when the send operation ends.
client.SendCompleted += new
SendCompletedEventHandler(SendCompletedCallback);
// The userState can be any object that allows your callback
// method to identify this send operation.
// For this example, the userToken is a string constant.
string userState = "test message1";
client.SendAsync(message, userState);
Console.WriteLine("Sending message... press c to cancel mail. Press any other key to exit.");
string answer = Console.ReadLine();
// If the user canceled the send, and mail hasn't been sent yet,
// then cancel the pending operation.
if (answer.StartsWith("c") && mailSent == false)
{
client.SendAsyncCancel();
}
// Clean up.
message.Dispose();
Console.WriteLine("Goodbye.");
}
}
}
using System.Net.Mail;
MailMessage message = new MailMessage();
try
{
message.To.Add(new MailAddress("toaddress"));
message.From = new MailAddress("from address");
message.Subject = "Test mail";
message.Body = "this is the mail sended through my c#.net program";
message.BodyEncoding = System.Text.Encoding.UTF8;
message.SubjectEncoding = System.Text.Encoding.UTF8;
SmtpClient client = new SmtpClient();
client.Port = 587; // Gmail works on this port
client.Host = "smtp.gmail.com";
System.Net.NetworkCredential nc = new System.Net.NetworkCredential("yourmail@gmail.com", "your gmail password");// is u are using Gmail
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = nc;
client.Send(message);
}
catch (Exception ex)
{
;
}
MailMessage msg = new MailMessage();
string mail="give mail adress of destination to which u want to send mail";
msg.From = new MailAddress("write ur from adress");
msg.To.Add(mail);
msg.Subject = "write anything what u required";
msg.IsBodyHtml = true;
msg.Priority = MailPriority.High;
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("ur mail id", "password");
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
object userstate = msg;
client.Send(msg);
anugeorge09
Member
2 Points
126 Posts
how to send email from asp.net,C# 4.0
Jul 25, 2012 09:08 AM|LINK
how to send email from asp.net,C# 4.0 please provide full code needed to configure the smtp server and send email
Vivek J Pand...
Participant
922 Points
257 Posts
Re: how to send email from asp.net,C# 4.0
Jul 25, 2012 09:11 AM|LINK
using System; using System.Net; using System.Net.Mail; using System.Net.Mime; using System.Threading; using System.ComponentModel; namespace Examples.SmptExamples.Async { public class SimpleAsynchronousExample { static bool mailSent = false; private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e) { // Get the unique identifier for this asynchronous operation. String token = (string) e.UserState; if (e.Cancelled) { Console.WriteLine("[{0}] Send canceled.", token); } if (e.Error != null) { Console.WriteLine("[{0}] {1}", token, e.Error.ToString()); } else { Console.WriteLine("Message sent."); } mailSent = true; } public static void Main(string[] args) { // Command line argument must the the SMTP host. SmtpClient client = new SmtpClient(args[0]); // Specify the e-mail sender. // Create a mailing address that includes a UTF8 character // in the display name. MailAddress from = new MailAddress("jane@contoso.com", "Jane " + (char)0xD8+ " Clayton", System.Text.Encoding.UTF8); // Set destinations for the e-mail message. MailAddress to = new MailAddress("ben@contoso.com"); // Specify the message content. MailMessage message = new MailMessage(from, to); message.Body = "This is a test e-mail message sent by an application. "; // Include some non-ASCII characters in body and subject. string someArrows = new string(new char[] {'\u2190', '\u2191', '\u2192', '\u2193'}); message.Body += Environment.NewLine + someArrows; message.BodyEncoding = System.Text.Encoding.UTF8; message.Subject = "test message 1" + someArrows; message.SubjectEncoding = System.Text.Encoding.UTF8; // Set the method that is called back when the send operation ends. client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback); // The userState can be any object that allows your callback // method to identify this send operation. // For this example, the userToken is a string constant. string userState = "test message1"; client.SendAsync(message, userState); Console.WriteLine("Sending message... press c to cancel mail. Press any other key to exit."); string answer = Console.ReadLine(); // If the user canceled the send, and mail hasn't been sent yet, // then cancel the pending operation. if (answer.StartsWith("c") && mailSent == false) { client.SendAsyncCancel(); } // Clean up. message.Dispose(); Console.WriteLine("Goodbye."); } } }http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx
--Vivek
gaikwad_anil...
Contributor
2805 Points
534 Posts
Re: how to send email from asp.net,C# 4.0
Jul 25, 2012 09:14 AM|LINK
http://thecodekey.com/Articles/ASP_net/send_mail_in_asp_net.aspx
www.thecodekey.com
Please mark as answer if useful
santosh.jagd...
Star
7625 Points
1454 Posts
Re: how to send email from asp.net,C# 4.0
Jul 25, 2012 09:18 AM|LINK
MCP
ahamedthambi
Member
131 Points
118 Posts
Re: how to send email from asp.net,C# 4.0
Jul 25, 2012 09:26 AM|LINK
murali krish...
Member
87 Points
86 Posts
Re: how to send email from asp.net,C# 4.0
Jul 25, 2012 09:58 AM|LINK
using System.Net.Mail; MailMessage message = new MailMessage(); try { message.To.Add(new MailAddress("toaddress")); message.From = new MailAddress("from address"); message.Subject = "Test mail"; message.Body = "this is the mail sended through my c#.net program"; message.BodyEncoding = System.Text.Encoding.UTF8; message.SubjectEncoding = System.Text.Encoding.UTF8; SmtpClient client = new SmtpClient(); client.Port = 587; // Gmail works on this port client.Host = "smtp.gmail.com"; System.Net.NetworkCredential nc = new System.Net.NetworkCredential("yourmail@gmail.com", "your gmail password");// is u are using Gmail client.EnableSsl = true; client.UseDefaultCredentials = false; client.Credentials = nc; client.Send(message); } catch (Exception ex) { ; }I hope this code will help to you.
Thanks,
nageshrgosul
Participant
1044 Points
424 Posts
Re: how to send email from asp.net,C# 4.0
Jul 25, 2012 09:59 AM|LINK
Hi,
MailMessage msg = new MailMessage();
string mail="give mail adress of destination to which u want to send mail";
msg.From = new MailAddress("write ur from adress");
msg.To.Add(mail);
msg.Subject = "write anything what u required";
msg.IsBodyHtml = true;
msg.Priority = MailPriority.High;
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("ur mail id", "password");
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
object userstate = msg;
client.Send(msg);
bhushan_bhar...
Participant
1508 Points
581 Posts
Re: how to send email from asp.net,C# 4.0
Jul 25, 2012 10:23 AM|LINK
Have a look this link
http://usingaspdotnet.blogspot.in/2011/03/email-using-aspnet.html
Useofasp.net/
Howtouseasp.net
mudassarkhan
All-Star
78956 Points
13402 Posts
MVP
Re: how to send email from asp.net,C# 4.0
Jul 25, 2012 10:28 AM|LINK
Complete code with sample available here
http://www.aspsnippets.com/Articles/Send-SMTP-Emails-using-System.Net-Class-in-C.aspx
Contact me
Damn Code
Participant
1883 Points
323 Posts
Re: how to send email from asp.net,C# 4.0
Jul 25, 2012 02:05 PM|LINK
Hi, friend
Follow these steps to implement sending mail task
first write this in source code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Page</title> <style type="text/css"> .style1 { width: 100%; } .style2 { width: 91px; text-align: center; } </style> </head> <body> <form id="form1" runat="server"> <div> <table class="style1"> <tr> <td class="style2"> From:</td> <td> <asp:TextBox ID="TextBox1" runat="server" Width="394px"></asp:TextBox> </td> <td> </td> <td> </td> </tr> <tr> <td class="style2"> To:</td> <td> <asp:TextBox ID="TextBox2" runat="server" Width="394px"></asp:TextBox> </td> <td> </td> <td> </td> </tr> <tr> <td class="style2"> CC:</td> <td> <asp:TextBox ID="TextBox3" runat="server" Width="394px"></asp:TextBox> </td> <td> </td> <td> </td> </tr> <tr> <td class="style2"> BCC:</td> <td> <asp:TextBox ID="TextBox4" runat="server" Width="394px"></asp:TextBox> </td> <td> </td> <td> </td> </tr> <tr> <td class="style2"> Subject:</td> <td> <asp:TextBox ID="TextBox5" runat="server" Width="394px"></asp:TextBox> </td> <td> </td> <td> </td> </tr> <tr> <td class="style2"> Body:</td> <td> <asp:TextBox ID="TextBox6" runat="server" Height="191px" TextMode="MultiLine" Width="394px"></asp:TextBox> </td> <td> </td> <td> </td> </tr> <tr> <td class="style2"> Attachment 1:</td> <td> <asp:FileUpload ID="FileUpload1" runat="server" Width="394px" /> </td> <td> </td> <td> </td> </tr> <tr> <td class="style2"> Attachment 2:</td> <td> <asp:FileUpload ID="FileUpload2" runat="server" Width="394px" /> </td> <td> </td> <td> </td> </tr> <tr> <td class="style2"> </td> <td> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Send" Width="104px" /> </td> <td> </td> <td> </td> </tr> </table> </div> </form> </body> </html>second write this in code behind
using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Net.Mail; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { MailMessage mm = new MailMessage(); mm.From = new MailAddress("yourmail@gmail.com"); mm.To.Add(new MailAddress(TextBox2.Text.Trim())); if (TextBox3.Text != "") { mm.CC.Add(TextBox3.Text); } else if (TextBox4.Text != "") { mm.Bcc.Add(TextBox4.Text); } mm.Subject = TextBox5.Text.Trim(); mm.Body = TextBox6.Text; //Response.Write(FileUpload1.PostedFile.FileName); if (FileUpload1.PostedFile.FileName !="") { mm.Attachments.Add(new Attachment(FileUpload1.PostedFile.FileName)); } // mm.Attachments.Add(new Attachment(FileUpload2.FileName)); mm.Priority = MailPriority.High; mm.IsBodyHtml = true; SmtpClient sc = new SmtpClient(); sc.Host = "smtp.gmail.com"; sc.EnableSsl = true; sc.Port = 587; // sc.UseDefaultCredentials = false; //System.Net.NetworkCredential cred = new System.Net.NetworkCredential("yourmail@gmail.com", "pass"); sc.Credentials = new System.Net.NetworkCredential("yourmail@gmail.com", "pass"); try { sc.Send(mm); Response.Write("The Mail Has Been Sent"); } catch (Exception ex) { Response.Write(ex.Message); } } }Please remember to Mark the replies as Answers if they help & unmark them if they provide no help.