using System.Net.Mail;试了很多SMTP发送邮件的代码,结果相同。 代码: using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Mail; using System.Net.Mime; using System.Threading; using System.ComponentModel; namespace 测试 { //smtp类 class Class1 { static bool mailSent = false; public 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) { string svr = "mail.lierda.com"; // Command line argument must the the SMTP host. SmtpClient client = new SmtpClient(svr, 25); // Specify the e-mail sender. // Create a mailing address that includes a UTF8 character // in the display name. MailAddress from = new MailAddress("[email protected]"); // "yuxiaozhong" + (char)0xD8 + "4821698",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.Credentials = new System.Net.NetworkCredential(from.Address, "11181118", "lierda.com");// 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. //client.SendAsyncCancel(); if (answer.StartsWith("c") && mailSent == false) { client.SendAsyncCancel(); } // Clean up. message.Dispose(); Console.WriteLine("Goodbye."); } } }
下面是个工具代码
引用System.Net.Mail;命名空间
/// <summary>
/// 发送E-mail
/// </summary>
/// <param name="TO">收件人(仅支持单个收件人)</param>
/// <param name="subject">邮件主题</param>
/// <param name="body">邮件内容,支持 HTML</param>
public static bool SendMail(string TO, string subject, string body, bool isHtml)
{
string[] batchTo = TO.Split(';');
foreach (var to in batchTo)
{
MailMessage mail = new MailMessage("[email protected]", to);
mail.From = new MailAddress(ConfigurationManager.AppSettings["SmtpSender"],
ConfigurationManager.AppSettings["DisplayName"]);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = isHtml;
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["SmtpServer"];
smtp.Port = int.Parse(ConfigurationManager.AppSettings["SmtpPort"]);
System.Net.NetworkCredential credential = new System.Net.NetworkCredential();
credential.UserName = ConfigurationManager.AppSettings["SmtpUsername"];
credential.Password = ConfigurationManager.AppSettings["SmtpPin"];
smtp.Credentials = credential;
try
{
smtp.Send(mail);
if (batchTo.Length == 1)
return true;
}
catch (SmtpException)
{
if (batchTo.Length == 1)
return false;
}
} return true;
}
using System.Web.Mail;
using System.Net;
public void Fyj()//用户注册完可以调用这个发邮件方法
{ try
{
MailMessage Mail = new MailMessage();
Mail.From = "[email protected]";//你要发邮件地址
Mail.To = "[email protected]";//目标地址(要发的用户邮箱地址)
Mail.Subject = "测试发送邮件!";//邮件标题
Mail.Body = "哈哈测试下"; //邮件内容
Mail.BodyFormat = MailFormat.Html;
Mail.Priority = MailPriority.High;
Mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
Mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "你的用户名");//用户名
Mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "你的邮箱密码");//密码
System.Web.Mail.SmtpMail.SmtpServer = "smtp.163.com";
System.Web.Mail.SmtpMail.Send(Mail);
Response.Write("<script>alert('发送成功!')</script>");
}
catch (Exception exp)
{
Response.Write(exp.Message);
}
}
程序如下:
jmail.Message jmessage=new jmail.MessageClass();
jmessage.Charset="GB2312";//编码
jmessage.From="[email protected]";//发送人的邮件地址
jmessage.FromName= "系统管理员" ;//发送人的姓名
//jmessage.ReplyTo="[email protected]";//回复时的地址
jmessage.Subject="你的密码!";//邮件主题
jmessage.AddRecipient(email ,"","");//接收人的邮件地址,姓名
jmessage.Body= "你在本网站的帐号密码已经成功找回,请你妥善保管!卡号:"+cardid+"密码:"+cardpassword ;//邮件内容
jmessage.MailServerUserName= "flcandclf" ;//发送者邮箱的用户名
jmessage.MailServerPassWord= "flc198068" ;//发送者邮箱的密码
jmessage.Send("smtp.sohu.com",false) ;//发送着邮箱的smtp服务器地址
Response.Write("<script language='javascript'>alert('密码已成功发送到指定邮箱,请查收!');</script>");
jmessage.Close() ;
看看这个
装了防火墙就不好用了。
{
////设置发件人信箱,及显示名字
MailAddress from = new MailAddress("[email protected]", "J.L.C");
//设置收件人信箱,及显示名字
MailAddress to = new MailAddress(model.Email, model.LoginName);
//创建一个MailMessage对象
MailMessage oMail = new MailMessage(from, to); oMail.Subject = "购物网"; //邮件标题
oMail.Body = "你的恢复密码是" + model.LoginName; //邮件内容 oMail.IsBodyHtml = true; //指定邮件格式,支持HTML格式
oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");//邮件采用的编码
oMail.Priority = MailPriority.High;//设置邮件的优先级为高 //发送邮件服务器
SmtpClient client = new SmtpClient();
client.Host = "smtp.163.com"; //指定邮件服务器
client.Credentials = new NetworkCredential("[email protected]", "*****");//指定服务器邮件,及密码 //发送
try
{
client.Send(oMail); //发送邮件
lbEmail.Text = "恭喜你!邮件发送成功。"; }
catch
{
lbEmail.Text = "邮件发送失败,检查网络及信箱是否可用。";
} oMail.Dispose(); //释放资源
}
参考
http://www.cnblogs.com/jailu/archive/2007/04/10/707036.htmlhttp://www.cnblogs.com/mac-j/archive/2008/08/08/1263705.html
2 jmail
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Threading;
using System.ComponentModel; namespace 测试
{ //smtp类
class Class1
{
static bool mailSent = false;
public 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)
{
string svr = "mail.lierda.com";
// Command line argument must the the SMTP host.
SmtpClient client = new SmtpClient(svr, 25); // Specify the e-mail sender.
// Create a mailing address that includes a UTF8 character
// in the display name.
MailAddress from = new MailAddress("[email protected]");
// "yuxiaozhong" + (char)0xD8 + "4821698",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.Credentials = new System.Net.NetworkCredential(from.Address, "11181118", "lierda.com");//
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.
//client.SendAsyncCancel();
if (answer.StartsWith("c") && mailSent == false)
{
client.SendAsyncCancel();
}
// Clean up.
message.Dispose();
Console.WriteLine("Goodbye.");
}
}
}