日期:2014-05-20  浏览次数:20811 次

谁给我传几个.net 发送邮件的代码
要能够发送附件的。

------解决方案--------------------
using System.Web.Mail;


 MailMessage msg = new MailMessage();
//发送方地址(如aaa@163.com) 
msg.From = "longyun1077@163.com";
//接收方地址(如123@163.com) 
msg.To = CleanString.InputText(username.Text.ToString().Trim(), 30);
//正文内容类型 
msg.BodyFormat = MailFormat.Html;
//正文内容编码 
msg.BodyEncoding = System.Text.Encoding.Default;
 //主题 
msg.Subject = "恭喜您成功注册龙运在线!";
 //内容 
msg.Body = "尊敬的" + CleanString.InputText(Tusernc.Text.ToString().Trim(), 30) + "用户,恭喜您成功注册<a href='www.aaa.com'>aaa</a>!您的密码为" + CleanString.InputText(password.Text.ToString().Trim(), 30) + "!请妥协保管!";
//设置为需要用户验证 
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//设置验证用户名 
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "xxx");
//设置验证密码 
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "xxx");
//邮件服务器地址(如smtp.163.com) 
SmtpMail.SmtpServer = "smtp.163.com";
//发送 
SmtpMail.Send(msg);

------解决方案--------------------
using System.Web.Mail;
MailMessage objMailMessage;
MailAttachment objMailAttachment;
// 创建一个附件对象
objMailAttachment = new MailAttachment( "d:\\test.txt" );//发送邮件的附件
// 创建邮件消息
objMailMessage = new MailMessage();
objMailMessage.From = "mysina@sina.com";//源邮件地址
objMailMessage.To = "scucj@126.com";//目的邮件地址,也就是发给我哈
objMailMessage.Subject = "邮件发送标题:你好";//发送邮件的标题
objMailMessage.Body = "邮件发送标内容:测试一下是否发送成功!";//发送邮件的内容
objMailMessage.Attachments.Add( objMailAttachment );//将附件附加到邮件消息对象中
//接着利用sina的SMTP来发送邮件,需要使用Microsoft .NET Framework SDK v1.1和它以上的版本
//基本权限
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1";
//用户名
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "mysina" ;
//密码
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "123";
//如果没有上述三行代码,则出现如下错误提示:服务器拒绝了一个或多个收件人地址。服务器响应为: 554 : Client host rejected: Access denied
//SMTP地址
SmtpMail.SmtpServer = "smtp.sina.com.cn";
//开始发送邮件
SmtpMail.Send( objMailMessage );