发送邮件代码


using System.Net.Mail;

    public void SendEmail() { MailMessage mail = new MailMessage(); //设置邮件的标题 mail.Subject = "EmailTest"; //设置邮件的发件人 mail.From = new MailAddress("HSXsend@163.com", "hsx_send"); //设置邮件的收件人 mail.To.Add(new MailAddress("HSXreceive@163.com", "hsx_receive")); //设置邮件的抄送人 mail.CC.Add(new MailAddress("kindenough2@163.com", "mail_CC")); //设置邮件的内容 mail.Body = "就是测试用"; //设置邮件的格式 mail.BodyEncoding = System.Text.Encoding.UTF8; mail.IsBodyHtml = true; ////设置邮件的发送级别 mail.Priority = MailPriority.Normal; mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess; SmtpClient client = new SmtpClient(); //设置用于 SMTP 事务的主机的名称,填IP地址也可以了 http://mail.163.com/ //client.Host = "smtp.163.com"; client.Host = "smtp.126.com"; //设置用于 SMTP 事务的端口,默认的是 25 client.Port = 25; client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential("HSXsend@163.com", "这里是邮箱密码"); client.DeliveryMethod = SmtpDeliveryMethod.Network; //附件 string strFilePath = @"C:\test.jpg"; Attachment at = new Attachment(strFilePath); at.Name = System.IO.Path.GetFileName(strFilePath); at.NameEncoding = System.Text.Encoding.GetEncoding("gb2312"); at.TransferEncoding = System.Net.Mime.TransferEncoding.Base64; at.ContentDisposition.Inline = true; at.ContentDisposition.DispositionType = System.Net.Mime.DispositionTypeNames.Inline; string cid = at.ContentId; mail.Attachments.Add(at); //都定义完了,正式发送了! client.Send(mail); }

 

posted @ 2013-11-27 16:11  just_xing  阅读(266)  评论(0编辑  收藏  举报