c# 发送邮件
2007-06-02 10:56
232 查看
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());
}
}
}
相关文章推荐
- 使用C#发送邮件
- C# 发送邮件
- C#编程实现发送邮件的方法(可添加附件)
- c#发送邮件之System.Net.Mail方法
- C# 发送邮件
- C#System.Net.Mail采用简单邮件传输协议发送邮件
- c#.net 下的邮件发送(含附件)
- C# - MailSender 邮件发送组件源代码 (支持ESMTP, 附件)
- C# 发送邮件代码
- C#邮件发送
- c# 邮件发送通用类
- C#发送邮件
- C#使用smtp发送邮件
- C#中如何利用Gmail账户发送邮件
- C#封装发送邮件SendMail函数
- C#通过Outlook发送邮件
- 通过C#发送自定义的html格式邮件
- 20101015 学习记录2:c# 发送邮件
- C#邮件发送代码实现(MailSender.cs)
- C#使用CDO发送邮件的方法