C#邮件发送代码

 using System;

using System.Collections.Generic;

using System.Linq;

using System.Net.Mail;

using System.Text;

using System.Net;

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            SendEmail("[email protected]", "[email protected]", "测试邮件", "测试", false, "密码");



        }







        static private void SendEmail(string fromAddress, string toAddress, string msgSubject, string body, bool isHtmlBody, string smtpPassword)

        {

            SmtpClient client = new SmtpClient();



            MailMessage message = new MailMessage();



            // 设置发信人的EMAIL地址

            message.From = new MailAddress(fromAddress);



            // 设置收信人的EMAIL地址

            message.To.Add(toAddress);

            // 设置回复的EMAIL地址

         //   message.ReplyTo = new MailAddress(replyToAddress);



            // 设置抄送的EMAIL地址

            // message.CC.Add(ccAddress);

            // message.Bcc.Add(bccAddress);



            // 设置发信主题及内容

            message.Subject = msgSubject;

            message.Body = body;

            message.IsBodyHtml = isHtmlBody;

            // 设置SMTP host及端口

            client.Host = "smtp.qq.com";

            client.Port = 25;

            client.UseDefaultCredentials = false;

            System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential(fromAddress, smtpPassword);

            client.Credentials = basicAuthenticationInfo;

            client.EnableSsl = true;

            client.Send(message);





        }

    }

}

猜你喜欢

转载自blog.csdn.net/zhengjian1996/article/details/112936234