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("[email protected]", "Jane " + (char)0xD8+ " Clayton", System.Text.Encoding.UTF8); // Set destinations for the e-mail message. MailAddress to = new MailAddress("[email protected]"); // 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."); } } }
MailMessage mailMsg = new MailMessage(); mailMsg.From = new MailAddress("[email protected]");//发送邮件用户名 mailMsg.To.Add("[email protected]");//接收邮件用户 mailMsg.Subject = "邮件主题"; mailMsg.Body = "邮件主体内容ffffffffffffffffffff"; mailMsg.BodyEncoding = System.Text.Encoding.UTF8; mailMsg.IsBodyHtml = false; mailMsg.Priority = MailPriority.High; string file = Request.PhysicalApplicationPath + "test\\data.htm"; //附件的地址 // Create the file attachment for this e-mail message. Attachment data = new Attachment(file, MediaTypeNames.Application.Octet); // Add time stamp information for the file. ContentDisposition disposition = data.ContentDisposition; disposition.CreationDate = System.IO.File.GetCreationTime(file); disposition.ModificationDate = System.IO.File.GetLastWriteTime(file); disposition.ReadDate = System.IO.File.GetLastAccessTime(file); mailMsg.Attachments.Add(data); SmtpClient smtp = new SmtpClient(); // 提供身份验证的用户名和密码 // 网易邮件用户可能为:username password // Gmail 用户可能为:[email protected] password smtp.Credentials = new NetworkCredential("banny.liu", "bangy"); smtp.Port = 8383; // Gmail 使用 465 和 587 端口 smtp.Host = "mail.tmiworld.com.cn"; // 如 smtp.163.com, smtp.gmail.com smtp.EnableSsl = false; // 如果使用GMail,则需要设置为true //smtp.SendCompleted += new SendCompletedEventHandler(SendMailCompleted); smtp.SendAsync(mailMsg, mailMsg); Response.Write("成功了"); 这是我的代码·· 你们能帮我看看 错在哪里吗··
http://topic.csdn.net/u/20080916/14/E493C6C2-5E9D-4681-921C-27D30056CAED.html
http://topic.csdn.net/u/20080917/02/264BCC42-B202-4984-A050-8C2AF9879B9C.html
……………………………………………
private static readonly string _emailHost = ConfigurationManager.AppSettings["EMailHost"];
//電子郵件發件人
private static readonly string _emailSender = ConfigurationManager.AppSettings["EMailSender"];
//電子郵件發件人姓名
private static readonly string _emailSenderName = ConfigurationManager.AppSettings["EMailSenderName"];
//電子郵件發件人密碼
private static readonly string _emailSenderPWD = ConfigurationManager.AppSettings["EMailSenderPWD"];
/// <summary>
/// 發送電子郵件
/// </summary>
/// <param name="address">收信人地址</param>
/// <param name="addressName">收信人姓名</param>
/// <param name="subject">主旨</param>
/// <param name="body">內容</param>
/// <returns></returns>
public static bool SendMail(string address , string addressName , string subject , string body)
{
System.Net.Mail.SmtpClient mail = new System.Net.Mail.SmtpClient(_emailHost);
mail.UseDefaultCredentials = false ;
mail.Credentials = new System.Net.NetworkCredential(_emailSender, _emailSenderPWD);
mail.Timeout = 20000 ;
mail.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; // System.Text.Encoding encoding = new System.Text.Encoding.GetEncoding("big5");
//發件人電子郵件
System.Net.Mail.MailAddress senderAddress = new System.Net.Mail.MailAddress(_emailSender, _emailSenderName);
//收件人電子郵件
System.Net.Mail.MailAddress addresseeAddress = new System.Net.Mail.MailAddress(address, addressName); System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(senderAddress, addresseeAddress);
message.Body = body ;
message.Subject = subject ;
//message.SubjectEncoding = encoding
//message.BodyEncoding = encoding
message.IsBodyHtml = true ;
try{
mail.Send(message) ;
return true;
}catch( Exception ex)
{
return false;
}
}
private static readonly string _emailHost = ConfigurationManager.AppSettings["EMailHost"];
//電子郵件發件人
private static readonly string _emailSender = ConfigurationManager.AppSettings["EMailSender"];
//電子郵件發件人姓名
private static readonly string _emailSenderName = ConfigurationManager.AppSettings["EMailSenderName"];
//電子郵件發件人密碼
private static readonly string _emailSenderPWD = ConfigurationManager.AppSettings["EMailSenderPWD"];
/// <summary>
/// 發送電子郵件
/// </summary>
/// <param name="address">收信人地址 </param>
/// <param name="addressName">收信人姓名 </param>
/// <param name="subject">主旨 </param>
/// <param name="body">內容 </param>
/// <returns> </returns>
public static bool SendMail(string address , string addressName , string subject , string body)
{
System.Net.Mail.SmtpClient mail = new System.Net.Mail.SmtpClient(_emailHost);
mail.UseDefaultCredentials = false ;
mail.Credentials = new System.Net.NetworkCredential(_emailSender, _emailSenderPWD);
mail.Timeout = 20000 ;
mail.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; // System.Text.Encoding encoding = new System.Text.Encoding.GetEncoding("big5");
//發件人電子郵件
System.Net.Mail.MailAddress senderAddress = new System.Net.Mail.MailAddress(_emailSender, _emailSenderName);
//收件人電子郵件
System.Net.Mail.MailAddress addresseeAddress = new System.Net.Mail.MailAddress(address, addressName); System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(senderAddress, addresseeAddress);
message.Body = body ;
message.Subject = subject ;
//message.SubjectEncoding = encoding
//message.BodyEncoding = encoding
message.IsBodyHtml = true ;
try{
mail.Send(message) ;
return true;
}catch( Exception ex)
{
return false;
}
}
using System;
using System.Data;
using System.Configuration;
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.Net;
/// <summary>
/// Mail 的摘要说明
/// </summary>
public class Mail
{
public Mail()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="MailFrom">发件人的地址,例如: [email protected]</param>
/// <param name="MailTo">收件人的地址例如: [email protected]</param>
/// <param name="subject">发送邮件的主题</param>
/// <param name="body">发送邮件的正文</param>
/// <param name="host">STMT主机的名称或IP地址</param>
/// <param name="userName">与STMT主机凭据关联的用户名</param>
/// <param name="userPwd">与STMT主机凭据关联的密码</param>
/// <returns>返回是否成功</returns>
public bool SendMail(string MailFrom, string MailTo, string subject, string body, string host, string userName, string userPwd)
{
//string sReturn = string.Empty;
//string MailFrom = "[email protected]";
//string MailTo = "[email protected]";
//string subject = "主题";
//string body = "正文";
//string host = "smtp.sohu.com";
//string userName = "tiancaixgd";
//string userPwd = "7736006";
System.Net.Mail.MailMessage mailMsg = new System.Net.Mail.MailMessage(MailFrom, MailTo, subject, body);
mailMsg.IsBodyHtml = false;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(host, 25);
NetworkCredential NC = new NetworkCredential(userName, userPwd);
smtp.Credentials = NC;
smtp.Timeout = 20*1000;
try
{
smtp.Send(mailMsg);
return true;
}
catch (Exception ee)
{
throw ee;
}
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="MailFrom">发件人的地址,例如: [email protected]</param>
/// <param name="MailTo">收件人的地址例如: [email protected]</param>
/// <param name="subject">发送邮件的主题</param>
/// <param name="body">发送邮件的正文</param>
/// <param name="host">STMT主机的名称或IP地址</param>
/// <param name="userName">与STMT主机凭据关联的用户名</param>
/// <param name="userPwd">与STMT主机凭据关联的密码</param>
/// <param name="port">STMT的端口号</param>
/// <returns>返回是否成功</returns>
public bool SendMail(string MailFrom, string MailTo, string subject, string body, string host, string userName, string userPwd,int port)
{
//string sReturn = string.Empty;
//string MailFrom = "[email protected]";
//string MailTo = "[email protected]";
//string subject = "主题";
//string body = "正文";
//string host = "smtp.sohu.com";
//string userName = "tiancaixgd";
//string userPwd = "7736006";
System.Net.Mail.MailMessage mailMsg = new System.Net.Mail.MailMessage(MailFrom, MailTo, subject, body);
mailMsg.IsBodyHtml = false;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(host, port);
NetworkCredential NC = new NetworkCredential(userName, userPwd);
smtp.Credentials = NC;
smtp.Timeout = 20 * 1000;
try
{
smtp.Send(mailMsg);
return true;
}
catch (Exception ee)
{
throw ee;
}
}
}
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("[email protected]",
"Jane " + (char)0xD8+ " Clayton",
System.Text.Encoding.UTF8);
// Set destinations for the e-mail message.
MailAddress to = new MailAddress("[email protected]");
// 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.");
}
}
}
mailMsg.From = new MailAddress("[email protected]");//发送邮件用户名
mailMsg.To.Add("[email protected]");//接收邮件用户
mailMsg.Subject = "邮件主题";
mailMsg.Body = "邮件主体内容ffffffffffffffffffff";
mailMsg.BodyEncoding = System.Text.Encoding.UTF8;
mailMsg.IsBodyHtml = false;
mailMsg.Priority = MailPriority.High; string file = Request.PhysicalApplicationPath + "test\\data.htm"; //附件的地址
// Create the file attachment for this e-mail message.
Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
// Add time stamp information for the file.
ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
mailMsg.Attachments.Add(data); SmtpClient smtp = new SmtpClient();
// 提供身份验证的用户名和密码
// 网易邮件用户可能为:username password
// Gmail 用户可能为:[email protected] password
smtp.Credentials = new NetworkCredential("banny.liu", "bangy");
smtp.Port = 8383; // Gmail 使用 465 和 587 端口
smtp.Host = "mail.tmiworld.com.cn"; // 如 smtp.163.com, smtp.gmail.com
smtp.EnableSsl = false; // 如果使用GMail,则需要设置为true
//smtp.SendCompleted += new SendCompletedEventHandler(SendMailCompleted);
smtp.SendAsync(mailMsg, mailMsg);
Response.Write("成功了");
这是我的代码·· 你们能帮我看看 错在哪里吗··