首页 > 代码库 > c# 发送邮件

c# 发送邮件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            var smtp = new System.Net.Mail.SmtpClient("ipordomain", 25);
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = new System.Net.NetworkCredential("user", "pass");
            using (var mail = new System.Net.Mail.MailMessage())
            {
                mail.Subject = "邮件" + DateTime.Now.ToString("yyyyMMddHHmmss");
                mail.SubjectEncoding = System.Text.Encoding.GetEncoding("GB2312");
                mail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
                mail.From = new System.Net.Mail.MailAddress("mouren@mouwang.net","mouren");
                mail.Priority = System.Net.Mail.MailPriority.Normal;
                mail.IsBodyHtml = false;
                mail.Body = "某某人给自己发了一封邮件。这是一封伪造发件人的测试邮件!";
                mail.To.Add("fanjinfei@firstelite.net");

                //只有部分服务器允许任意发件人,163邮箱不允许

                try
                {
                    smtp.Send(mail);
                    Console.WriteLine("发送成功!");
                }
                catch (Exception ex)
                {
                    Debug.Print(ex.ToString());
                    Console.WriteLine("发送失败!");
                }

                Console.ReadLine();
            }
        }
    }
}

 

c# 发送邮件