提交的同时发送邮件。
偶滴
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
protected void sendEmail(string sendTo, string txtCC, string sendFrom, string sendSubject, string sendMessage)
{
// create mail message object
try
{
// create the email message
MailMessage message = new MailMessage(
sendFrom,
sendTo,
sendSubject,
sendMessage);
MailAddress SendCC = new MailAddress(txtCC);
message.CC.Add(SendCC);
// MailAddress SendBCC = new MailAddress("xx@xxxx");
// message.Bcc.Add(SendBCC);
// create smtp client at mail server location
SmtpClient client = new
SmtpClient( " smtp.moultxxxxx.com " );
// add credentials
client.UseDefaultCredentials = true ;
// client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
// send message
client.Send(message);
}
catch (Exception ex)
{
......;
}
}
别人地~
ASP.NET 2.0(C#)发邮件实例(源代码)
{
MailMessage message = new MailMessage();
message.To.Add( " #####@###.### " ); // 收件人
message.Subject = " 主题 " ; // 主题
message.From = new System.Net.Mail.MailAddress( " *****@****.**** " , " ***** " ); // 发件人邮箱及昵称
message.Body = " 内容 " ; // 内容
SmtpClient smtpClient = new SmtpClient();
smtpClient.EnableSsl = false ; // 是否使用安全套接字层 (SSL) 加密连接
smtpClient.Host = " smtp.163.com " ;
smtpClient.Port = 25 ; // 端口号
smtpClient.Credentials = new System.Net.NetworkCredential( " ***** " , " ****** " ); // 填写登录邮箱的用户名及密码
try {
smtpClient.Send(message);
Response。Write( " <script language= " javascript " >window。alert('成功!');</script> " ); // 此处用“。”代替“.”以免影响此页面浏览
}
catch
{
Response。Write( " <script language= " javascript " >window。alert('失败!');</script> " ); // 此处用“。”代替“.”以免影响此页面浏览
}
}
如果选择了163的SMTP服务器,“smtp.163.com”,发件人必须是登录用户的163 email用户地址。
smtp.Credentials = new System.Net.NetworkCredential("163emailaddress","emailpassword");
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.From = new System.Net.Mail.MailAddress("163emailaddress@163.com");
C#发送Email邮件三种方法的总结
通过.Net FrameWork 2.0下提供的“System.Net.Mail”可以轻松的实现,本文列举了3种途径来发送:
1.通过Localhost;
2.通过普通SMTP;
3.通过SSL的SMTP;
下面一个一个来说:
1.通过LocalHost
public void SendMailLocalhost()
{
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.To.Add("a@a.com");
msg.To.Add("b@b.com");
/* msg.To.Add("b@b.com");
* msg.To.Add("b@b.com");
* msg.To.Add("b@b.com");可以发送给多人
*/
msg.CC.Add(c@c.com);
/*
* msg.CC.Add("c@c.com");
* msg.CC.Add("c@c.com");可以抄送给多人
*/
msg.From = new MailAddress("a@a.com", "AlphaWu", System.Text.Encoding.UTF8);
/* 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码*/
msg.Subject = "这是测试邮件";//邮件标题
msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
msg.Body = "邮件内容";//邮件内容
msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
msg.IsBodyHtml = false;//是否是HTML邮件
msg.Priority = MailPriority.High;//邮件优先级
SmtpClient client = new SmtpClient();
client.Host = "localhost";
object userState = msg;
try
{
client.SendAsync(msg, userState);
//简单一点儿可以client.Send(msg);
MessageBox.Show("发送成功");
}
catch (System.Net.Mail.SmtpException ex)
{
MessageBox.Show(ex.Message, "发送邮件出错");
}
}
2.通过普通SMTP
public void SendMailUseZj()
{
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.To.Add(a@a.com);
msg.To.Add(b@b.com);
/*
* msg.To.Add("b@b.com");
* msg.To.Add("b@b.com");
* msg.To.Add("b@b.com");可以发送给多人
*/
msg.CC.Add("c@c.com");
/*
* msg.CC.Add("c@c.com");
* msg.CC.Add("c@c.com");可以抄送给多人
*/
msg.From = new MailAddress("a@a.com", "AlphaWu", System.Text.Encoding.UTF8);
/* 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码*/
msg.Subject = "这是测试邮件";//邮件标题
msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
msg.Body = "邮件内容";//邮件内容
msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
msg.IsBodyHtml = false;//是否是HTML邮件
msg.Priority = MailPriority.High;//邮件优先级
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("username@zj.com", "userpass");
//在zj.com注册的邮箱和密码
client.Host = "smtp.zj.com";
object userState = msg;
try
{
client.SendAsync(msg, userState);
//简单一点儿可以client.Send(msg);
MessageBox.Show("发送成功");
}
catch (System.Net.Mail.SmtpException ex)
{
MessageBox.Show(ex.Message, "发送邮件出错");
}
}
上述方法不适用于所有SMTP,经测试zj.com可以,而smtp.163.com不行
3.通过SSL的SMTP
public void SendMailUseGmail()
{
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.To.Add(a@a.com);
msg.To.Add(b@b.com);
/*
* msg.To.Add("b@b.com");
* msg.To.Add("b@b.com");
* msg.To.Add("b@b.com");可以发送给多人
*/
msg.CC.Add(c@c.com);
/*
* msg.CC.Add("c@c.com");
* msg.CC.Add("c@c.com");可以抄送给多人
*/
msg.From = new MailAddress("a@a.com", "AlphaWu", System.Text.Encoding.UTF8);
/* 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码*/
msg.Subject = "这是测试邮件";//邮件标题
msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
msg.Body = "邮件内容";//邮件内容
msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
msg.IsBodyHtml = false;//是否是HTML邮件
msg.Priority = MailPriority.High;//邮件优先级
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("username@gmail.com", "password");
//上述写你的GMail邮箱和密码
client.Port = 587;//Gmail使用的端口
client.Host = "smtp.gmail.com";
client.EnableSsl = true;//经过ssl加密
object userState = msg;
try
{
client.SendAsync(msg, userState);
//简单一点儿可以client.Send(msg);
MessageBox.Show("发送成功");
}
catch (System.Net.Mail.SmtpException ex)
{
MessageBox.Show(ex.Message, "发送邮件出错");
}
}
通过Gmail来发送邮件,成功率极高,几乎都可以发到,推荐使用。
.net1.1
using System.Web.Mail;
使用:
SendSMTPEMail("100.100.100.100", "someone@xxx.com", "xxxx", "someone@xxx.com", "webtest", TextBox1.Text, null, null);
方法体:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
{
System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
mail.BodyFormat = MailFormat.Html;
mail.From = strFrom;
mail.To = strto; // 多个收件人之间用分号分割
mail.Bcc = bcc;
mail.Cc = cc;
mail.Subject = " mail test!!! " ;
mail.Body = TextBox1.Text;
mail.Fields.Add( " http://schemas.microsoft.com/cdo/configuration/smtpauthenticate " , " 1 " );
mail.Fields.Add( " http://schemas.microsoft.com/cdo/configuration/sendusername " , strFrom);
mail.Fields.Add( " http://schemas.microsoft.com/cdo/configuration/sendpassword " , strFromPass);
System.Web.Mail.MailAttachment attachment = new System.Web.Mail.MailAttachment( " c:\\log.log " );
mail.Attachments.Add(attachment);
SmtpMail.SmtpServer = strSmtpServer;
SmtpMail.Send(mail);
}
.net2.0
using System.Net.Mail;
使用:
SendSMTPEMail("100.100.100.100", "someone@xxx.com", "xxxx", "someone@xxx.com", "webtest", TextBox1.Text, null, null);
方法体:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
{
System.Net.Mail.SmtpClient client = new SmtpClient(strSmtpServer);
client.UseDefaultCredentials = false ;
client.Credentials = new System.Net.NetworkCredential(strFrom, strFromPass);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(strFrom, strto, strSubject, strBody);
System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment( " c:\\log.log " );
message.Attachments.Add(attachment);
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true ;
client.Send(message);
}