MailMessage发送邮件的例子:C# 发送邮件的Helper类 主要就是用Web命名空间下的Mail.Mailmessage类用此类发送邮件主要就是对MailMessage类的实例设置一些属性,然后用SmtpMail指定smtp服务器发送。代码如下:
代码如下 | 复制代码 |
<span id="more-1794"></span> using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Mail; using System.Net.Mime; using System.IO; using System.Timers; using System.Xml; using System.Net; namespace TEST.SUBUNIT { public class EmailHelper { public MailMessage mailMessage(string fromAddress,string desemailuser, string toAddress, string emailSubject, string emailText) //邮件的发送收者,支持群发,多个地址之间用 半角逗号 分开 { MailMessage mm = new MailMessage(); mm.To.Add("abc@qq.com"); //mm.To.Add(toAddress);//暂时屏蔽 ////抄送 mm.CC.Add(new MailAddress("11@qq.com", "张三", Encoding.GetEncoding(936))); mm.CC.Add(new MailAddress("22@qq.com", "李四", Encoding.GetEncoding(936))); mm.CC.Add(new MailAddress("33@126.com", "王五", Encoding.GetEncoding(936))); //mm.Bcc.Add("44@qq.com,44@qq.com");
mm.From = new MailAddress(fromAddress, desemailuser.Length>0?desemailuser:fromAddress, Encoding.GetEncoding(936)); mm.SubjectEncoding = Encoding.GetEncoding(936);//这里非常重要,如果你的邮件标题包含中文,这里一定要指定,否则对方收到的极有可能是乱码。 mm.Subject = emailSubject; //邮件标题 mm.IsBodyHtml = true; //邮件正文是否是HTML格式
mm.BodyEncoding = Encoding.GetEncoding(936); //邮件正文的编码, 设置不正确, 接收者会收到乱码 //------------------------------------------------------------------------- emailText += "这是要正确发送的邮箱:" + toAddress; mm.Body = emailText;//邮件正文 mm.Priority = MailPriority.High; //邮件的优先级,分为 Low, Normal, High,通常用 Normal即可 //mm.Attachments.Add(new Attachment(@"d:a.doc",System.Net.Mime.MediaTypeNames.Application.Rtf)); ////第二个参数,表示附件的文件类型,可以不用指定 return mm; }
public bool sendEmail(string fromemailaddress, string toemailaddress,string emailtitle,string emailcontent,string host,string port,string fromemailuser,string desemailuser,string fromemailpass) { bool b=false;
string message=string.Empty;
MailMessage mailessage =this.mailMessage(fromemailaddress,desemailuser,toemailaddress,emailtitle,emailcontent); SmtpClient smtp = new SmtpClient(); //实例化一个SmtpClient smtp.DeliveryMethod = SmtpDeliveryMethod.Network; //将smtp的出站方式设为 Network smtp.EnableSsl = false;//smtp服务器是否启用SSL加密 smtp.Host = host; //指定 smtp 服务器地址 smtp.Port = int.Parse(port); //指定 smtp 服务器的端口,默认是25 smtp.Credentials = new NetworkCredential(fromemailuser, fromemailpass);//认证 try { smtp.Send(mailessage); b = True; message= "发送成功!"; } catch (System.Net.Mail.SmtpException ex) { b = False; message= "发送失败!"; } return b; }
} } |
例2
代码如下 | 复制代码 |
using Systme.Web.Mail;//引入命名空间 protected void Button1_Click(object sender, EventArgs e) { MailMessage mmsg = new MailMessage();//实例一个mailmessage mmsg.Priority = MailPriority.Low;//设置优先级别 mmsg.From = "weicfprince@163.com";//发件人 mmsg.To = "weicfprince@hotmail.com";//收件人 mmsg.BodyFormat = MailFormat.Text;//正文格式 mmsg.Subject = "邮件发送测试"; mmsg.Body = "我给我自己另一个邮箱发送了一个文本邮件"; mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1); //发件人邮箱信息 mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "weicfprince"); mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "******");//密码在此保密哈哈 SmtpMail.SmtpServer = "smtp.163.com";//指定smtp服务器 try { SmtpMail.Send(mmsg);//发送邮件 Response.Write("1"); } catch (Exception ex) { Response.Write(ex.Message); } } |
当然也还可以发送附件,用MailAttachments,它提供属性和方法来创建一个邮件附件对象.
MailAttachments ma= new MailAttachment( "d://test.txt" );//发送邮件的附件
mmsg.Attachments.Add( ma);//将附件附加到邮件消息对象中
以上是完全可以实现发送邮件功能的,只是有些邮箱不可以发送,原因还是不太清楚。