用C#发邮件

2021-04-21 14:26

阅读:348

标签:邮件服务器   测试的   obj   add   default   post   发送   odi   net   

以下有2种方法:

第一种:

using System;
using System.Net.Mail;

//当时测试的是QQ邮箱
class TestMail
{
    static void Main(string[] args)
    {
        string mailContent = "邮件内容";
        MailMessage msg = new System.Net.Mail.MailMessage();
        msg.To.Add("416795230@qq.com,693679419@qq.com"); //收件人

        //发件人信息
        msg.From = new MailAddress("693679419@qq.com", "发送人姓名", System.Text.Encoding.UTF8);
        msg.Subject = "这是测试邮件";   //邮件标题
        msg.SubjectEncoding = System.Text.Encoding.UTF8;    //标题编码
        msg.Body = mailContent; //邮件主体
        msg.BodyEncoding = System.Text.Encoding.UTF8;
        msg.IsBodyHtml = true;  //是否HTML
        msg.Priority = MailPriority.High;   //优先级
        SmtpClient client = new SmtpClient();
        //注意:下面的这个密码是授权码,不是你登录QQ的密码,查看方法:QQ邮箱---设置里面
        client.Credentials = new System.Net.NetworkCredential("693679419@qq.com", "asdfghjkl");
        client.EnableSsl = true;
        client.Port = 587;   //443
        client.Host = "smtp.qq.com";//mail.adient.com
        object userState = msg;
        try
        {
            client.Send(msg);
            //MessageBox.Show("发送成功");
        }
       catch (Exception ex)
       {
            //MessageBox.Show(ex.Message, "发送邮件出错");
       }
    }
}

 

 

 

第二种:

//当时测试的是outlook 延锋的邮箱
using System.Net.Mail;

namespace ConsoleApplication11
{
    class Program
    {
        static void Main(string[] args)
        {
            MailMessage mail = new MailMessage();
            //设置邮件的标题            
            mail.Subject = "测试邮件";
            //设置邮件的发件人            
            //mail.From = new MailAddress("yingchun.hou@adient.com", "Xiao_1006");
            //发件人的邮箱地址,后面参数 张猛:收件人看到的发件人的名称
            mail.From = new MailAddress("YFAS.SWMS_Quality@adient.com", "张猛");
            //设置邮件的收件人,ZMENG没有什么用        
            mail.To.Add("416795230@qq.com,693679419@qq.com");
            //设置邮件的抄送人             
         //   mail.CC.Add(new MailAddress("nick.yin@amusegroup.com", "Nick"));
            //设置邮件的内容             
            mail.Body = "就是测试用111";
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            mail.IsBodyHtml = true;
            mail.Priority = MailPriority.Normal;
            mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
            SmtpClient client = new SmtpClient();
            //邮件服务器的名称,要问对方公司才知道
            client.Host = "smtp.ga.adient.com";
            //发邮件的端口号,要问对方公司才知道
            client.Port = 25;
            client.UseDefaultCredentials = false;
            //client.Credentials = new System.Net.NetworkCredential("yingchun.hou@adient.com", "Xiao_1006");
            //发件人的账号和密码
            client.Credentials = new System.Net.NetworkCredential("YFAS.SWMS_Quality@adient.com", "Yfas123");
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Send(mail);
        }
    }
}

 

用C#发邮件

标签:邮件服务器   测试的   obj   add   default   post   发送   odi   net   

原文地址:https://www.cnblogs.com/ziqiumeng/p/8580400.html


评论


亲,登录后才可以留言!