C#发送邮件
2008-07-23 13:52
155 查看
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.ComponentModel;
public partial class WebMail_Mail : System.Web.UI.Page
{
static bool mailSent = false;
static string ReturnString = "";
public static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
// Get the unique identifier for this asynchronous operation.
String token = (string)e.UserState;
if (e.Cancelled)
{
ReturnString = token + " Send canceled.";
//Response.Write(ss);
}
if (e.Error != null)
{
//Response.Write(token +" "+e.Error.ToString());
ReturnString = token + " " + e.Error.ToString();
}
else
{
//Response.Write("Message sent.");
ReturnString = "Message sent.";
}
mailSent = true;
}
protected void Page_Load(object sender, EventArgs e)
{
/*
MailMessage msg = new MailMessage();
//
MailAddress Fr_address = new MailAddress("oapro_test@163.com", "oapro_test");
MailAddressCollection cc_address = new MailAddressCollection();
cc_address.Add("oapro_test@163.com");
msg.From = Fr_address;
msg.Subject = "this is a test By bxf";
msg.Body = "白肖凤测试";
//身份严正
System.Net.ICredentialsByHost Credential;
//
SmtpClient smtp = new SmtpClient("smtp.163.com",25);
smtp.Timeout = 30;
//smtp.Send(msg);
smtp.Credentials =
*/
/*
System.Net.Mail.SmtpClient client = new SmtpClient("smtp.163.com",25);
client.UseDefaultCredentials = true;
client.Credentials = new System.Net.NetworkCredential("oapro_test", "");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new MailMessage("oapro_test@163.com", "oapro_test@163.com", "this is a test By bxf", "白肖凤测试");
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
try
{
client.Send(message);
}
catch (System.Net.Mail.SmtpException ex)
{
Response.Write(ex.ToString());
}
)*/
// Command line argument must the the SMTP host.
SmtpClient client = new SmtpClient("smtp.163.com",25);
//验证
client.UseDefaultCredentials = true;
client.Credentials = new System.Net.NetworkCredential("", "");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
// Specify the e-mail sender.
// Create a mailing address that includes a UTF8 character
// in the display name.
MailAddress from = new MailAddress("bxf0011@163.com",
"Jane " + (char)0xD8+ " Clayton",
System.Text.Encoding.UTF8);
// Set destinations for the e-mail message.
MailAddress to = new MailAddress("oapro_test@163.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";
try
{
// client.SendAsync(message, userState);
client.Send(message);
Response.Write("Sending message... press c to cancel mail. Press any other key to exit.");
string answer = ReturnString;
// 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();
Response.Write("Goodbye.");
}
catch (System.Net.Mail.SmtpException ex)
{
Response.Write(ex.ToString());
}
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.ComponentModel;
public partial class WebMail_Mail : System.Web.UI.Page
{
static bool mailSent = false;
static string ReturnString = "";
public static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
// Get the unique identifier for this asynchronous operation.
String token = (string)e.UserState;
if (e.Cancelled)
{
ReturnString = token + " Send canceled.";
//Response.Write(ss);
}
if (e.Error != null)
{
//Response.Write(token +" "+e.Error.ToString());
ReturnString = token + " " + e.Error.ToString();
}
else
{
//Response.Write("Message sent.");
ReturnString = "Message sent.";
}
mailSent = true;
}
protected void Page_Load(object sender, EventArgs e)
{
/*
MailMessage msg = new MailMessage();
//
MailAddress Fr_address = new MailAddress("oapro_test@163.com", "oapro_test");
MailAddressCollection cc_address = new MailAddressCollection();
cc_address.Add("oapro_test@163.com");
msg.From = Fr_address;
msg.Subject = "this is a test By bxf";
msg.Body = "白肖凤测试";
//身份严正
System.Net.ICredentialsByHost Credential;
//
SmtpClient smtp = new SmtpClient("smtp.163.com",25);
smtp.Timeout = 30;
//smtp.Send(msg);
smtp.Credentials =
*/
/*
System.Net.Mail.SmtpClient client = new SmtpClient("smtp.163.com",25);
client.UseDefaultCredentials = true;
client.Credentials = new System.Net.NetworkCredential("oapro_test", "");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new MailMessage("oapro_test@163.com", "oapro_test@163.com", "this is a test By bxf", "白肖凤测试");
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
try
{
client.Send(message);
}
catch (System.Net.Mail.SmtpException ex)
{
Response.Write(ex.ToString());
}
)*/
// Command line argument must the the SMTP host.
SmtpClient client = new SmtpClient("smtp.163.com",25);
//验证
client.UseDefaultCredentials = true;
client.Credentials = new System.Net.NetworkCredential("", "");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
// Specify the e-mail sender.
// Create a mailing address that includes a UTF8 character
// in the display name.
MailAddress from = new MailAddress("bxf0011@163.com",
"Jane " + (char)0xD8+ " Clayton",
System.Text.Encoding.UTF8);
// Set destinations for the e-mail message.
MailAddress to = new MailAddress("oapro_test@163.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";
try
{
// client.SendAsync(message, userState);
client.Send(message);
Response.Write("Sending message... press c to cancel mail. Press any other key to exit.");
string answer = ReturnString;
// 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();
Response.Write("Goodbye.");
}
catch (System.Net.Mail.SmtpException ex)
{
Response.Write(ex.ToString());
}
}
}
相关文章推荐