利用stmp协议发送邮件

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

using System.Net.Mail;

     MailMessage message = null;
        SmtpClient client = null;
        public void Send()
        {
            try
            {
                string argFrom = "test@gmail.com";
                string argTo = "test@hotmail.com";
                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("test@gmail.com", "password");
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.Timeout = 100000;
                client.Send(message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

 

posted @ 2013-03-21 14:31  V.Wang  阅读(328)  评论(0编辑  收藏  举报