日期:2014-05-17 浏览次数:20454 次
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.Mail; namespace MailSender { public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { MailMessage objMailMessage; MailAttachment objMailAttachment; // 创建一个附件对象 objMailAttachment = new MailAttachment("C:\\1.xml");//发送邮件的附件 // 创建邮件消息 objMailMessage = new MailMessage(); objMailMessage.From = "mytest110@sina.com";//源邮件地址 objMailMessage.To = "********@qq.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", "mytest110"); //密码 objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "******"); //如果没有上述三行代码,则出现如下错误提示:服务器拒绝了一个或多个收件人地址。服务器响应为:530 Authentication required //SMTP地址 SmtpMail.SmtpServer = "smtp.sina.com"; // 开始发送邮件 // 在发送之前,去新浪邮箱里开启POP/SMTP设置 邮箱设置->账户->POP/SMTP设置->开启 // 否则会报错误0x80040217. The server response was not available SmtpMail.Send(objMailMessage); } } }