利用stmp协议发送邮件

网易,新浪,谷歌,腾讯的邮件服务都支持stmp协议。

using System.Net.Mail;

     MailMessage message = null;
        SmtpClient client = null;
        public void Send()
        {
            try
            {
                string argFrom = "[email protected]";
                string argTo = "[email protected]";
                string argSubject = "邮件测试";
                string argBody = "test";
                message = new MailMessage(argFrom, argTo, argSubject, argBody);
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.IsBodyHtml = true;
                message.Attachments.Add(new Attachment(@"D:\2e2sd42lxj.jpg"));

                client = new SmtpClient("smtp.gmail.com");
                client.UseDefaultCredentials = false;
                //client.Port = 465; // foxmail需要使用此端口,未验证
                // client.Port =587;// gmail需要使用此端口,本人用普通邮箱测试,使用此端口反而提示:由于连接方在一段时间后没有正确答复或连接

                client.EnableSsl = true; // gmail需要此安全设置,qq则不需要
                client.Credentials = new System.Net.NetworkCredential("[email protected]", "password");
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.Timeout = 100000;
                client.Send(message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

 

转载于:https://www.cnblogs.com/Kakasi/archive/2013/03/21/2973363.html

你可能感兴趣的:(利用stmp协议发送邮件)